Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(form): useIndeterminateChecked correctly uses readonly prefix
  • Loading branch information
mlaursen committed Jul 3, 2021
1 parent 6ed2b65 commit 7f69a71
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/form/src/useIndeterminateChecked.ts
Expand Up @@ -7,7 +7,7 @@ import { Dispatch, SetStateAction, useState } from "react";
export interface ProvidedIndeterminateCheckboxProps {
"aria-checked"?: "mixed";
checked: boolean;
onChange: () => void;
onChange(): void;
indeterminate: boolean;
}

Expand All @@ -20,7 +20,7 @@ export interface ProvidedIndeterminateControlledCheckboxProps<
> {
value: T;
checked: boolean;
onChange: () => void;
onChange(): void;
}

/**
Expand All @@ -34,8 +34,8 @@ export type GetIndeterminateControlledCheckboxProps<T extends string> = (
export interface IndeterminateCheckedReturnValue<T extends string> {
getProps: GetIndeterminateControlledCheckboxProps<T>;
rootProps: ProvidedIndeterminateCheckboxProps;
checkedValues: T[];
setCheckedValues: Dispatch<SetStateAction<T[]>>;
checkedValues: readonly T[];
setCheckedValues: Dispatch<SetStateAction<readonly T[]>>;
}

/**
Expand All @@ -48,8 +48,8 @@ export interface IndeterminateCheckedReturnValue<T extends string> {
* #### Simple value list with labels lookup:
*
* ```tsx
* const values = ["a", "b", "c", "d"];
* const LABELS = { a: "Label 1", b: "Label 2", c: "Label 3", d: "Label 4" };
* const values = ["a", "b", "c", "d"] as const;
* const LABELS = { a: "Label 1", b: "Label 2", c: "Label 3", d: "Label 4" } as const;
* const { getProps, rootProps } = useIndeterminateChecked(values);
*
* return (
Expand Down Expand Up @@ -127,15 +127,16 @@ export interface IndeterminateCheckedReturnValue<T extends string> {
* the list of values can be changed from external sources as well.
*/
export function useIndeterminateChecked<T extends string>(
values: T[],
defaultCheckedValues: T[] | (() => T[]) = [],
onChange?: (checkedValues: T[]) => void
values: readonly T[],
defaultCheckedValues: readonly T[] | (() => readonly T[]) = [],
onChange?: (checkedValues: readonly T[]) => void
): IndeterminateCheckedReturnValue<T> {
const [checkedValues, setCheckedValues] = useState<T[]>(defaultCheckedValues);
const [checkedValues, setCheckedValues] =
useState<readonly T[]>(defaultCheckedValues);
const checked = checkedValues.length > 0;
const indeterminate = checked && checkedValues.length < values.length;

const updateCheckedValues = (values: T[]): void => {
const updateCheckedValues = (values: readonly T[]): void => {
if (onChange) {
onChange(values);
}
Expand Down

0 comments on commit 7f69a71

Please sign in to comment.