Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove validation, show helper text, and only expand on focus when el…
…ements are present
  • Loading branch information
elazzabi committed Aug 12, 2025
commit 306d98c2f3587ede58b42634319819123d5f9aed
47 changes: 20 additions & 27 deletions packages/dataviews/src/dataform-controls/array.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default function ArrayControl< Item >( {
}: DataFormControlProps< Item > ) {
const { id, label, placeholder, elements } = field;
const value = field.getValue( { item: data } );
const findSuggestionByValue = useCallback(

const findElementByValue = useCallback(
( suggestionValue: string ) => {
return elements?.find(
( suggestion ) => suggestion.value === suggestionValue
Expand All @@ -26,7 +27,7 @@ export default function ArrayControl< Item >( {
[ elements ]
);

const findSuggestionByLabel = useCallback(
const findElementByLabel = useCallback(
( suggestionLabel: string ) => {
return elements?.find(
( suggestion ) => suggestion.label === suggestionLabel
Expand All @@ -39,38 +40,32 @@ export default function ArrayControl< Item >( {
const arrayValue = useMemo(
() =>
Array.isArray( value )
? value
.map( ( v ) => findSuggestionByValue( v )?.label )
.filter(
( item ): item is string => item !== undefined
)
? value.map( ( token ) => {
const tokenLabel = findElementByValue( token )?.label;
return tokenLabel || token;
} )
: [],
[ value, findSuggestionByValue ]
[ value, findElementByValue ]
);

const onChangeControl = useCallback(
( tokens: ( string | { value: string } )[] ) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to understand when the returned tokens can be an array of objects instead of strings. Unless I'm missing some context, I can't reproduce; every individual token is always a string. Can you confirm this? If so, can we simplify this to only handle strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is because the onChange prop from FormTokenField expects that. I haven't been able to reproduce this too, as it's always strings, but changing that will raise a TypeScript error 😞

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I missed that. It's all good, thanks for the context.

// Convert TokenItem objects to strings
const stringTokens = tokens.map( ( token ) =>
typeof token === 'string'
? findSuggestionByLabel( token )?.value
: token.value
);
onChange( {
[ id ]: stringTokens,
const stringTokens = tokens.map( ( token ) => {
if ( typeof token !== 'string' ) {
return token.value;
}

const tokenByLabel = findElementByLabel( token );

return tokenByLabel?.value || token;
} );
},
[ id, onChange, findSuggestionByLabel ]
);

// Custom validation function for FormTokenField
const validateInput = useCallback(
( token: string ) => {
return !! elements?.some( ( element ) => {
return element.label === token;
onChange( {
[ id ]: stringTokens,
} );
},
[ elements ]
[ id, onChange, findElementByLabel ]
);

return (
Expand All @@ -82,9 +77,7 @@ export default function ArrayControl< Item >( {
suggestions={
elements?.map( ( suggestion ) => suggestion.label ) ?? []
}
__experimentalValidateInput={ validateInput }
__experimentalShowHowTo={ false }
__experimentalExpandOnFocus
__experimentalExpandOnFocus={ elements && elements.length > 0 }
__next40pxDefaultSize
__nextHasNoMarginBottom
/>
Expand Down