Skip to content

Commit 6998be7

Browse files
committed
fix(Form): 增加datePicker
1 parent 7c776a9 commit 6998be7

File tree

6 files changed

+106
-33
lines changed

6 files changed

+106
-33
lines changed

example/examples/src/routes/Form/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ const FormDemo = () => {
113113
step: 0.2,
114114
},
115115
},
116+
{
117+
type: 'datePicker',
118+
field: 'datePicker',
119+
name: 'datePicker时间选择器',
120+
attr: {
121+
placeholder: '请选择',
122+
},
123+
},
116124
{
117125
type: 'treeSelect',
118126
field: 'treeSelect',
@@ -212,7 +220,7 @@ const FormDemo = () => {
212220
<Form
213221
form={form}
214222
schema={schema}
215-
initialValues={{name: '王滴滴', rate: 4, picker: ['2']}}
223+
initialValues={{name: '王滴滴', rate: 4, datePicker: new Date(), picker: ['2']}}
216224
watch={{
217225
name: (value: unknown) => console.log('value', value),
218226
}}

packages/core/src/DatePicker/index.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { forwardRef, useImperativeHandle } from 'react';
22
import { TouchableOpacity, SafeAreaView, View } from 'react-native';
33
import Flex from '../Flex';
44
import Modal from '../Modal';
5-
import H6 from '../Typography/H6';
5+
import Text from '../Typography/Text';
66
import dayjs from 'dayjs';
77
import DatePickerRN from './components/date-picker';
88
import { DatePickerPropsBase, ModalPickerProps } from './components/date-picker/type';
@@ -21,7 +21,7 @@ const DatePicker = forwardRef<DatePickerRef, DatePickerProps>((props, ref) => {
2121
displayType = 'modal',
2222
visible = false,
2323
onClosed,
24-
format = 'YYYY-MM-DD HH:mm',
24+
format = 'YYYY-MM-DD HH:mm:ss',
2525
labelUnit = { year: '年', month: '月', day: '日', hour: '时', minute: '分' },
2626
mode = 'date',
2727
minDate,
@@ -71,15 +71,21 @@ const DatePicker = forwardRef<DatePickerRef, DatePickerProps>((props, ref) => {
7171
<Flex justify="between">
7272
<Flex.Item>
7373
<TouchableOpacity activeOpacity={0.5} onPress={handleClose}>
74-
<H6 style={{ color: theme.colors.primary_background || '#3578e5' }}>{cancelText}</H6>
74+
<Text color="primary_background" style={{ fontSize: 16 }}>
75+
{cancelText}
76+
</Text>
7577
</TouchableOpacity>
7678
</Flex.Item>
7779
<Flex.Item>
78-
<H6 style={{ color: theme.colors.primary_background || '#3578e5' }}>{title}</H6>
80+
<Text color="primary_background" style={{ fontSize: 16 }}>
81+
{title}
82+
</Text>
7983
</Flex.Item>
8084
<Flex.Item>
8185
<TouchableOpacity activeOpacity={0.5} onPress={handleOk}>
82-
<H6 style={{ color: theme.colors.primary_background || '#3578e5' }}>{okText}</H6>
86+
<Text color="primary_background" style={{ fontSize: 16 }}>
87+
{okText}
88+
</Text>
8389
</TouchableOpacity>
8490
</Flex.Item>
8591
</Flex>

packages/core/src/Form/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export default FormDemo
326326
| 参数 | 说明 | 类型 | 默认值 |
327327
|------|------|-----|------|
328328
| `field` | 字段名 | string | - |
329-
| `type` | 字段类型(`input` \| `textArea` \| `slider` \| `rate` \| `radio` \| `search`\| `switch`\| `checkBox`\| `stepper`\| `picker`\| `treeSelect`\ | `cardList`) | string | - |
329+
| `type` | 字段类型(`input` \| `textArea` \| `slider` \| `rate` \| `radio` \| `search`\| `switch`\| `checkBox`\| `stepper`\| `picker`\| `treeSelect`\ | `datePicker`\ | `cardList`) | string | - |
330330
| `name` | 标签名 | string | - |
331331
| `validate` | 验证规则 | RulesOption['validate'] | - |
332332
| `options` | 集合 | Array<{ label: string; value: KeyType }> | - |
Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,71 @@
11
import React, { useState } from 'react';
2-
import { SafeAreaView, Pressable, View, Text, StyleSheet } from 'react-native';
2+
import { Pressable, SafeAreaView, View, StyleSheet } from 'react-native';
33
import DatePicker, { DatePickerProps } from '../../DatePicker';
4+
import Ellipsis from '../../Ellipsis';
45
import Icon from '../../Icon';
6+
import { Theme } from '../../theme';
7+
import { useTheme } from '@shopify/restyle';
8+
import dayjs from 'dayjs';
59

6-
const FormDatePicker = ({ value, ok, ...attr }: DatePickerProps & { ok?: (value: string) => void }) => {
10+
const FormDatePicker = ({
11+
disabled,
12+
placeholder,
13+
contentStyle,
14+
extra,
15+
showClear,
16+
value,
17+
onChange,
18+
format = 'YYYY-MM-DD HH:mm:ss',
19+
...attr
20+
}: any) => {
21+
console.log('format', format);
722
const [visible, setVisible] = useState(false);
23+
const theme = useTheme<Theme>();
24+
const style = createStyles({
25+
disabled: theme.colors.disabled,
26+
backgroundColor: theme.colors.mask,
27+
title: theme.colors.primary_text,
28+
});
29+
30+
const extraContent = React.useMemo(() => {
31+
if (React.isValidElement(extra)) {
32+
return extra;
33+
}
34+
if (value && showClear) {
35+
return (
36+
<Pressable onPress={() => onChange?.('')} style={{ paddingRight: 3 }} disabled={disabled}>
37+
<Icon name="circle-close-o" size={18} color={theme.colors.primary_text || '#ccc'} />
38+
</Pressable>
39+
);
40+
}
41+
return <Icon name="right" size={18} color={theme.colors.text || '#ccc'} />;
42+
}, [extra, value, showClear]);
43+
844
return (
945
<SafeAreaView>
10-
<Pressable onPress={() => setVisible(true)}>
11-
<View style={[styles.content]}>
12-
<Text style={styles.contentTitle}>{value ? value : ''}</Text>
13-
<Icon name="right" size={18} color="#A19EA0" />
46+
<Pressable
47+
onPress={() => {
48+
if (disabled) return;
49+
setVisible(true);
50+
}}
51+
>
52+
<View style={[disabled ? style.disabled : style.content, contentStyle]}>
53+
<Ellipsis style={[style.contentTitle]} maxLen={30}>
54+
{(value && dayjs(value).format(format)) || placeholder}
55+
</Ellipsis>
56+
{extraContent}
1457
</View>
1558
</Pressable>
1659
<DatePicker
17-
value={value}
60+
title="请选择日期"
61+
mode="datetime"
1862
visible={visible}
63+
value={value}
1964
onClosed={() => setVisible(false)}
20-
onOk={(value) => {
21-
setVisible(false);
22-
ok?.(value);
65+
onChange={(value) => {
66+
onChange?.(value);
2367
}}
68+
format={format}
2469
{...attr}
2570
/>
2671
</SafeAreaView>
@@ -29,17 +74,29 @@ const FormDatePicker = ({ value, ok, ...attr }: DatePickerProps & { ok?: (value:
2974

3075
export default FormDatePicker;
3176

32-
const styles = StyleSheet.create({
33-
contentTitle: {
34-
fontSize: 16,
35-
color: 'black',
36-
},
37-
content: {
38-
flexDirection: 'row',
39-
height: 35,
40-
alignItems: 'center',
41-
justifyContent: 'space-between',
42-
paddingRight: 5,
43-
backgroundColor: '#fff',
44-
},
45-
});
77+
function createStyles({ backgroundColor = '#fff', disabled = '#f5f5f5', title = '#000' }) {
78+
return StyleSheet.create({
79+
content: {
80+
flexDirection: 'row',
81+
height: 35,
82+
alignItems: 'center',
83+
justifyContent: 'space-between',
84+
paddingRight: 5,
85+
backgroundColor: backgroundColor,
86+
paddingHorizontal: 16,
87+
},
88+
disabled: {
89+
flexDirection: 'row',
90+
height: 35,
91+
alignItems: 'center',
92+
justifyContent: 'space-between',
93+
paddingRight: 5,
94+
backgroundColor: disabled,
95+
paddingHorizontal: 16,
96+
},
97+
contentTitle: {
98+
fontSize: 16,
99+
color: title,
100+
},
101+
});
102+
}

packages/core/src/Form/comps/picker.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Theme } from '../../theme';
88
import { useTheme } from '@shopify/restyle';
99
import { getTreeDeep } from '../../utils/tree-select';
1010

11-
export interface RnPickerProps extends PickerProps {
11+
export interface RnPickerProps extends Omit<PickerProps, 'data'> {
1212
disabled?: boolean;
1313
placeholder?: string;
1414
contentStyle?: ViewStyle;

packages/core/src/Form/form.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Radio from './comps/radio';
1414
import Switch from './comps/switch';
1515
import CheckBox from './comps/checkBox';
1616
import Picker from './comps/picker';
17+
import DatePicker from './comps/datePicker';
1718

1819
const Form = (baseProps: FormProps) => {
1920
const {
@@ -58,7 +59,8 @@ const Form = (baseProps: FormProps) => {
5859
checkBox: <CheckBox />,
5960
stepper: <Stepper value={0} onChange={() => {}} />,
6061
treeSelect: <TreeSelect options={[]} />,
61-
picker: <Picker options={[]} />,
62+
picker: <Picker />,
63+
datePicker: <DatePicker />,
6264
},
6365
changeValidate: changeValidate,
6466
};

0 commit comments

Comments
 (0)