Skip to content

Commit

Permalink
fix(react): correctly init options when value (or defaultValue) provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius Osokinas committed Jul 25, 2022
1 parent b89220b commit 26ae14d
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/react/src/components/option-list/options-list-init.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useRef } from 'react';
import { ComboboxProps } from '../combobox/combobox';
import { CustomSelectProps } from '../select/custom';
import { OptionListEvent } from './option-list';
import { OptionListProvider } from './useOptionList';
Expand All @@ -8,19 +9,25 @@ export const OptionListInit = ({
defaultValue,
value,
onInit,
}: Pick<CustomSelectProps, 'children' | 'defaultValue' | 'value'> & {
}: (
| Pick<ComboboxProps, 'children' | 'defaultValue' | 'value'>
| Pick<CustomSelectProps, 'children' | 'defaultValue' | 'value'>
) & {
onInit: (selected: OptionListEvent) => void;
}) => {
const hasInit = useRef(false);
const currentValue = value ?? defaultValue;

return (
<OptionListProvider
value={{
value: value ?? defaultValue,
value: currentValue,
initialize(option, value) {
if (hasInit.current) return;
hasInit.current = true;
onInit({ option, value });
if (!currentValue || value === currentValue) {
hasInit.current = true;
onInit({ option, value });
}
},
select: noop,
}}
Expand Down

0 comments on commit 26ae14d

Please sign in to comment.