From ab13fad3639efa7cc3c607eaa1bef3bdd80cb7ae Mon Sep 17 00:00:00 2001 From: bmartel Date: Sat, 20 Jan 2024 14:09:55 -0600 Subject: [PATCH] fix: SelectOnChange API and types (#36) --- example/src/App.tsx | 2 +- src/use-select.ts | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index 104eb29..b4e9483 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -93,7 +93,7 @@ const StatefulMultiSelect: FC< ) diff --git a/src/use-select.ts b/src/use-select.ts index d3bacb6..1205260 100644 --- a/src/use-select.ts +++ b/src/use-select.ts @@ -283,8 +283,9 @@ export interface UseMultiSelectProps { export interface UseMultiSelectReturn { value?: string | string[] options: Option[] - onChange?: SelectOnChange + onChange: SelectOnChange } + export interface UseSelectProps extends UsePopperProps { onChange: SelectOnChange single?: boolean @@ -1212,17 +1213,16 @@ export function useMultiSelect( const onChangeRef = useRef() onChangeRef.current = props.onChange; - const setNextValue = useCallback((value: string|number|(string|number)[]) => { + const setNextValue = useCallback((value, change) => { setValue(value as any) - onChangeRef.current?.(value) - console.log("setNextValue:", value) + onChangeRef.current?.(value, change) }, []) const onChange = useCallback( (next, change) => { switch (change?.action) { case ChangeActions.SingleCreate: - setNextValue(next as string) + setNextValue(next as string, change) setOptions((o) => { const opt = getOption(next as any) return o.some((_o) => getOption(_o).value === opt.value) @@ -1232,15 +1232,15 @@ export function useMultiSelect( break case ChangeActions.SingleClear: case ChangeActions.SingleRemove: - setNextValue(next as string) + setNextValue(next as string, change) break case ChangeActions.SingleSelect: - setNextValue(next as string) + setNextValue(next as string, change) break case ChangeActions.MultiCreate: const nextValue = next as string[] const created = next[nextValue.length - 1] - setNextValue(nextValue) + setNextValue(nextValue, change) setOptions((o) => { const opt = getOption(created as any) return o.some((_o) => getOption(_o).value === opt.value) @@ -1250,11 +1250,11 @@ export function useMultiSelect( break case ChangeActions.MultiClear: case ChangeActions.MultiRemove: - setNextValue(next as string[]) + setNextValue(next as string[], change) break case ChangeActions.MultiSelect: default: - setNextValue(next as string[]) + setNextValue(next as string[], change) } }, [setValue, setOptions, getOption]