Skip to content

Commit bcdab79

Browse files
committed
Pass form prop to input so it works even if the select is ouside the form tag
1 parent 2f94e8d commit bcdab79

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

packages/react-select/src/Select.tsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -2019,11 +2019,17 @@ export default class Select<
20192019
);
20202020
}
20212021
renderFormField() {
2022-
const { delimiter, isDisabled, isMulti, name, required } = this.props;
2022+
const { delimiter, form, isDisabled, isMulti, name, required } = this.props;
20232023
const { selectValue } = this.state;
20242024

20252025
if (required && !this.hasValue() && !isDisabled) {
2026-
return <RequiredInput name={name} onFocus={this.onValueInputFocus} />;
2026+
return (
2027+
<RequiredInput
2028+
form={form}
2029+
name={name}
2030+
onFocus={this.onValueInputFocus}
2031+
/>
2032+
);
20272033
}
20282034

20292035
if (!name || isDisabled) return;
@@ -2039,21 +2045,22 @@ export default class Select<
20392045
selectValue.length > 0 ? (
20402046
selectValue.map((opt, i) => (
20412047
<input
2048+
form={form}
20422049
key={`i-${i}`}
20432050
name={name}
20442051
type="hidden"
20452052
value={this.getOptionValue(opt)}
20462053
/>
20472054
))
20482055
) : (
2049-
<input name={name} type="hidden" value="" />
2056+
<input form={form} name={name} type="hidden" value="" />
20502057
);
20512058

20522059
return <div>{input}</div>;
20532060
}
20542061
} else {
20552062
const value = selectValue[0] ? this.getOptionValue(selectValue[0]) : '';
2056-
return <input name={name} type="hidden" value={value} />;
2063+
return <input form={form} name={name} type="hidden" value={value} />;
20572064
}
20582065
}
20592066

packages/react-select/src/internal/RequiredInput.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { FocusEventHandler, FunctionComponent } from 'react';
33
import { jsx } from '@emotion/react';
44

55
const RequiredInput: FunctionComponent<{
6+
readonly form?: string;
67
readonly name?: string;
78
readonly onFocus: FocusEventHandler<HTMLInputElement>;
8-
}> = ({ name, onFocus }) => (
9+
}> = ({ form, name, onFocus }) => (
910
<input
1011
required
12+
form={form}
1113
name={name}
1214
tabIndex={-1}
1315
aria-hidden="true"

0 commit comments

Comments
 (0)