-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fixes #94
- Loading branch information
1 parent
5df7b99
commit 412f651
Showing
6 changed files
with
114 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,64 @@ | ||
import { UseFieldOptions } from "form-atoms"; | ||
import { useAtomValue } from "jotai"; | ||
import { ChangeEvent, useEffect, useState } from "react"; | ||
import { ChangeEvent, useMemo, useRef } from "react"; | ||
|
||
import { | ||
UseMultiSelectFieldProps as UseCheckboxGroupFieldProps, | ||
type UseMultiSelectFieldProps as UseCheckboxGroupFieldProps, | ||
useFieldProps, | ||
} from "../"; | ||
import { ZodArrayField, ZodFieldValue } from "../../fields"; | ||
|
||
export const useCheckboxGroupFieldProps = < | ||
Option, | ||
Field extends ZodArrayField, | ||
>({ | ||
field, | ||
options, | ||
getValue, | ||
}: UseCheckboxGroupFieldProps<Option, Field>) => { | ||
import type { ZodArrayField, ZodFieldValue } from "../../fields"; | ||
|
||
export const useCheckboxGroupFieldProps = <Option, Field extends ZodArrayField>( | ||
{ field, options, getValue }: UseCheckboxGroupFieldProps<Option, Field>, | ||
fieldOptions?: UseFieldOptions<ZodFieldValue<Field>>, | ||
) => { | ||
const atom = useAtomValue(field); | ||
const fieldValue = useAtomValue(atom.value); | ||
const [value, setValue] = useState<number[]>(() => | ||
fieldValue.map((activeOption) => options.indexOf(activeOption)), | ||
const optionValues = useMemo( | ||
() => options.map(getValue), | ||
[getValue, options], | ||
); | ||
|
||
const prevValue = useRef(fieldValue); | ||
|
||
const activeIndexes = useRef<number[]>( | ||
fieldValue.map((activeOption) => optionValues.indexOf(activeOption)), | ||
); | ||
|
||
if (prevValue.current != fieldValue) { | ||
/** | ||
* The field was set from outside via initialValue, reset action, or set manually. | ||
* Recompute the indexes. | ||
**/ | ||
activeIndexes.current = fieldValue.map((activeOption) => | ||
optionValues.indexOf(activeOption), | ||
); | ||
} | ||
|
||
const getEventValue = (event: ChangeEvent<HTMLInputElement>) => { | ||
const index = parseInt(event.currentTarget.value); | ||
const values = event.currentTarget.checked | ||
? [...value, index] | ||
: value.filter((val) => val != index); | ||
const nextIndexes = event.currentTarget.checked | ||
? [...activeIndexes.current, index] | ||
: activeIndexes.current.filter((val) => val != index); | ||
|
||
setValue(values); | ||
activeIndexes.current = nextIndexes; | ||
|
||
const activeOptions = values | ||
.map((idx) => options[idx]) | ||
.filter(Boolean) as Option[]; | ||
const nextValues = nextIndexes.map((index) => optionValues[index]); | ||
|
||
return activeOptions.map(getValue) as ZodFieldValue<Field>; | ||
}; | ||
/** | ||
* When user change event happened, we set the value. | ||
* On the next render when the fieldValue is updated, we can skip calculating the activeIndexes. | ||
*/ | ||
prevValue.current = nextValues; | ||
|
||
useEffect(() => { | ||
if (fieldValue.length === 0 && value.length !== 0) { | ||
// reset local state, when form was reset | ||
setValue([]); | ||
} | ||
}, [fieldValue]); | ||
return nextValues as ZodFieldValue<Field>; | ||
}; | ||
|
||
const props = useFieldProps<Field, HTMLInputElement>(field, getEventValue); | ||
const props = useFieldProps<Field, HTMLInputElement>( | ||
field, | ||
getEventValue, | ||
fieldOptions, | ||
); | ||
|
||
return { ...props, value }; | ||
return { ...props, value: activeIndexes.current }; | ||
}; |