import type { Dispatch, SetStateAction } from "react"; /** * @internal * @remarks \@since 2.8.5 */ declare type Initializer = readonly V[] | (() => readonly V[]); /** * The change handler for indeterminate checkboxes. * * @param values - The current list of checked values. * @typeParam V - The values allowed for the list of checkboxes. * @internal * @remarks \@since 2.8.5 */ declare type OnChange = (values: readonly V[]) => void; /** * @remarks \@since 2.8.5 * @typeParam V - The values allowed for the list of checkboxes. */ export interface IndeterminateCheckedHookOptions { /** * Enabling this option will update the returned props to rename `onChange` to * `onCheckedChange` to work with the {@link MenuItemCheckbox} component. * * @defaultValue `false` */ menu?: boolean; /** * This is the `useState` initializer that can be used if some checkboxes should * be checked by default. */ onChange?: OnChange; /** * The change handler for indeterminate checkboxes. * * @param values - The current list of checked values. */ defaultCheckedValues?: Initializer; } /** @remarks \@since 2.8.5 */ export interface BaseProvidedIndeterminateCheckboxProps { /** * Note: This will only be provided when the {@link indeterminate} prop is * `true`. */ "aria-checked"?: "mixed"; /** * Boolean if the root checkbox is currently checked. */ checked: boolean; /** * This will be set to `true` when at least one checkbox has been checked but * not every checkbox to enable the {@link CheckboxProps.indeterminate} state. */ indeterminate: boolean; } /** * @remarks \@since 2.8.5 * @internal */ export interface ProvidedIndeterminateCheckboxProps extends BaseProvidedIndeterminateCheckboxProps { onChange(): void; } /** * @remarks \@since 2.8.5 * @internal */ export interface ProvidedIndeterminateMenuItemCheckboxProps extends BaseProvidedIndeterminateCheckboxProps { onCheckedChange(): void; } /** * @remarks \@since 2.8.5 * @internal */ interface ProvidedCombinedIndeterminateProps extends BaseProvidedIndeterminateCheckboxProps { onChange?(): void; onCheckedChange?(): void; } /** * @remarks \@since 2.8.5 * @typeParam V - The values allowed for the list of checkboxes. */ export interface BaseProvidedIndeterminateControlledCheckboxProps { /** * One of the values provided to the {@link useIndeterminateChecked} hook. */ value: V; /** * Boolean if the current checkbox is checked. */ checked: boolean; } /** * @remarks \@since 2.8.5 * @typeParam V - The values allowed for the list of checkboxes. * @internal */ export interface ProvidedIndeterminateControlledCheckboxProps extends BaseProvidedIndeterminateControlledCheckboxProps { onChange(): void; } /** * @remarks \@since 2.8.5 * @typeParam V - The values allowed for the list of checkboxes. * @internal */ export interface ProvidedIndeterminateControlledMenuItemCheckboxProps extends BaseProvidedIndeterminateControlledCheckboxProps { onCheckedChange(): void; } /** * @remarks \@since 2.8.5 * @typeParam V - The values allowed for the list of checkboxes. * @internal */ interface ProvidedCombinedIndeterminateControlledProps extends BaseProvidedIndeterminateControlledCheckboxProps { onChange?(): void; onCheckedChange?(): void; } /** * @remarks \@since 2.8.5 * @typeParam V - The values allowed for the list of checkboxes. */ export interface BaseIndeterminateCheckedHookReturnValue { /** * A list of all the values that are currently checked. */ checkedValues: readonly V[]; /** * A function to manually override the {@link checkedValues} if the default * hook's implementation does not work for your use-case. */ setCheckedValues: Dispatch>; } /** * @remarks \@since 2.8.5 * @typeParam V - The values allowed for the list of checkboxes. * @internal */ interface OnChangeReturnValue extends BaseIndeterminateCheckedHookReturnValue { rootProps: ProvidedIndeterminateCheckboxProps; getProps(value: V): ProvidedIndeterminateControlledCheckboxProps; } /** * @remarks \@since 2.8.5 * @typeParam V - The values allowed for the list of checkboxes. * @internal */ interface OnCheckedChangeReturnValue extends BaseIndeterminateCheckedHookReturnValue { rootProps: ProvidedIndeterminateMenuItemCheckboxProps; getProps(value: V): ProvidedIndeterminateControlledMenuItemCheckboxProps; } /** * @remarks \@since 2.8.5 * @typeParam V - The values allowed for the list of checkboxes. * @internal */ export interface CombinedIndeterminateCheckedHookReturnValue extends BaseIndeterminateCheckedHookReturnValue { rootProps: ProvidedCombinedIndeterminateProps; getProps(value: V): ProvidedCombinedIndeterminateControlledProps; } /** * This hook allows you to toggle the state of multiple checkboxes in a single * place along with an indeterminate checkbox that can check/uncheck all * checkboxes at once. * * @example * Simple value list with labels lookup: * ```tsx * 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 ( * <> * * {values.map((value, i) => ( * * ))} * * ); * ``` * * @example * Fetch Data From Server and check first result * ```tsx * interface ServerFetchedData { * id: Guid; * name: string; * } * * * const [data, setData] = useState([]); * const { getProps, rootProps, setCheckedValues } = useIndeterminateChecked( * data.map(({ id }) => id), * ); * * useEffect(() => { * let cancelled = false; * (async function load() { * const response = await fetch("/my-api"); * const json = await response.json(); * if (!cancelled) { * // pretend validation and sanity checks * setData(json); * setCheckedValues(json[0].id); * } * })(); * return () => { * cancelled = true; * }; * }, []); * * return ( * <> * * {data.map(({ id, name }, i) => ( * * ))} * * ); * ``` * * @example * With MenuItemCheckbox * ```tsx * 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, { * menu: true, * }); * * return ( * * * Toggle All * * {values.map((value, i) => ( * * {LABELS[value]} * * ))} * * ); * ``` * * @typeParam V - The allowed values for the checkboxes * @param values - The allowed values for the checkboxes which is used to * control the checked states. * @param defaultOrOptions - The {@link IndeterminateCheckedHookOptions} or a * `useState` initializer callback/default value for backwards compatibility * @param optionalOnChange - This is really just for backwards compatibility and * should not be used. Use {@link IndeterminateCheckedHookOptions.onChange} * instead. * @returns an object containing the `rootProps` to pass to the indeterminate * checkbox, a `getProps` function to provide the controlled behavior for the * additional `values` in the checkbox list, a list of `checkedValues`, and a * `setCheckedValues` function to manually override the state if needed. */ export declare function useIndeterminateChecked(values: readonly V[], options?: IndeterminateCheckedHookOptions & { menu?: false; }): OnChangeReturnValue; export declare function useIndeterminateChecked(values: readonly V[], options: IndeterminateCheckedHookOptions & { menu: true; }): OnCheckedChangeReturnValue; export {};