Skip to content

Commit

Permalink
fix: SelectOnChange API and types (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmartel authored Jan 20, 2024
1 parent f13e023 commit ab13fad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const StatefulMultiSelect: FC<
<MultiSelect
value={value}
options={options}
onChange={onChange!}
onChange={onChange}
{...props}
/>
)
Expand Down
20 changes: 10 additions & 10 deletions src/use-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1212,17 +1213,16 @@ export function useMultiSelect(
const onChangeRef = useRef<typeof props.onChange>()
onChangeRef.current = props.onChange;

const setNextValue = useCallback((value: string|number|(string|number)[]) => {
const setNextValue = useCallback<SelectOnChange>((value, change) => {
setValue(value as any)
onChangeRef.current?.(value)
console.log("setNextValue:", value)
onChangeRef.current?.(value, change)
}, [])

const onChange = useCallback<SelectOnChange>(
(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)
Expand All @@ -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)
Expand All @@ -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]
Expand Down

0 comments on commit ab13fad

Please sign in to comment.