-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathindex.tsx
276 lines (255 loc) · 7.89 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import React, { createContext, useContext } from 'react'
import { isArr, isEmpty, isValid, toArr } from '@formily/shared'
import { Field } from '@formily/core'
import { useField, observer } from '@formily/react'
import { InputProps } from '@alifd/next/lib/input'
import { SelectProps } from '@alifd/next/lib/select'
import { TreeSelectProps } from '@alifd/next/lib/tree-select'
import { CascaderProps } from '@alifd/next/lib/cascader'
import {
DatePickerProps,
RangePickerProps as DateRangePickerProps,
} from '@alifd/next/lib/date-picker'
import { TimePickerProps } from '@alifd/next/lib/time-picker'
import { Tag } from '@alifd/next'
import cls from 'classnames'
import { formatMomentValue, usePrefixCls } from '../__builtins__'
const PlaceholderContext = createContext<React.ReactNode>('N/A')
const Placeholder = PlaceholderContext.Provider
const usePlaceholder = (value?: any) => {
const placeholder = useContext(PlaceholderContext) || 'N/A'
return !isEmpty(value) ? value : placeholder
}
interface IGetValueByValue {
(
array: any[],
inputValue: any,
keyMap?: { inputKey?: string; outputKey?: string; childrenKey?: string },
path?: any[]
): any
}
const getValueByValue: IGetValueByValue = (
array,
inputValue,
keyMap,
path = []
) => {
const {
inputKey = 'value',
outputKey = 'label',
childrenKey = 'children',
} = keyMap || {}
let outputValue: any
if (isArr(array)) {
if (isArr(inputValue)) {
outputValue = inputValue.map((v) =>
getValueByValue(array, v, keyMap, path)
)
} else {
array.forEach((obj) => {
if (outputValue === undefined) {
const currentPath = [...path, obj?.[outputKey]]
if (obj?.[inputKey] === inputValue) {
outputValue = {
leaf: obj?.[outputKey],
whole: currentPath,
}
} else if (obj?.[childrenKey]?.length) {
outputValue = getValueByValue(
obj?.[childrenKey],
inputValue,
keyMap,
currentPath
)
}
}
})
}
return outputValue
}
return undefined
}
const Input: React.FC<InputProps> = (props) => {
const prefixCls = usePrefixCls('form-text', props)
return (
<div className={cls(prefixCls, props.className)}>
{props.addonBefore}
{props.innerBefore}
{usePlaceholder(props.value)}
{props.innerAfter}
{props.addonAfter}
</div>
)
}
const Select: React.FC<SelectProps> = observer((props) => {
const field = useField<Field>()
const prefixCls = usePrefixCls('form-text', props)
const dataSource: any[] = field?.dataSource?.length
? field.dataSource
: props?.dataSource?.length
? props.dataSource
: []
const placeholder = usePlaceholder()
const getSelected = () => {
const value = props.value
if (props.mode === 'multiple' || props.mode === 'tag') {
if (props.useDetailValue) {
return isArr(value) ? value : []
} else {
return isArr(value)
? value.map((val) => ({ label: val, value: val }))
: []
}
} else {
if (props.useDetailValue) {
return isValid(value) ? [value] : []
} else {
return isValid(value) ? [{ label: value, value }] : []
}
}
}
const getLabel = (target: any) => {
return (
dataSource?.find((item) => item.value == target?.value)?.label ||
target.label ||
placeholder
)
}
const getLabels = () => {
const selected = getSelected()
if (!selected.length) return placeholder
if (selected.length === 1) return getLabel(selected[0])
return selected.map((item, key) => {
return (
<Tag type="primary" size="small" key={key}>
{getLabel(item)}
</Tag>
)
})
}
return <div className={cls(prefixCls, props.className)}>{getLabels()}</div>
})
const TreeSelect: React.FC<TreeSelectProps> = observer((props) => {
const field = useField<Field>()
const placeholder = usePlaceholder()
const prefixCls = usePrefixCls('form-text', props)
const dataSource = field?.dataSource?.length
? field.dataSource
: props?.dataSource?.length
? props.dataSource
: []
const getSelected = () => {
const value = props.value
if (props.multiple) {
if (props['useDetailValue']) {
return isArr(value) ? value : []
} else {
return isArr(value)
? value.map((val) => ({ label: val, value: val }))
: []
}
} else {
if (props['useDetailValue']) {
return value ? [value] : []
} else {
return value ? [{ label: value, value }] : []
}
}
}
const findLabel = (value: any, dataSource: any[]) => {
for (let i = 0; i < dataSource?.length; i++) {
const item = dataSource[i]
if (item?.value === value) {
return item?.label
} else {
const childLabel = findLabel(value, item?.children)
if (childLabel) return childLabel
}
}
}
const getLabels = () => {
const selected = getSelected()
if (!selected?.length)
return (
<Tag type="primary" size="small">
{placeholder}
</Tag>
)
return selected.map(({ value, label }, key) => {
return (
<Tag type="primary" size="small" key={key}>
{findLabel(value, dataSource) || label || placeholder}
</Tag>
)
})
}
return <div className={cls(prefixCls, props.className)}>{getLabels()}</div>
})
const Cascader: React.FC<CascaderProps> = observer((props) => {
const field = useField<Field>()
const placeholder = usePlaceholder()
const prefixCls = usePrefixCls('form-text', props)
const dataSource: any[] = field?.dataSource?.length
? field.dataSource
: props?.dataSource?.length
? props.dataSource
: []
const getSelected = () => {
return props.multiple ? toArr(props.value) : [props.value]
}
const getLabels = () => {
const selected = getSelected()
const labels = getValueByValue(dataSource, selected)
?.filter((item) => isValid(item))
?.map((item) => item?.whole?.join('/'))
.join(', ')
return labels || placeholder
}
return <div className={cls(prefixCls, props.className)}>{getLabels()}</div>
})
const DatePicker: React.FC<DatePickerProps> = (props) => {
const placeholder = usePlaceholder()
const prefixCls = usePrefixCls('form-text', props)
const getLabels = () => {
const labels = formatMomentValue(props.value, props.format, placeholder)
return isArr(labels) ? labels.join('~') : labels
}
return <div className={cls(prefixCls, props.className)}>{getLabels()}</div>
}
const DateRangePicker: React.FC<DateRangePickerProps> = (props) => {
const placeholder = usePlaceholder()
const prefixCls = usePrefixCls('form-text', props)
const getLabels = () => {
const labels = formatMomentValue(props.value, props.format, placeholder)
return isArr(labels) ? labels.join('~') : labels
}
return <div className={cls(prefixCls, props.className)}>{getLabels()}</div>
}
const TimePicker: React.FC<TimePickerProps> = (props) => {
const placeholder = usePlaceholder()
const prefixCls = usePrefixCls('form-text', props)
const getLabels = () => {
const labels = formatMomentValue(props.value, props.format, placeholder)
return isArr(labels) ? labels.join('~') : labels
}
return <div className={cls(prefixCls, props.className)}>{getLabels()}</div>
}
const Text = (props: React.PropsWithChildren<any>) => {
const prefixCls = usePrefixCls('form-text', props)
return (
<div className={cls(prefixCls, props.className)} style={props.style}>
{usePlaceholder(props.value)}
</div>
)
}
Text.Input = Input
Text.Select = Select
Text.TreeSelect = TreeSelect
Text.Cascader = Cascader
Text.DatePicker = DatePicker
Text.DateRangePicker = DateRangePicker
Text.TimePicker = TimePicker
Text.Placeholder = Placeholder
Text.usePlaceholder = usePlaceholder
export const PreviewText = Text
export default PreviewText