ant-design/components/button/button-group.tsx

57 lines
1.4 KiB
TypeScript
Raw Normal View History

import classNames from 'classnames';
2022-06-22 14:57:09 +08:00
import * as React from 'react';
import { ConfigContext } from '../config-provider';
2022-06-22 14:57:09 +08:00
import type { SizeType } from '../config-provider/SizeContext';
import warning from '../_util/warning';
2016-07-14 13:29:50 +08:00
2016-09-13 15:31:29 +08:00
export interface ButtonGroupProps {
size?: SizeType;
2016-07-14 13:29:50 +08:00
style?: React.CSSProperties;
className?: string;
prefixCls?: string;
children?: React.ReactNode;
2016-07-14 13:29:50 +08:00
}
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:
warning(!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>
);
};
2017-07-18 16:46:19 +08:00
export default ButtonGroup;