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

Refactor(select): Select 컴포넌트를 제어 혹은 비제어컴포넌트로 스위칭이 가능하도록 수정 #11

Merged
merged 2 commits into from
Apr 6, 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
24 changes: 15 additions & 9 deletions packages/design-system/src/components/select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { SelectSearchInput } from './SelectSearchInput';
import { SelectOptions } from './SelectOptions';
import { SelectOption } from './SelectOption';
import { forwardRefWithAs } from '@utils';
import { useControllableState } from '@hooks';

type ValueType = string | string[] | undefined | null;
export const SelectActionsContext = createContext<{
setOpen: React.Dispatch<React.SetStateAction<boolean>>;
handleSelectValue: (value: string) => void;
Expand All @@ -23,28 +25,33 @@ SelectActionsContext.displayName = 'SelectActionsContext';

export const SelectDataContext = createContext<{
open: boolean;
selectedValue: null | string | string[];
selectedValue?: ValueType;
} | null>(null);
SelectDataContext.displayName = 'SelectDataContext';

interface SelectRootProps {
value: string | string[];
defaultValue?: ValueType;
value?: ValueType;
multiple?: boolean;
children?: React.ReactNode;
className?: string;
onChange?(value: null | string | string[]): void;
onChange?(value?: ValueType): void;
}
const SelectRoot = ({
value,
onChange,
defaultValue,
value: controlledValue,
onChange: controlledOnChange,
children,
multiple = false,
}: SelectRootProps) => {
const selectRef = useRef<HTMLDivElement>(null);
const [open, setOpen] = useState(false);
const [selectedValue, setSelectedValue] = useState<null | string | string[]>(
value,
);
let [selectedValue = multiple ? [] : undefined, setSelectedValue] =
useControllableState<ValueType>(
controlledValue,
controlledOnChange,
defaultValue,
);

const handleSelectValue = useCallback(
(item: string) => {
Expand All @@ -60,7 +67,6 @@ const SelectRoot = ({
}

setSelectedValue(selectedList);
onChange?.(selectedList);
},
[multiple, selectedValue],
);
Expand Down
4 changes: 4 additions & 0 deletions packages/design-system/src/hooks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ export type { ContextData } from './useContextData';

export { useContextAction } from './useContextAction';
export type { ContextAction } from './useContextAction';

export { useEvent } from './useEvent';
export { useLatestValue } from './useLatestValue';
export { useControllableState } from './useControllableState';
49 changes: 49 additions & 0 deletions packages/design-system/src/hooks/useControllableState.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { useRef, useState } from 'react';
import { useEvent } from './useEvent';

export const useControllableState = <T,>(
controlledValue: T | undefined,
onChange?: (value: T) => void,
defaultValue?: T,
) => {
let [internalValue, setInternalValue] = useState(defaultValue);

let isControlled = controlledValue !== undefined;
let wasControlled = useRef(isControlled);
let didWarnOnUncontrolledToControlled = useRef(false);
let didWarnOnControlledToUncontrolled = useRef(false);

if (
isControlled &&
!wasControlled.current &&
!didWarnOnUncontrolledToControlled.current
) {
didWarnOnUncontrolledToControlled.current = true;
wasControlled.current = isControlled;
console.error(
'A component is changing from uncontrolled to controlled. This may be caused by the value changing from undefined to a defined value, which should not happen.',
);
} else if (
!isControlled &&
wasControlled.current &&
!didWarnOnControlledToUncontrolled.current
) {
didWarnOnControlledToUncontrolled.current = true;
wasControlled.current = isControlled;
console.error(
'A component is changing from controlled to uncontrolled. This may be caused by the value changing from a defined value to undefined, which should not happen.',
);
}

return [
(isControlled ? controlledValue : internalValue)!,
useEvent((value: T) => {
if (isControlled) {
return onChange?.(value);
} else {
setInternalValue(value);
return onChange?.(value);
}
}),
] as const;
};
13 changes: 13 additions & 0 deletions packages/design-system/src/hooks/useEvent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import { useLatestValue } from './useLatestValue';

export let useEvent =
// TODO: Add React.useEvent ?? once the useEvent hook is available
function useEvent<
F extends (...args: any[]) => any,

Check warning on line 7 in packages/design-system/src/hooks/useEvent.tsx

View workflow job for this annotation

GitHub Actions / setup-and-lint

Unexpected any. Specify a different type
P extends any[] = Parameters<F>,

Check warning on line 8 in packages/design-system/src/hooks/useEvent.tsx

View workflow job for this annotation

GitHub Actions / setup-and-lint

Unexpected any. Specify a different type
R = ReturnType<F>,
>(fn: (...args: P) => R) {
let cache = useLatestValue(fn);
return React.useCallback((...args: P) => cache.current(...args), [cache]);
};
11 changes: 11 additions & 0 deletions packages/design-system/src/hooks/useLatestValue.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useEffect, useRef } from 'react';

export const useLatestValue = <T,>(value: T) => {
let cache = useRef(value);

useEffect(() => {
cache.current = value;
}, [value]);

return cache;
};