@@ -21,6 +21,12 @@ type SelectOptionValue = LocatorSelectOptionAction['values'][number]
2121
2222type MatchType = 'value' | 'label' | 'index'
2323
24+ function isNonEmptyValue ( entry : SelectOptionValue ) : boolean {
25+ if ( entry . label !== undefined ) return entry . label !== ''
26+ if ( entry . index !== undefined ) return true
27+ return ( entry . value ?? '' ) !== ''
28+ }
29+
2430function getMatchType ( entry : SelectOptionValue ) : MatchType {
2531 if ( entry . label !== undefined ) return 'label'
2632 if ( entry . index !== undefined ) return 'index'
@@ -54,6 +60,14 @@ export function SelectOptionValuesForm({
5460 onChange,
5561} : SelectOptionValuesFormProps ) {
5662 const [ isPopoverOpen , setIsPopoverOpen ] = useState ( false )
63+ const [ isTouched , setIsTouched ] = useState ( false )
64+
65+ const handlePopoverOpenChange = ( open : boolean ) => {
66+ setIsPopoverOpen ( open )
67+ if ( ! open ) {
68+ setIsTouched ( true )
69+ }
70+ }
5771
5872 const handleChangeType = ( index : number , type : MatchType ) => {
5973 const current = values [ index ]
@@ -81,14 +95,24 @@ export function SelectOptionValuesForm({
8195 onChange ( values . filter ( ( _ , i ) => i !== index ) )
8296 }
8397
98+ const nonEmptyValues = values . filter ( isNonEmptyValue )
99+
84100 return (
85- < Popover . Root open = { isPopoverOpen } onOpenChange = { setIsPopoverOpen } >
101+ < Popover . Root open = { isPopoverOpen } onOpenChange = { handlePopoverOpenChange } >
86102 < Popover . Trigger >
87103 < ValuePopoverBadge
88104 displayValue = {
89- values . length === 0 ? '(none)' : < SelectOptions options = { values } />
105+ nonEmptyValues . length === 0 ? (
106+ 'option(s)'
107+ ) : (
108+ < SelectOptions options = { nonEmptyValues } />
109+ )
110+ }
111+ error = {
112+ isTouched && nonEmptyValues . length === 0
113+ ? 'At least one option is required'
114+ : null
90115 }
91- error = { values . length === 0 ? 'At least one option is required' : null }
92116 />
93117 </ Popover . Trigger >
94118 < Popover . Content align = "start" size = "1" width = "360px" >
0 commit comments