mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-28 21:19:37 +08:00
f8e7cba353
* chore: refactor grp with context * test: Update snapshot * chore: clean up * test: fix test case * chore: clean up * test: fix test
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import * as React from 'react';
|
|
import classNames from 'classnames';
|
|
import { SizeType } from '../config-provider/SizeContext';
|
|
import { ConfigContext } from '../config-provider';
|
|
import devWarning from '../_util/devWarning';
|
|
|
|
export interface ButtonGroupProps {
|
|
size?: SizeType;
|
|
style?: React.CSSProperties;
|
|
className?: string;
|
|
prefixCls?: string;
|
|
children?: React.ReactNode;
|
|
}
|
|
|
|
export const GroupSizeContext = React.createContext<SizeType | undefined>(undefined);
|
|
|
|
const ButtonGroup: React.FC<ButtonGroupProps> = props => {
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const { prefixCls: customizePrefixCls, size, className, ...others } = props;
|
|
const prefixCls = getPrefixCls('btn-group', customizePrefixCls);
|
|
|
|
// large => lg
|
|
// small => sm
|
|
let sizeCls = '';
|
|
switch (size) {
|
|
case 'large':
|
|
sizeCls = 'lg';
|
|
break;
|
|
case 'small':
|
|
sizeCls = 'sm';
|
|
break;
|
|
case 'middle':
|
|
case undefined:
|
|
break;
|
|
default:
|
|
devWarning(!size, 'Button.Group', 'Invalid prop `size`.');
|
|
}
|
|
|
|
const classes = classNames(
|
|
prefixCls,
|
|
{
|
|
[`${prefixCls}-${sizeCls}`]: sizeCls,
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
},
|
|
className,
|
|
);
|
|
|
|
return (
|
|
<GroupSizeContext.Provider value={size}>
|
|
<div {...others} className={classes} />
|
|
</GroupSizeContext.Provider>
|
|
);
|
|
};
|
|
|
|
export default ButtonGroup;
|