Skip to content

feat(Pagination): 增加简洁版本 & 更新文档 #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions example/examples/src/routes/Pagination/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class Index extends Component<IndexProps, IndexState> {
this.state = {
current: 1,
current1: 1,
current2: 2,
};
}

Expand All @@ -31,18 +32,6 @@ export default class Index extends Component<IndexProps, IndexState> {
<Layout>
<Header title={title} description={description} />
<Body style={{backgroundColor: '#fff'}}>
<Card title="使用文字">
<View style={{paddingHorizontal: 20}}>
<Pagination
current={this.state.current}
total={60}
pageSize={8}
onPageChange={(type, current) => {
this.setState({current});
}}
/>
</View>
</Card>
<Card title="使用跳转页码">
<View style={{paddingHorizontal: 20}}>
<Pagination
Expand All @@ -57,6 +46,32 @@ export default class Index extends Component<IndexProps, IndexState> {
/>
</View>
</Card>
<Card title="简单跳转">
<View style={{paddingHorizontal: 20}}>
<Pagination
simple
icon
current={this.state.current2}
total={100}
pageSize={10}
onPageChange={(type, current2) => {
this.setState({current2});
}}
/>
</View>
</Card>
<Card title="使用文字">
<View style={{paddingHorizontal: 20}}>
<Pagination
current={this.state.current}
total={60}
pageSize={8}
onPageChange={(type, current) => {
this.setState({current});
}}
/>
</View>
</Card>
<Card title="使用icon">
<View style={{paddingHorizontal: 20}}>
<Pagination
Expand Down
81 changes: 64 additions & 17 deletions packages/core/src/Pagination/Page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { View, StyleSheet, Text, TouchableHighlight } from 'react-native';
import React, { useState, useEffect } from 'react';
import { View, ViewStyle, TextStyle, StyleSheet, Text, TouchableHighlight, TextInput } from 'react-native';
import { containerStyle, containerSize, contentSize } from './DirText';
import { size } from './index';
import Button from '../Button';
Expand All @@ -11,10 +11,19 @@ export interface PageProps {
totalPage: number;
renderPages?: (current: number, totalPage: number) => React.ReactNode;
onCurrent?: (current: number, totalPage?: number) => unknown;
setCurrent: React.Dispatch<React.SetStateAction<number>>;
simple?: boolean;
}

const Page = (props: PageProps) => {
const { size, currentColor, current, totalPage, renderPages, onCurrent } = props;
const { size, currentColor, current, totalPage, renderPages, onCurrent, setCurrent, simple } = props;

useEffect(() => {
setJumpCurrent(String(current));
}, [current]);
const [jumpCurrent, setJumpCurrent] = useState('1');
const [currentType, setJumpCurrentType] = useState(true);

const textSize = size === 'small' ? 1 : 2;
if (renderPages) {
return (
Expand All @@ -30,7 +39,35 @@ const Page = (props: PageProps) => {
{ minWidth: containerSize[size], height: containerSize[size], borderWidth: 0, flexShrink: 0 },
]}
>
<Button bordered={false}>
{simple === true ? (
<TextInput
keyboardType="number-pad"
onBlur={() => {
let newJumpCurrent = Number(jumpCurrent);
if (newJumpCurrent >= totalPage) {
setCurrent(totalPage);
} else {
setCurrent(newJumpCurrent);
}
}}
onFocus={() => {
setJumpCurrent('');
}}
blurOnSubmit={true}
onChangeText={(text) => {
setJumpCurrent(text);
}}
value={jumpCurrent}
style={[
styles.inputStyle,
{
color: currentColor ?? '#46a6ff',
fontSize: contentSize[size],
lineHeight: contentSize[size] + textSize,
},
]}
/>
) : (
<Text
style={{
color: currentColor ?? '#46a6ff',
Expand All @@ -40,24 +77,34 @@ const Page = (props: PageProps) => {
>
{current}
</Text>
<Text
style={{
color: currentColor ?? '#46a6ff',
fontSize: contentSize[size] - 1,
lineHeight: contentSize[size] - textSize,
}}
>
/
</Text>
<Text style={{ color: '#3d3d3d', fontSize: contentSize[size], lineHeight: contentSize[size] + textSize }}>
{totalPage}
</Text>
</Button>
)}
<Text
style={{
color: currentColor ?? '#46a6ff',
fontSize: contentSize[size] - 1,
lineHeight: contentSize[size] - textSize,
}}
>
/
</Text>
<Text style={{ color: '#3d3d3d', fontSize: contentSize[size], lineHeight: contentSize[size] + textSize }}>
{totalPage}
</Text>
</View>
);
};

export const inputStyle: ViewStyle | TextStyle = {
height: 27,
width: 33,
borderColor: 'gray',
borderWidth: 0.5,
textAlign: 'center',
padding: 2,
marginHorizontal: 3,
};
const styles = StyleSheet.create({
containerStyle,
inputStyle,
});
export default Page;
28 changes: 27 additions & 1 deletion packages/core/src/Pagination/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Pagination 分页器

用于展示页码、请求数据等。

<img src='https://user-images.githubusercontent.com/66067296/140001996-ff0fe66c-0482-4576-9f19-11be3a6b7ada.png' alt='Pagination' style='zoom:33%;' />
<img src='https://user-images.githubusercontent.com/66067296/140044665-d27bccd1-24ba-4eaf-949b-89b6dc9f0dad.png' alt='Pagination' style='zoom:33%;' />

### 基础示例

Expand Down Expand Up @@ -78,6 +78,30 @@ function Demo() {
}
```

### 简单版本

```jsx
import { Fragment, useState } from 'react';
import { Pagination } from '@uiw/react-native';
function Demo() {
const [current, setCurrent] = useState(false)
return (
<Fragment>
<Pagination
current={current}
total={60}
pageSize={8}
simple
onPageChange={(type, current) => {
setCurrent(current)
console.log('type, current: ', type, current);
}}
/>
</Fragment>
);
}
```

### Props

```ts
Expand Down Expand Up @@ -106,5 +130,7 @@ export interface PaginationProps {
color?: string
/** 页码跳转 */
jumpBtn?: boolean;
/** 简洁版本 */
simple?: boolean;
}
```
5 changes: 5 additions & 0 deletions packages/core/src/Pagination/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ export interface PaginationProps {
color?: string;
/** 页码跳转 */
jumpBtn?: boolean;
/** 简洁版本 */
simple?: boolean;
}

const Pagination = (props: PaginationProps) => {
const {
size = 'default',
icon = false,
jumpBtn = false,
simple = false,
currentColor,
total,
pageSize = 10,
Expand Down Expand Up @@ -78,6 +81,8 @@ const Pagination = (props: PaginationProps) => {
color={color}
/>
<Page
simple={simple}
setCurrent={setCurrent}
renderPages={renderPages}
onCurrent={onCurrent}
size={size}
Expand Down