@@ -2,7 +2,7 @@ import React, { useMemo } from 'react';
22import { useMyVariable } from '../context/MyVariableContext' ;
33import { debounce } from 'lodash' ;
44import CreatableSelect from 'react-select/creatable' ;
5- import { saveNewTags } from '../utils/saveNewTags'
5+ import { saveNewTags } from '../utils/saveNewTags' ;
66
77interface SelectTagsProps {
88 onSelect : ( names : string ) => void ;
@@ -11,39 +11,63 @@ interface SelectTagsProps {
1111}
1212
1313const SelectTags : React . FC < SelectTagsProps > = ( { onSelect, initialValue, type } ) => {
14- let initialOptions = initialValue ? initialValue . split ( ", " ) . map ( ( val ) => ( { label : val , value : val } ) ) : [ ] ;
14+ const initialOptions = initialValue
15+ ? initialValue . split ( ", " ) . map ( val => ( { label : val , value : val } ) )
16+ : [ ] ;
1517 const [ selectedLabels , setSelectedLabels ] = React . useState ( initialOptions ) ;
1618 const { myVariable } = useMyVariable ( ) ;
17- const options = myVariable . tags [ `${ type } ` ] || [
18- { value : 'chocolate' , label : 'Chocolate' } ,
19- { value : 'strawberry' , label : 'Strawberry' } ,
20- { value : 'vanilla' , label : 'Vanilla' }
21- ] ;
22-
19+ const options =
20+ myVariable . tags [ type ] ||
21+ [
22+ { value : 'chocolate' , label : 'Chocolate' } ,
23+ { value : 'strawberry' , label : 'Strawberry' } ,
24+ { value : 'vanilla' , label : 'Vanilla' }
25+ ] ;
26+
2327 React . useEffect ( ( ) => {
24- let initialOptions = initialValue ? initialValue . split ( ", " ) . map ( ( val ) => ( { label : val , value : val } ) ) : [ ] ;
25- setSelectedLabels ( initialOptions ) ;
28+ const initialOpts = initialValue
29+ ? initialValue . split ( ", " ) . map ( val => ( { label : val , value : val } ) )
30+ : [ ] ;
31+ setSelectedLabels ( initialOpts ) ;
2632 } , [ initialValue ] ) ;
2733
34+ // Debounced update that saves tags
2835 const debouncedHandleInputChange = useMemo (
29- ( ) => debounce ( async ( selected : any ) => {
30- setSelectedLabels ( selected ) ; // Update local state
31- const labs : string [ ] = selected . map ( ( item : any ) => item . label ) ;
32- const status = await saveNewTags ( labs , type ) ;
33- onSelect ( labs . join ( ", " ) ) ; // Update parent component's state
34- } , 1000 ) ,
36+ ( ) =>
37+ debounce ( async ( selected : any ) => {
38+ setSelectedLabels ( selected ) ; // Update local state
39+ const labs : string [ ] = selected . map ( ( item : any ) => item . label ) ;
40+ await saveNewTags ( labs , type ) ;
41+ onSelect ( labs . join ( ", " ) ) ; // Update parent component's state
42+ } , 1000 ) ,
3543 [ onSelect , type ]
3644 ) ;
37-
45+
46+ // Handle creation of new option (e.g., when user types comma separated tags)
47+ const handleCreateOption = ( inputValue : string ) => {
48+ // Split the input on commas, trim whitespace and remove empty strings
49+ const newTags = inputValue
50+ . split ( ',' )
51+ . map ( tag => tag . trim ( ) )
52+ . filter ( tag => tag ) ;
53+ // Map each tag to the option format
54+ const newOptions = newTags . map ( tag => ( { label : tag , value : tag } ) ) ;
55+ // Merge with the existing selected labels
56+ const updatedSelected = [ ...selectedLabels , ...newOptions ] ;
57+ setSelectedLabels ( updatedSelected ) ;
58+ debouncedHandleInputChange ( updatedSelected ) ;
59+ } ;
60+
3861 return (
3962 < div title = "When you type, hit enter to add item and start typing again to add another or select from the dropdown" >
40- < CreatableSelect
63+ < CreatableSelect
4164 isMulti
4265 options = { options }
4366 value = { selectedLabels }
4467 onChange = { ( selected ) => {
4568 debouncedHandleInputChange ( selected || [ ] ) ;
4669 } }
70+ onCreateOption = { handleCreateOption }
4771 styles = { {
4872 control : ( baseStyles , state ) => ( {
4973 ...baseStyles ,
0 commit comments