Skip to content

Commit 644a5a0

Browse files
committed
fix(Form): 集成tree组件
1 parent 8451925 commit 644a5a0

File tree

6 files changed

+207
-9
lines changed

6 files changed

+207
-9
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,49 @@ const FormDemo = () => {
148148
field: 'verificationCode',
149149
name: '验证码倒计时',
150150
},
151+
{
152+
type: 'tree',
153+
field: 'tree',
154+
name: '树形控件',
155+
attr: {
156+
options: [
157+
{
158+
label: '今天',
159+
value: '01',
160+
children: [
161+
{
162+
label: '上午',
163+
value: '01-1',
164+
children: [
165+
{
166+
label: '9点',
167+
value: '01-1-1',
168+
},
169+
{
170+
label: '10点',
171+
value: '01-1-2',
172+
},
173+
{
174+
label: '11点',
175+
value: '01-1-3',
176+
},
177+
],
178+
},
179+
{
180+
label: '下午',
181+
value: '02-1',
182+
children: [
183+
{
184+
label: '14点',
185+
value: '02-1-1',
186+
},
187+
],
188+
},
189+
],
190+
},
191+
],
192+
},
193+
},
151194
{
152195
type: 'treeSelect',
153196
field: 'treeSelect',

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,24 @@ export default class TreeDemo extends React.Component<TreeViewProps> {
6666
<Button onPress={() => this.setState({visible: true})}>打开</Button>
6767
<Modal visible={this.state.visible} placement="bottom" onClosed={() => this.setState({visible: false})}>
6868
<ScrollView style={{height: 500}}>
69-
<Tree treeData={option} defaultExpandAll />
69+
<Tree
70+
treeData={option}
71+
defaultExpandAll
72+
onCheck={(value: any) => {
73+
console.log('onCheck', value);
74+
}}
75+
/>
7076
</ScrollView>
7177
</Modal>
7278
</Card>
7379
<Card title="树形选择">
74-
<Tree treeData={option} defaultExpandAll />
80+
<Tree
81+
treeData={option}
82+
defaultExpandAll
83+
onCheck={(value: any) => {
84+
console.log('onCheck', value);
85+
}}
86+
/>
7587
</Card>
7688
</Body>
7789
</React.Fragment>

packages/core/src/DatePicker/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ DatePicker
66
77
### 基础示例
88

9-
```jsx mdx:preview&background=#bebebe29
9+
```jsx
1010
import { Button } from '@uiw/react-native';
1111
import React,{ useState } from 'react';
1212
import { View,Text } from 'react-native';
@@ -63,7 +63,7 @@ DatePeriodInput
6363

6464
### 基础示例
6565

66-
```jsx mdx:preview&background=#bebebe29
66+
```jsx
6767
import React,{ useState } from 'react';
6868
import { View} from 'react-native';
6969
import DatePeriodInput from '@uiw/react-native/lib/DatePicker/date-period-input';
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import Tree from '../../Tree';
2+
import { KeyType } from '../types';
3+
import React, { useState } from 'react';
4+
import { Pressable, SafeAreaView, View, StyleSheet, ViewStyle, ScrollView } from 'react-native';
5+
import Ellipsis from '../../Ellipsis';
6+
import Modal from '../../Modal';
7+
import Icon from '../../Icon';
8+
import { Theme } from '../../theme';
9+
import { useTheme } from '@shopify/restyle';
10+
11+
interface FormTreeProps {
12+
value?: any;
13+
onChange?: (value: KeyType) => void;
14+
options?: Array<{ label: string; value: KeyType }>;
15+
disabled?: boolean;
16+
placeholder?: string;
17+
contentStyle?: ViewStyle;
18+
extra?: JSX.Element;
19+
showClear?: boolean;
20+
}
21+
22+
interface entity {
23+
value: KeyType;
24+
label: string;
25+
children?: children[];
26+
}
27+
interface children {
28+
value: KeyType;
29+
label: string;
30+
children?: children[];
31+
}
32+
33+
const FormTree = ({
34+
value,
35+
onChange,
36+
options = [],
37+
// ...others,
38+
disabled,
39+
placeholder = '请选择',
40+
contentStyle,
41+
extra,
42+
showClear,
43+
...attr
44+
}: FormTreeProps) => {
45+
const [visible, setVisible] = useState(false);
46+
const theme = useTheme<Theme>();
47+
const style = createStyles({
48+
disabled: theme.colors.disabled,
49+
backgroundColor: theme.colors.mask,
50+
title: theme.colors.primary_text,
51+
});
52+
53+
function treeToArray(tree: any) {
54+
var res: any = [];
55+
for (const item of tree) {
56+
const { children, ...i } = item;
57+
if (children && children.length) {
58+
res = res.concat(treeToArray(children));
59+
}
60+
res.push(i);
61+
}
62+
return res;
63+
}
64+
let arrTools = treeToArray(options).filter((filItem: any) =>
65+
value?.some((somItem: any) => Object.is(filItem.value, somItem)),
66+
);
67+
68+
const labelVal = arrTools.map((a: any) => {
69+
return a.label;
70+
});
71+
72+
const extraContent = React.useMemo(() => {
73+
if (React.isValidElement(extra)) {
74+
return extra;
75+
}
76+
if (value && showClear) {
77+
return (
78+
<Pressable onPress={() => onChange} style={{ paddingRight: 3 }} disabled={disabled}>
79+
<Icon name="circle-close-o" size={18} color={theme.colors.primary_text || '#ccc'} />
80+
</Pressable>
81+
);
82+
}
83+
return <Icon name="right" size={18} color={theme.colors.text || '#ccc'} />;
84+
}, [extra, value, showClear]);
85+
return (
86+
<SafeAreaView>
87+
<Pressable
88+
onPress={() => {
89+
if (disabled) return;
90+
setVisible(true);
91+
}}
92+
>
93+
<View style={[disabled ? style.disabled : style.content, contentStyle]}>
94+
<Ellipsis style={[style.contentTitle]} maxLen={30}>
95+
{(labelVal.length > 0 && labelVal) || placeholder}
96+
</Ellipsis>
97+
{extraContent}
98+
</View>
99+
</Pressable>
100+
<Modal visible={visible} placement="bottom" onClosed={() => setVisible(false)}>
101+
<ScrollView style={{ height: 400 }}>
102+
<Tree
103+
treeData={options}
104+
defaultExpandAll
105+
onCheck={(value: any) => {
106+
onChange?.(value);
107+
}}
108+
/>
109+
</ScrollView>
110+
</Modal>
111+
</SafeAreaView>
112+
);
113+
};
114+
115+
export default FormTree;
116+
117+
function createStyles({ backgroundColor = '#fff', disabled = '#f5f5f5', title = '#000' }) {
118+
return StyleSheet.create({
119+
content: {
120+
flexDirection: 'row',
121+
height: 35,
122+
alignItems: 'center',
123+
justifyContent: 'space-between',
124+
paddingRight: 5,
125+
backgroundColor: backgroundColor,
126+
paddingHorizontal: 16,
127+
},
128+
disabled: {
129+
flexDirection: 'row',
130+
height: 35,
131+
alignItems: 'center',
132+
justifyContent: 'space-between',
133+
paddingRight: 5,
134+
backgroundColor: disabled,
135+
paddingHorizontal: 16,
136+
},
137+
contentTitle: {
138+
fontSize: 16,
139+
color: title,
140+
},
141+
});
142+
}

packages/core/src/Form/form.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Switch from './comps/switch';
1717
import CheckBox from './comps/checkBox';
1818
import Picker from './comps/picker';
1919
import DatePicker from './comps/datePicker';
20+
import Tree from './comps/tree';
2021

2122
const Form = (baseProps: FormProps) => {
2223
const {
@@ -65,6 +66,7 @@ const Form = (baseProps: FormProps) => {
6566
datePicker: <DatePicker />,
6667
datePeriodInput: <DatePeriodInput />,
6768
verificationCode: <VerificationCode outerStyle={{ backgroundColor: '#FFF' }} />,
69+
tree: <Tree />,
6870
},
6971
changeValidate: changeValidate,
7072
};

packages/core/src/Tree/components/TreeNode.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ const TreeNode: FC<TreeNodeProps> = (props) => {
2929
return customIcon(checked);
3030
}
3131
return (
32-
<CheckBox
33-
checked={checked}
34-
onChange={(checked) => {
35-
console.log(checked, 'checked');
36-
}}
32+
<Icon
33+
size={16}
34+
name={checked ? 'circle-check' : 'circle-o'}
35+
color={checked ? theme.colors.primary200 : theme.colors.gray300}
3736
/>
3837
);
3938
};

0 commit comments

Comments
 (0)