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: export useSelect hook's parameter type #742

Merged
merged 1 commit into from
Jul 8, 2024
Merged
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
20 changes: 11 additions & 9 deletions src/components/hooks/useSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ type FilterType<SourceType, Type> = Pick<
}[keyof SourceType]
>;

interface ItemOptions<T> {
interface UseSelectCommonOptions<T> {
renderItem?: (item: T) => ReactNode;
defaultSelectedItem?: T;
}

interface BaseOptions<T> extends ItemOptions<T> {
interface UseSelectBaseOptions<T> extends UseSelectCommonOptions<T> {
itemTextKey: keyof FilterType<T, string>;
}

interface RenderOptions<T> extends ItemOptions<T> {
interface UseSelectRenderOptions<T> extends UseSelectCommonOptions<T> {
getItemText: (item: T) => string;
}

type SelectOptions<T> = BaseOptions<T> | RenderOptions<T>;
export type UseSelectOptions<T> =
| UseSelectBaseOptions<T>
| UseSelectRenderOptions<T>;

function isAccessLabelByKey<T>(
options: SelectOptions<T>,
): options is BaseOptions<T> {
options: UseSelectOptions<T>,
): options is UseSelectBaseOptions<T> {
return 'itemTextKey' in options;
}

function getLabel<T>(item: T, options: SelectOptions<T>) {
function getLabel<T>(item: T, options: UseSelectOptions<T>) {
const isAccessLByLabelKey = isAccessLabelByKey(options);

if (!item || (isAccessLByLabelKey && !options.itemTextKey)) {
Expand All @@ -44,7 +46,7 @@ function getLabel<T>(item: T, options: SelectOptions<T>) {
return options.getItemText(item);
}

export function useSelect<T>(options: SelectOptions<T>) {
export function useSelect<T>(options: UseSelectOptions<T>) {
const { defaultSelectedItem = null } = options;

const [value, setValue] = useState<T | null>(defaultSelectedItem);
Expand Down Expand Up @@ -86,7 +88,7 @@ export function useSelect<T>(options: SelectOptions<T>) {
};
}

function getItemRenderer<T>(value: T, options: SelectOptions<T>) {
function getItemRenderer<T>(value: T, options: UseSelectOptions<T>) {
const selectedLabel = getLabel(value, options);

const render: ItemRenderer<T> = (
Expand Down
Loading