MCPcopy Create free account
hub / github.com/adobe/react-spectrum / HiddenSelect

Function HiddenSelect

packages/react-aria/src/select/HiddenSelect.tsx:156–246  ·  view source on GitHub ↗
(
  props: HiddenSelectProps<T, M>
)

Source from the content-addressed store, hash-verified

154 * form autofill, mobile form navigation, and native form submission.
155 */
156export function HiddenSelect<T, M extends SelectionMode = 'single'>(
157 props: HiddenSelectProps<T, M>
158): JSX.Element | null {
159 let {state, triggerRef, label, name, form, isDisabled} = props;
160 let selectRef = useRef(null);
161 let inputRef = useRef(null);
162 let {containerProps, selectProps} = useHiddenSelect(
163 {...props, selectRef: state.collection.size <= 300 ? selectRef : inputRef},
164 state,
165 triggerRef
166 );
167
168 let values: (Key | null)[] = Array.isArray(state.value) ? state.value : [state.value];
169
170 // If used in a <form>, use a hidden input so the value can be submitted to a server.
171 // If the collection isn't too big, use a hidden <select> element for this so that browser
172 // autofill will work. Otherwise, use an <input type="hidden">.
173 if (state.collection.size <= 300) {
174 return (
175 <div {...containerProps} data-testid="hidden-select-container">
176 <label>
177 {label}
178 <select {...selectProps} ref={selectRef}>
179 <option value="" label={'\u00A0'}>
180 {'\u00A0'}
181 </option>
182 {[...state.collection.getKeys()].map(key => {
183 let item = state.collection.getItem(key);
184 if (item && item.type === 'item') {
185 return (
186 <option key={item.key} value={item.key}>
187 {item.textValue}
188 </option>
189 );
190 }
191 })}
192 {/* The collection may be empty during the initial render. */}
193 {/* Rendering options for the current values ensures the select has a value immediately, */}
194 {/* making FormData reads consistent. */}
195 {state.collection.size === 0 &&
196 name &&
197 values.map((value, i) => <option key={i} value={value ?? ''} />)}
198 </select>
199 </label>
200 </div>
201 );
202 } else if (name) {
203 let data = selectData.get(state) || {};
204 let {validationBehavior} = data;
205
206 // Always render at least one hidden input to ensure required form submission.
207 if (values.length === 0) {
208 values = [null];
209 }
210
211 let res = values.map((value, i) => {
212 let inputProps: InputHTMLAttributes<HTMLInputElement> = {
213 type: 'hidden',

Callers

nothing calls this directly

Calls 3

useHiddenSelectFunction · 0.85
getKeysMethod · 0.65
getItemMethod · 0.65

Tested by

no test coverage detected