2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2022-04-22 17:40:52 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
2022-11-23 20:22:38 +08:00
|
|
|
import { useToken } from '../theme/internal';
|
2022-05-10 15:43:29 +08:00
|
|
|
import warning from '../_util/warning';
|
2016-07-14 13:29:50 +08:00
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export interface ButtonGroupProps {
|
2020-01-03 13:38:16 +08:00
|
|
|
size?: SizeType;
|
2016-07-14 13:29:50 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
className?: string;
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls?: string;
|
2022-04-08 22:55:42 +08:00
|
|
|
children?: React.ReactNode;
|
2016-07-14 13:29:50 +08:00
|
|
|
}
|
|
|
|
|
2022-04-22 17:40:52 +08:00
|
|
|
export const GroupSizeContext = React.createContext<SizeType | undefined>(undefined);
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const ButtonGroup: React.FC<ButtonGroupProps> = (props) => {
|
2022-04-22 17:40:52 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
|
|
|
|
const { prefixCls: customizePrefixCls, size, className, ...others } = props;
|
|
|
|
const prefixCls = getPrefixCls('btn-group', customizePrefixCls);
|
|
|
|
|
2022-04-22 22:36:50 +08:00
|
|
|
const [, , hashId] = useToken();
|
|
|
|
|
2022-04-22 17:40:52 +08:00
|
|
|
let sizeCls = '';
|
2023-01-11 10:32:05 +08:00
|
|
|
|
2022-04-22 17:40:52 +08:00
|
|
|
switch (size) {
|
|
|
|
case 'large':
|
|
|
|
sizeCls = 'lg';
|
|
|
|
break;
|
|
|
|
case 'small':
|
|
|
|
sizeCls = 'sm';
|
|
|
|
break;
|
|
|
|
case 'middle':
|
|
|
|
case undefined:
|
|
|
|
break;
|
|
|
|
default:
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(!size, 'Button.Group', 'Invalid prop `size`.');
|
2022-04-22 17:40:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const classes = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${sizeCls}`]: sizeCls,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
2022-04-22 22:36:50 +08:00
|
|
|
hashId,
|
2022-04-22 17:40:52 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<GroupSizeContext.Provider value={size}>
|
|
|
|
<div {...others} className={classes} />
|
|
|
|
</GroupSizeContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
2017-07-18 16:46:19 +08:00
|
|
|
|
|
|
|
export default ButtonGroup;
|