@@ -24,21 +24,31 @@ interface Option {
2424 readonly value : string ;
2525}
2626
27- export function WidgetTags ( props : WidgetProps ) {
27+ export function WidgetTagger ( props : WidgetProps ) {
2828 const { field } = props ;
2929
30- if ( field . type !== 'array' ) {
31- throw new Error ( 'WidgetTags only supports array fields' ) ;
30+ if ( field . type === 'string' ) {
31+ if ( ! field . enum ) {
32+ throw new Error ( "WidgetTagger: 'enum' is required for string fields" ) ;
33+ }
34+
35+ return < WidgetTaggerWithOptions { ...props } /> ;
3236 }
3337
34- if ( field . items . type === 'string' && field . items . enum ) {
35- return < WidgetTagsWithOptions { ...props } /> ;
38+ if ( field . type === 'array' && field . items . type === 'string' ) {
39+ return field . items . enum ? (
40+ < WidgetTaggerWithOptions { ...props } isMulti />
41+ ) : (
42+ < WidgetTaggerNoOptions { ...props } />
43+ ) ;
3644 }
3745
38- return < WidgetTagsNoOptions { ...props } /> ;
46+ throw new Error (
47+ `WidgetTagger: Field type must be 'string' or 'array'. Got: ${ field . type } `
48+ ) ;
3949}
4050
41- function WidgetTagsNoOptions ( props : WidgetProps ) {
51+ function WidgetTaggerNoOptions ( props : WidgetProps ) {
4252 const { pointer, isRequired, field } = props ;
4353
4454 const [ inputValue , setInputValue ] = useState ( '' ) ;
@@ -89,17 +99,26 @@ function WidgetTagsNoOptions(props: WidgetProps) {
8999 ) ;
90100}
91101
92- function WidgetTagsWithOptions ( props : WidgetProps ) {
93- const { pointer, isRequired } = props ;
94- const field = props . field as SchemaFieldArray < SchemaFieldString > ;
102+ function WidgetTaggerWithOptions ( props : WidgetProps & { isMulti ?: boolean } ) {
103+ const { pointer, isRequired, isMulti, field } = props ;
95104
96105 const [ inputValue , setInputValue ] = useState ( '' ) ;
97106
98107 const [ { value } , , { setValue } ] = useField ( pointer ) ;
99- const selectedValues = Array . isArray ( value ) ? value . map ( createOption ) : [ ] ;
108+ const selectedValues = value
109+ ? isMulti
110+ ? value . map ( createOption )
111+ : createOption ( value )
112+ : isMulti
113+ ? [ ]
114+ : undefined ;
100115
101116 const options = useMemo ( ( ) => {
102- return field . items . enum ! . map < Option > ( ( [ value , label ] ) => ( {
117+ const enums = isMulti
118+ ? ( field as SchemaFieldArray < SchemaFieldString > ) . items . enum
119+ : ( field as SchemaFieldString ) . enum ;
120+
121+ return enums ! . map < Option > ( ( [ value , label ] ) => ( {
103122 value,
104123 label
105124 } ) ) ;
@@ -128,14 +147,20 @@ function WidgetTagsWithOptions(props: WidgetProps) {
128147 < CreatableSelect
129148 inputValue = { inputValue }
130149 isClearable
131- isMulti
150+ isMulti = { isMulti }
132151 onChange = { ( newValue ) => {
133- setValue ( newValue . map ( ( option ) => option . value ) ) ;
152+ if ( isMulti ) {
153+ setValue (
154+ ( ( newValue as Option [ ] ) || [ ] ) . map ( ( option ) => option . value )
155+ ) ;
156+ } else {
157+ setValue ( newValue ?. value ) ;
158+ }
134159 } }
135160 onInputChange = { ( newValue ) => setInputValue ( newValue ) }
136161 onKeyDown = { handleKeyDown }
137162 options = { options }
138- placeholder = 'Select or type to add options '
163+ placeholder = 'Select or type to add'
139164 value = { selectedValues }
140165 />
141166 </ FormControl >
0 commit comments