Skip to content

Commit

Permalink
Pass form prop to input so it works even if the select is ouside the …
Browse files Browse the repository at this point in the history
…form tag
  • Loading branch information
zvin committed Oct 19, 2023
1 parent 2f94e8d commit bcdab79
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 11 additions & 4 deletions packages/react-select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2019,11 +2019,17 @@ export default class Select<
);
}
renderFormField() {
const { delimiter, isDisabled, isMulti, name, required } = this.props;
const { delimiter, form, isDisabled, isMulti, name, required } = this.props;
const { selectValue } = this.state;

if (required && !this.hasValue() && !isDisabled) {
return <RequiredInput name={name} onFocus={this.onValueInputFocus} />;
return (
<RequiredInput
form={form}
name={name}
onFocus={this.onValueInputFocus}
/>
);
}

if (!name || isDisabled) return;
Expand All @@ -2039,21 +2045,22 @@ export default class Select<
selectValue.length > 0 ? (
selectValue.map((opt, i) => (
<input
form={form}
key={`i-${i}`}
name={name}
type="hidden"
value={this.getOptionValue(opt)}
/>
))
) : (
<input name={name} type="hidden" value="" />
<input form={form} name={name} type="hidden" value="" />
);

return <div>{input}</div>;
}
} else {
const value = selectValue[0] ? this.getOptionValue(selectValue[0]) : '';
return <input name={name} type="hidden" value={value} />;
return <input form={form} name={name} type="hidden" value={value} />;
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/react-select/src/internal/RequiredInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { FocusEventHandler, FunctionComponent } from 'react';
import { jsx } from '@emotion/react';

const RequiredInput: FunctionComponent<{
readonly form?: string;
readonly name?: string;
readonly onFocus: FocusEventHandler<HTMLInputElement>;
}> = ({ name, onFocus }) => (
}> = ({ form, name, onFocus }) => (
<input
required
form={form}
name={name}
tabIndex={-1}
aria-hidden="true"
Expand Down

0 comments on commit bcdab79

Please sign in to comment.