|
11 | 11 | */ |
12 | 12 |
|
13 | 13 | import {FocusableElement, RefObject} from '@react-types/shared'; |
14 | | -import React, {JSX, ReactNode, useRef} from 'react'; |
| 14 | +import React, {InputHTMLAttributes, JSX, ReactNode, useRef} from 'react'; |
15 | 15 | import {selectData} from './useSelect'; |
16 | 16 | import {SelectState} from '@react-stately/select'; |
17 | 17 | import {useFormReset} from '@react-aria/utils'; |
@@ -142,19 +142,29 @@ export function HiddenSelect<T>(props: HiddenSelectProps<T>): JSX.Element | null |
142 | 142 | ); |
143 | 143 | } else if (name) { |
144 | 144 | let data = selectData.get(state) || {}; |
145 | | - // Use a hidden <input type="text"> rather than <input type="hidden"> |
146 | | - // so that an empty value blocks HTML form submission when the field is required. |
| 145 | + let {validationBehavior} = data; |
| 146 | + |
| 147 | + let inputProps: InputHTMLAttributes<HTMLInputElement> = { |
| 148 | + type: 'hidden', |
| 149 | + autoComplete: selectProps.autoComplete, |
| 150 | + name, |
| 151 | + disabled: isDisabled, |
| 152 | + value: state.selectedKey ?? '' |
| 153 | + }; |
| 154 | + |
| 155 | + if (validationBehavior === "native") { |
| 156 | + // Use a hidden <input type="text"> rather than <input type="hidden"> |
| 157 | + // so that an empty value blocks HTML form submission when the field is required. |
| 158 | + inputProps.type = 'text'; |
| 159 | + inputProps.hidden = true; |
| 160 | + inputProps.required = selectProps.required; |
| 161 | + // Ignore react warning. |
| 162 | + inputProps.onChange = () => {}; |
| 163 | + } |
| 164 | + |
147 | 165 | return ( |
148 | | - <input |
149 | | - type="text" |
150 | | - autoComplete={selectProps.autoComplete} |
151 | | - name={name} |
152 | | - disabled={isDisabled} |
153 | | - value={state.selectedKey ?? ''} |
154 | | - onChange={() => {/** Ignore react warning. */}} |
155 | | - required={data.isRequired} |
156 | | - hidden /> |
157 | | - ); |
| 166 | + <input {...inputProps} /> |
| 167 | + ) |
158 | 168 | } |
159 | 169 |
|
160 | 170 | return null; |
|
0 commit comments