This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Dictionary.tsx
147 lines (136 loc) · 4.4 KB
/
Dictionary.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
import React from 'react'
import { Icon, Select as AntdSelect } from 'antd'
import Select, { SelectProps } from '../Select/Select'
import ReadOnlyField from '../ReadOnlyField/ReadOnlyField'
import { MultivalueSingleValue } from '../../../interfaces/data'
import { BaseFieldProps } from '../../Field/Field'
export interface DictionaryProps extends BaseFieldProps {
value?: MultivalueSingleValue[] | string | null
onChange?: (value: MultivalueSingleValue[] | string) => void
values: Array<{ value: string; icon?: string }>
fieldName: string
placeholder?: string
style?: React.CSSProperties
metaIcon?: JSX.Element
valueIcon?: string
popupContainer?: HTMLElement
multiple?: boolean
}
/**
*
* @param props
* @category Components
*/
const Dictionary: React.FunctionComponent<DictionaryProps> = props => {
const {
multiple,
onChange,
values,
value,
valueIcon,
meta,
metaIcon,
readOnly,
className,
backgroundColor,
onDrillDown,
widgetName
} = props
const selectRef = React.useRef<AntdSelect<string>>(null)
const handleOnChange = React.useCallback(
(v: string | string[]) => {
if (multiple) {
onChange((v as string[]).map(item => ({ id: item, value: item })))
} else {
onChange((v as string) || '')
}
},
[multiple, onChange]
)
const resultValue = React.useMemo(() => {
if (multiple) {
return (value as MultivalueSingleValue[])?.map(i => i.value) ?? []
} else {
return value ?? undefined
}
}, [value, multiple])
const extendedProps: SelectProps = {
...props,
mode: multiple ? 'multiple' : 'default',
value: resultValue as string | string[],
allowClear: !!value,
showSearch: true,
onChange: handleOnChange,
dropdownMatchSelectWidth: false,
getPopupContainer: trigger => trigger.parentElement,
forwardedRef: selectRef
}
const options = React.useMemo(() => {
const noValues = !values?.length
const hasMultipleValue = noValues && multiple && value?.length
const hasSingleValue = noValues && !multiple && value
if (hasMultipleValue) {
return (value as MultivalueSingleValue[])?.map(item => {
return (
<Select.Option key={item.value} title={item.value}>
{item.options?.icon && getIconByParams(item.options.icon)}
<span>{item.value}</span>
</Select.Option>
)
})
}
if (hasSingleValue) {
return (
<Select.Option key={value as string} title={value as string}>
{metaIcon}
{valueIcon && getIconByParams(valueIcon)}
<span>{value}</span>
</Select.Option>
)
}
return values?.map(item => {
return (
<Select.Option key={item.value} title={item.value}>
{item.icon && getIconByParams(item.icon)}
<span>{item.value}</span>
</Select.Option>
)
})
}, [value, values, multiple, metaIcon, valueIcon])
if (readOnly) {
let readOnlyValue = value ?? ''
if (multiple) {
readOnlyValue = (readOnlyValue as MultivalueSingleValue[]).map(i => i.value).join(', ')
}
return (
<ReadOnlyField
widgetName={widgetName}
meta={meta}
className={className}
backgroundColor={backgroundColor}
onDrillDown={onDrillDown}
>
{readOnlyValue}
</ReadOnlyField>
)
}
return <Select {...extendedProps}>{options}</Select>
}
/**
* Returns Icon component
*
* @param params Contains `type` and `color`
* @param extraStyleClasses extra css classes
*/
export function getIconByParams(params: string, extraStyleClasses?: string) {
if (params) {
const [antIconType, cssColor] = params.split(' ')
return <Icon type={antIconType} style={{ color: cssColor }} className={extraStyleClasses} />
}
return null
}
/**
* @category Components
*/
const MemoizedDictionary = React.memo(Dictionary)
export default MemoizedDictionary