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

[Select] Support form attribute #2447

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/react/select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ interface SelectProps {
autoComplete?: string;
disabled?: boolean;
required?: boolean;
form?: string;
}

const Select: React.FC<SelectProps> = (props: ScopedProps<SelectProps>) => {
Expand All @@ -108,6 +109,7 @@ const Select: React.FC<SelectProps> = (props: ScopedProps<SelectProps>) => {
autoComplete,
disabled,
required,
form,
} = props;
const popperScope = usePopperScope(__scopeSelect);
const [trigger, setTrigger] = React.useState<SelectTriggerElement | null>(null);
Expand All @@ -127,7 +129,7 @@ const Select: React.FC<SelectProps> = (props: ScopedProps<SelectProps>) => {
const triggerPointerDownPosRef = React.useRef<{ x: number; y: number } | null>(null);

// We set this to true by default so that events bubble to forms without JS (SSR)
const isFormControl = trigger ? Boolean(trigger.closest('form')) : true;
const isFormControl = trigger ? (Boolean(trigger.closest('form')) || form) : true;
const [nativeOptionsSet, setNativeOptionsSet] = React.useState(new Set<NativeOption>());

// The native `select` only associates the correct default value if the corresponding
Expand Down Expand Up @@ -189,6 +191,7 @@ const Select: React.FC<SelectProps> = (props: ScopedProps<SelectProps>) => {
// enable form autofill
onChange={(event) => setValue(event.target.value)}
disabled={disabled}
form={form}
>
{value === undefined ? <option value="" /> : null}
{Array.from(nativeOptionsSet)}
Expand Down
Loading