|
| 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 | +} |
0 commit comments