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

Fix SelectField hydration mismatch error #859

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions packages/bento-design-system/src/SelectField/BaseSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FieldProps } from "../Field/FieldProps";
import { BentoConfigProvider, useBentoConfig } from "../BentoConfigContext";
import { AriaLabelingProps, DOMProps } from "@react-types/shared";
import * as selectComponents from "./components";
import { useEffect, useRef } from "react";
import { useEffect, useId, useRef } from "react";
import { BaseMultiProps, BaseSelectProps, BaseSingleProps, SelectOption } from "./types";

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

// 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
const generatedId = useId();
const id = fieldProps.id ?? generatedId;

return (
// NOTE(gabro): SelectField has its own config for List, so we override it here using BentoConfigProvider
<BentoConfigProvider value={{ list: dropdownConfig.list }}>
<Select
id={fieldProps.id}
id={id}
instanceId={id}
name={name}
aria-label={fieldProps["aria-label"]}
aria-labelledby={fieldProps["aria-labelledby"]}
Expand Down
3 changes: 2 additions & 1 deletion packages/bento-design-system/src/SelectField/SelectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ declare module "react-select/dist/declarations/src/Select" {
}

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

const validationState = issues ? "invalid" : "valid";
const { labelProps, fieldProps, descriptionProps, errorMessageProps } = useField({
id,
label,
description: assistiveText,
errorMessage: issues,
Expand Down
1 change: 1 addition & 0 deletions packages/bento-design-system/src/SelectField/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type BaseMultiProps = {
);

export type BaseSelectProps<A> = {
id?: string;
menuSize?: ListSize;
placeholder?: LocalizedString;
options: Array<SelectOption<A>>;
Expand Down
Loading