@@ -12,6 +12,9 @@ export const TagInput = forwardRef(function TagInput({
1212 onFocus,
1313 onKeyDown : externalOnKeyDown ,
1414} , ref ) {
15+ // Ensure value is always an array (defensive against corrupted data)
16+ const safeValue = Array . isArray ( value ) ? value : [ ]
17+
1518 const [ inputValue , setInputValue ] = useState ( '' )
1619 const [ isOpen , setIsOpen ] = useState ( false )
1720 const [ selectedIndex , setSelectedIndex ] = useState ( 0 )
@@ -26,14 +29,14 @@ export const TagInput = forwardRef(function TagInput({
2629 const filteredTags = useMemo ( ( ) => {
2730 const input = inputValue . toLowerCase ( ) . trim ( )
2831 return allTags
29- . filter ( ( tag ) => ! value . includes ( tag ) )
32+ . filter ( ( tag ) => ! safeValue . includes ( tag ) )
3033 . filter ( ( tag ) => ! input || tag . toLowerCase ( ) . includes ( input ) )
31- } , [ inputValue , allTags , value ] )
34+ } , [ inputValue , allTags , safeValue ] )
3235
3336 const normalizedInput = inputValue . trim ( ) . toLowerCase ( )
34- const showCreateOption = normalizedInput &&
37+ const showCreateOption = normalizedInput &&
3538 ! allTags . some ( ( tag ) => tag . toLowerCase ( ) === normalizedInput ) &&
36- ! value . includes ( normalizedInput )
39+ ! safeValue . includes ( normalizedInput )
3740
3841 const options = useMemo ( ( ) => {
3942 const items = filteredTags . map ( ( tag ) => ( { type : 'existing' , value : tag } ) )
@@ -57,8 +60,8 @@ export const TagInput = forwardRef(function TagInput({
5760 } , [ selectedIndex , isOpen ] )
5861
5962 const selectTag = ( tag ) => {
60- if ( ! value . includes ( tag ) ) {
61- onChange ( [ ...value , tag ] )
63+ if ( ! safeValue . includes ( tag ) ) {
64+ onChange ( [ ...safeValue , tag ] )
6265 }
6366 setInputValue ( '' )
6467 setSelectedIndex ( 0 )
@@ -108,8 +111,8 @@ export const TagInput = forwardRef(function TagInput({
108111 }
109112 }
110113
111- if ( e . key === 'Backspace' && ! inputValue && value . length > 0 ) {
112- onChange ( value . slice ( 0 , - 1 ) )
114+ if ( e . key === 'Backspace' && ! inputValue && safeValue . length > 0 ) {
115+ onChange ( safeValue . slice ( 0 , - 1 ) )
113116 }
114117
115118 externalOnKeyDown ?. ( e )
@@ -179,7 +182,7 @@ export const TagInput = forwardRef(function TagInput({
179182 </ ul >
180183 ) }
181184
182- { ! isOpen && value . length === 0 && ! inputValue && (
185+ { ! isOpen && safeValue . length === 0 && ! inputValue && (
183186 < p className = "mt-1.5 text-xs text-muted-foreground" >
184187 Press ↓ to see existing tags
185188 </ p >
0 commit comments