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 Feb 2, 2024
1 parent 06e3488 commit a177408
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-jokes-beam.md
@@ -0,0 +1,5 @@
---
'react-select': patch
---

Pass the form prop to the created input in renderFormField
15 changes: 11 additions & 4 deletions packages/react-select/src/Select.tsx
Expand Up @@ -2125,11 +2125,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 @@ -2145,21 +2151,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
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 a177408

Please sign in to comment.