Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass form prop to input so it works even if the select is ouside the form tag #5782

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/serious-jokes-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-select': patch
---

Pass the form prop to the created input in renderFormField
17 changes: 12 additions & 5 deletions packages/react-select/src/Select.tsx
Original file line number Diff line number Diff line change
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 @@ -2139,27 +2145,28 @@ export default class Select<
const value = selectValue
.map((opt) => this.getOptionValue(opt))
.join(delimiter);
return <input name={name} type="hidden" value={value} />;
return <input form={form} name={name} type="hidden" value={value} />;
} else {
const input =
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