-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbutton-group.tsx
More file actions
executable file
·82 lines (72 loc) · 2.55 KB
/
Copy pathbutton-group.tsx
File metadata and controls
executable file
·82 lines (72 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React, { useContext } from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import { ButtonColor, ButtonGroupProps, ButtonProps, ButtonShape, ButtonVariant } from './types';
import { SizeType } from '../_utils/props';
import { BUTTON_MARK } from './button';
const hasOwnProp = <T extends object>(props: T, key: keyof T): boolean => props[key] !== undefined;
const ButtonGroup = (props: ButtonGroupProps): React.ReactElement => {
const {
size = 'md',
variant = 'solid',
color = 'default',
disabled = false,
round = false,
shape,
inheritMode = 'fill',
prefixCls: customisedCls,
className,
children,
...otherProps
} = props;
const configContext = useContext(ConfigContext);
const prefixCls = getPrefixCls('btn-group', configContext.prefixCls, customisedCls);
const btnSize = props.size || configContext.componentSize || size;
const resolvedShape = shape || (round ? 'round' : 'default');
const cls = classNames(
prefixCls,
{
[`${prefixCls}_round`]: resolvedShape === 'round',
[`${prefixCls}_variant-${variant}`]: variant,
[`${prefixCls}_color-${color}`]: color,
[`${prefixCls}_${btnSize}`]: btnSize,
},
className
);
return (
<div {...otherProps} className={cls}>
{React.Children.map(children, (child) => {
if (!React.isValidElement<ButtonProps>(child)) {
return child;
}
const childType = child.type as unknown as Record<PropertyKey, unknown>;
if (!childType[BUTTON_MARK]) {
return child;
}
if (inheritMode === 'none') {
return child;
}
const childProps: Partial<ButtonProps> = {};
const applyProp = <K extends keyof ButtonProps>(key: K, value: ButtonProps[K]): void => {
if (inheritMode === 'override' || !hasOwnProp(child.props, key)) {
childProps[key] = value;
}
};
applyProp('variant', variant as ButtonVariant);
applyProp('color', color as ButtonColor);
applyProp('size', btnSize as SizeType);
applyProp('disabled', disabled);
if (
inheritMode === 'override' ||
(!hasOwnProp(child.props, 'shape') && !hasOwnProp(child.props, 'round'))
) {
childProps.shape = resolvedShape as ButtonShape;
}
return React.cloneElement(child, childProps);
})}
</div>
);
};
ButtonGroup.displayName = 'ButtonGroup';
export default ButtonGroup;