Skip to content

Commit

Permalink
fix: change the fix to render the entire select on mount
Browse files Browse the repository at this point in the history
  • Loading branch information
SiTaggart committed Oct 13, 2023
1 parent c74c43b commit 93f167c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .changeset/thirty-forks-poke.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"@twilio-paste/core": patch
---

[Select] fix the behaviour of `defaultValue` by removing the rehydration useEffect trick to stop a SSR mismatch. By only rendering the child options on mount via an effect it prevents `defaultValue` from selecting the default value. We will look for other ways of dealing with rehydration mismatches
[Select] fix the behaviour of `defaultValue` by tweaking the rehydration useEffect trick, to stop a SSR mismatch. By only rendering the child options on mount via an effect it prevents `defaultValue` from selecting the default value. We moved to rendering the entire Select element on mount to prevent SSR mismatch, and rendering a placeholder select to prevent layout shift.
36 changes: 23 additions & 13 deletions packages/paste-core/components/select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ const Select = React.forwardRef<HTMLSelectElement, SelectProps>(
},
ref,
) => {
const [showOptions, setShowOptions] = React.useState(false);
React.useEffect(() => {
setShowOptions(true);
}, []);

return (
<InputBox
disabled={disabled}
Expand All @@ -92,19 +97,24 @@ const Select = React.forwardRef<HTMLSelectElement, SelectProps>(
variant={variant}
>
<Box display="flex" width="100%" position="relative">
<SelectElement
aria-invalid={hasError}
data-not-selectize="true"
disabled={disabled}
ref={ref}
element={`${element}_ELEMENT`}
{...props}
multiple={multiple}
size={multiple ? size : 0}
variant={variant}
>
{children}
</SelectElement>
{showOptions ? (
<SelectElement
aria-invalid={hasError}
data-not-selectize="true"
disabled={disabled}
ref={ref}
element={`${element}_ELEMENT`}
{...props}
multiple={multiple}
size={multiple ? size : 0}
variant={variant}
key={props.id}
>
{children}
</SelectElement>
) : (
<SelectElement>{}</SelectElement>
)}
{!multiple && (
<InputChevronWrapper element={`${element}_CHEVRON_WRAPPER`}>
<ChevronDownIcon
Expand Down

0 comments on commit 93f167c

Please sign in to comment.