/
SelectInput.tsx
189 lines (169 loc) · 4.84 KB
/
SelectInput.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
import {isTitledListValue, TitledListValue} from '@sanity/types'
import {Box, Card, Flex, Inline, Radio, Select, Stack, Text} from '@sanity/ui'
import {capitalize} from 'lodash'
import React, {useId, forwardRef, useCallback, useMemo} from 'react'
import {ChangeIndicator} from '../../changeIndicators'
import {PatchEvent, set, unset} from '../patch'
import {StringInputProps} from '../types'
function toSelectItem(
option: TitledListValue<string | number> | string | number
): TitledListValue<string | number> {
return isTitledListValue(option) ? option : {title: capitalize(`${option}`), value: option}
}
const EMPTY_ITEM = {title: '', value: undefined}
/**
* @beta
*/
export function SelectInput(props: StringInputProps) {
const {
value,
readOnly,
validationError,
schemaType,
onChange,
path,
changed,
focused,
elementProps,
} = props
const items = useMemo(
() => (schemaType.options?.list || []).map(toSelectItem),
[schemaType.options?.list]
)
const currentItem = items.find((item) => item.value === value)
const isRadio = schemaType.options && schemaType.options.layout === 'radio'
const itemFromOptionValue = useCallback(
(optionValue: any) => {
const index = Number(optionValue)
return items[index]
},
[items]
)
const optionValueFromItem = useCallback(
(item: any) => {
return String(items.indexOf(item))
},
[items]
)
const inputId = useId()
const handleChange = React.useCallback(
(nextItem: TitledListValue<string | number> | null) => {
onChange(
PatchEvent.from(typeof nextItem?.value === 'undefined' ? unset() : set(nextItem.value))
)
},
[onChange]
)
const handleSelectChange = useCallback(
(event: React.ChangeEvent<HTMLSelectElement>) => {
const nextItem = itemFromOptionValue(event.currentTarget.value)
if (!nextItem) {
handleChange(EMPTY_ITEM)
return
}
handleChange(nextItem)
},
[handleChange, itemFromOptionValue]
)
const content = isRadio ? (
<RadioSelect
{...elementProps}
value={currentItem}
inputId={inputId}
items={items}
direction={schemaType.options?.direction || 'vertical'}
customValidity={validationError}
onChange={handleChange}
readOnly={readOnly}
/>
) : (
<Select
{...elementProps}
customValidity={validationError}
value={optionValueFromItem(currentItem)}
readOnly={readOnly}
onChange={handleSelectChange}
>
{[EMPTY_ITEM, ...items].map((item, i) => (
<option key={`${i - 1}`} value={i - 1}>
{item.title}
</option>
))}
</Select>
)
return (
<ChangeIndicator path={path} isChanged={changed} hasFocus={!!focused}>
{content}
</ChangeIndicator>
)
}
const RadioSelect = forwardRef(function RadioSelect(
props: {
items: TitledListValue<string | number>[]
value?: TitledListValue<string | number>
direction: 'horizontal' | 'vertical'
readOnly?: boolean
onChange: (value: TitledListValue<string | number> | null) => void
onFocus: (event: React.FocusEvent<HTMLElement>) => void
customValidity?: string
inputId?: string
},
ref: React.ForwardedRef<HTMLInputElement>
) {
const {items, value, onChange, onFocus, readOnly, customValidity, direction, inputId} = props
const Layout = direction === 'horizontal' ? Inline : Stack
return (
<Card border padding={3} radius={1}>
<Layout space={3} role="group">
{items.map((item, index) => (
<RadioSelectItem
customValidity={customValidity}
inputId={inputId}
item={item}
key={index}
onChange={onChange}
onFocus={onFocus}
readOnly={readOnly}
ref={index === 0 ? ref : null}
value={value}
/>
))}
</Layout>
</Card>
)
})
const RadioSelectItem = forwardRef(function RadioSelectItem(
props: {
customValidity?: string
inputId?: string
item: TitledListValue<string | number>
onChange: (value: TitledListValue<string | number> | null) => void
onFocus: (event: React.FocusEvent<HTMLElement>) => void
readOnly?: boolean
value?: TitledListValue<string | number>
},
ref: React.ForwardedRef<HTMLInputElement>
) {
const {customValidity, inputId, item, onChange, onFocus, readOnly, value} = props
const handleChange = useCallback(() => {
onChange(item)
}, [item, onChange])
return (
<Flex as="label" align="center">
<Radio
ref={ref}
checked={value === item}
onChange={handleChange}
onFocus={onFocus}
readOnly={readOnly}
customValidity={customValidity}
name={inputId}
/>
<Box marginLeft={2}>
<Text size={1} weight="semibold">
{item.title}
</Text>
</Box>
</Flex>
)
})