2023-10-12 09:43:35 +08:00
|
|
|
import * as React from 'react';
|
2016-12-16 20:23:36 +08:00
|
|
|
import classNames from 'classnames';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2023-10-12 09:43:35 +08:00
|
|
|
|
2022-06-22 14:57:09 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-11-14 16:10:23 +08:00
|
|
|
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { CheckboxChangeEvent } from './Checkbox';
|
|
|
|
import Checkbox from './Checkbox';
|
2024-04-01 15:49:45 +08:00
|
|
|
import type { CheckboxGroupContext } from './GroupContext';
|
2023-06-25 12:18:34 +08:00
|
|
|
import GroupContext from './GroupContext';
|
2022-03-03 14:55:19 +08:00
|
|
|
import useStyle from './style';
|
2017-07-08 17:46:18 +08:00
|
|
|
|
2024-05-27 15:06:38 +08:00
|
|
|
export interface CheckboxOptionType<T = any> {
|
2018-12-06 08:28:31 +08:00
|
|
|
label: React.ReactNode;
|
2023-12-22 17:07:34 +08:00
|
|
|
value: T;
|
2020-02-04 17:34:02 +08:00
|
|
|
style?: React.CSSProperties;
|
2016-07-13 11:14:24 +08:00
|
|
|
disabled?: boolean;
|
2023-06-15 09:46:05 +08:00
|
|
|
title?: string;
|
2023-10-12 09:43:35 +08:00
|
|
|
id?: string;
|
2018-10-23 23:40:40 +08:00
|
|
|
onChange?: (e: CheckboxChangeEvent) => void;
|
2023-11-23 20:27:22 +08:00
|
|
|
required?: boolean;
|
2016-06-30 10:05:29 +08:00
|
|
|
}
|
|
|
|
|
2024-05-27 15:06:38 +08:00
|
|
|
export interface AbstractCheckboxGroupProps<T = any> {
|
2017-04-14 16:20:09 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2023-12-22 17:07:34 +08:00
|
|
|
options?: (CheckboxOptionType<T> | string | number)[];
|
2017-04-14 16:20:09 +08:00
|
|
|
disabled?: boolean;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
}
|
|
|
|
|
2024-05-27 15:06:38 +08:00
|
|
|
export interface CheckboxGroupProps<T = any> extends AbstractCheckboxGroupProps<T> {
|
2019-03-30 10:47:03 +08:00
|
|
|
name?: string;
|
2023-12-22 17:07:34 +08:00
|
|
|
defaultValue?: T[];
|
|
|
|
value?: T[];
|
|
|
|
onChange?: (checkedValue: T[]) => void;
|
2020-10-19 16:42:07 +08:00
|
|
|
children?: React.ReactNode;
|
2016-06-30 10:05:29 +08:00
|
|
|
}
|
|
|
|
|
2024-05-27 15:06:38 +08:00
|
|
|
type InternalCheckboxValueType = string | number | boolean;
|
|
|
|
|
2023-12-22 17:07:34 +08:00
|
|
|
const CheckboxGroup = React.forwardRef(
|
2024-05-27 15:06:38 +08:00
|
|
|
<T extends InternalCheckboxValueType = InternalCheckboxValueType>(
|
2023-12-22 17:07:34 +08:00
|
|
|
props: CheckboxGroupProps<T>,
|
|
|
|
ref: React.ForwardedRef<HTMLDivElement>,
|
|
|
|
) => {
|
|
|
|
const {
|
|
|
|
defaultValue,
|
|
|
|
children,
|
|
|
|
options = [],
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
style,
|
|
|
|
onChange,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
|
|
|
|
const [value, setValue] = React.useState<T[]>(restProps.value || defaultValue || []);
|
|
|
|
const [registeredValues, setRegisteredValues] = React.useState<T[]>([]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if ('value' in restProps) {
|
|
|
|
setValue(restProps.value || []);
|
|
|
|
}
|
|
|
|
}, [restProps.value]);
|
|
|
|
|
|
|
|
const memoOptions = React.useMemo<CheckboxOptionType<T>[]>(
|
|
|
|
() =>
|
2024-04-01 15:49:45 +08:00
|
|
|
options.map<CheckboxOptionType<T>>((option: any) => {
|
2023-12-22 17:07:34 +08:00
|
|
|
if (typeof option === 'string' || typeof option === 'number') {
|
|
|
|
return { label: option, value: option };
|
|
|
|
}
|
|
|
|
return option;
|
2021-02-19 18:26:53 +08:00
|
|
|
}),
|
2023-12-22 17:07:34 +08:00
|
|
|
[options],
|
2021-02-19 18:26:53 +08:00
|
|
|
);
|
2023-12-22 17:07:34 +08:00
|
|
|
|
|
|
|
const cancelValue = (val: T) => {
|
|
|
|
setRegisteredValues((prevValues) => prevValues.filter((v) => v !== val));
|
|
|
|
};
|
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
const registerValue: CheckboxGroupContext<T>['registerValue'] = (val) => {
|
2023-12-22 17:07:34 +08:00
|
|
|
setRegisteredValues((prevValues) => [...prevValues, val]);
|
|
|
|
};
|
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
const toggleOption: CheckboxGroupContext<T>['toggleOption'] = (option) => {
|
2023-12-22 17:07:34 +08:00
|
|
|
const optionIndex = value.indexOf(option.value);
|
|
|
|
const newValue = [...value];
|
|
|
|
if (optionIndex === -1) {
|
|
|
|
newValue.push(option.value);
|
|
|
|
} else {
|
|
|
|
newValue.splice(optionIndex, 1);
|
|
|
|
}
|
|
|
|
if (!('value' in restProps)) {
|
|
|
|
setValue(newValue);
|
|
|
|
}
|
|
|
|
onChange?.(
|
|
|
|
newValue
|
|
|
|
.filter((val) => registeredValues.includes(val))
|
|
|
|
.sort((a, b) => {
|
|
|
|
const indexA = memoOptions.findIndex((opt) => opt.value === a);
|
|
|
|
const indexB = memoOptions.findIndex((opt) => opt.value === b);
|
|
|
|
return indexA - indexB;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('checkbox', customizePrefixCls);
|
|
|
|
const groupPrefixCls = `${prefixCls}-group`;
|
|
|
|
|
|
|
|
const rootCls = useCSSVarCls(prefixCls);
|
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
|
|
|
|
|
|
|
|
const domProps = omit(restProps, ['value', 'disabled']);
|
|
|
|
|
|
|
|
const childrenNode = options.length
|
|
|
|
? memoOptions.map<React.ReactNode>((option) => (
|
|
|
|
<Checkbox
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
key={option.value.toString()}
|
|
|
|
disabled={'disabled' in option ? option.disabled : restProps.disabled}
|
|
|
|
value={option.value}
|
|
|
|
checked={value.includes(option.value)}
|
|
|
|
onChange={option.onChange}
|
|
|
|
className={`${groupPrefixCls}-item`}
|
|
|
|
style={option.style}
|
|
|
|
title={option.title}
|
|
|
|
id={option.id}
|
|
|
|
required={option.required}
|
|
|
|
>
|
|
|
|
{option.label}
|
|
|
|
</Checkbox>
|
|
|
|
))
|
|
|
|
: children;
|
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
const context: CheckboxGroupContext<any> = {
|
2023-12-22 17:07:34 +08:00
|
|
|
toggleOption,
|
|
|
|
value,
|
|
|
|
disabled: restProps.disabled,
|
|
|
|
name: restProps.name,
|
|
|
|
// https://github.com/ant-design/ant-design/issues/16376
|
|
|
|
registerValue,
|
|
|
|
cancelValue,
|
|
|
|
};
|
|
|
|
const classString = classNames(
|
|
|
|
groupPrefixCls,
|
|
|
|
{
|
|
|
|
[`${groupPrefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
cssVarCls,
|
|
|
|
rootCls,
|
|
|
|
hashId,
|
|
|
|
);
|
|
|
|
return wrapCSSVar(
|
|
|
|
<div className={classString} style={style} {...domProps} ref={ref}>
|
|
|
|
<GroupContext.Provider value={context}>{childrenNode}</GroupContext.Provider>
|
|
|
|
</div>,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2020-10-18 23:31:20 +08:00
|
|
|
|
2023-06-25 12:18:34 +08:00
|
|
|
export type { CheckboxGroupContext } from './GroupContext';
|
|
|
|
export { GroupContext };
|
|
|
|
|
2024-05-27 15:06:38 +08:00
|
|
|
export default CheckboxGroup as <T = any>(
|
2023-12-22 17:07:34 +08:00
|
|
|
props: CheckboxGroupProps<T> & React.RefAttributes<HTMLDivElement>,
|
2024-01-14 05:53:36 +08:00
|
|
|
) => React.ReactElement;
|