Skip to content

Commit 385e076

Browse files
authored
Merge pull request #859 from buildo/select-ssr
2 parents 9455f6d + c7e1b32 commit 385e076

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

Diff for: packages/bento-design-system/src/SelectField/BaseSelect.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FieldProps } from "../Field/FieldProps";
44
import { BentoConfigProvider, useBentoConfig } from "../BentoConfigContext";
55
import { AriaLabelingProps, DOMProps } from "@react-types/shared";
66
import * as selectComponents from "./components";
7-
import { useEffect, useRef } from "react";
7+
import { useEffect, useId, useRef } from "react";
88
import { BaseMultiProps, BaseSelectProps, BaseSingleProps, SelectOption } from "./types";
99

1010
type MultiProps<A> = BaseMultiProps &
@@ -58,11 +58,16 @@ export function BaseSelect<A>(props: Props<A>) {
5858
clearable = true,
5959
} = props;
6060

61+
// NOTE(gabro): we want to make sure we have a stable ID across SSR rendering, to overcome this issue with react-select https://github.com/JedWatson/react-select/issues/2629
62+
const generatedId = useId();
63+
const id = fieldProps.id ?? generatedId;
64+
6165
return (
6266
// NOTE(gabro): SelectField has its own config for List, so we override it here using BentoConfigProvider
6367
<BentoConfigProvider value={{ list: dropdownConfig.list }}>
6468
<Select
65-
id={fieldProps.id}
69+
id={id}
70+
instanceId={id}
6671
name={name}
6772
aria-label={fieldProps["aria-label"]}
6873
aria-labelledby={fieldProps["aria-labelledby"]}

Diff for: packages/bento-design-system/src/SelectField/SelectField.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ declare module "react-select/dist/declarations/src/Select" {
2828
}
2929

3030
export function SelectField<A>(props: Props<A>) {
31-
const { label, hint, hintPlacement, assistiveText, issues, disabled } = props;
31+
const { id, label, hint, hintPlacement, assistiveText, issues, disabled } = props;
3232

3333
const validationState = issues ? "invalid" : "valid";
3434
const { labelProps, fieldProps, descriptionProps, errorMessageProps } = useField({
35+
id,
3536
label,
3637
description: assistiveText,
3738
errorMessage: issues,

Diff for: packages/bento-design-system/src/SelectField/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type BaseMultiProps = {
3232
);
3333

3434
export type BaseSelectProps<A> = {
35+
id?: string;
3536
menuSize?: ListSize;
3637
placeholder?: LocalizedString;
3738
options: Array<SelectOption<A>>;

0 commit comments

Comments
 (0)