Skip to content

Commit 0244e7f

Browse files
committed
refactor: Simplify Button Group Style
1 parent 3aedf48 commit 0244e7f

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

components/_util/createContext.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { inject, provide } from 'vue';
2+
3+
function createContext<T>() {
4+
const contextKey = Symbol('contextKey');
5+
const useProvide = (props: T) => {
6+
provide(contextKey, props);
7+
};
8+
const useInject = () => {
9+
return inject(contextKey, undefined as T) || ({} as T);
10+
};
11+
return {
12+
useProvide,
13+
useInject,
14+
};
15+
}
16+
17+
export default createContext;

components/button/button-group.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { computed, defineComponent } from 'vue';
22
import { flattenChildren } from '../_util/props-util';
33
import useConfigInject from '../_util/hooks/useConfigInject';
44

5-
import type { ExtractPropTypes, PropType } from 'vue';
5+
import type { ExtractPropTypes, PropType, ComputedRef } from 'vue';
66
import type { SizeType } from '../config-provider';
7-
import UnreachableException from '../_util/unreachableException';
7+
import devWarning from '../vc-util/devWarning';
8+
import createContext from '../_util/createContext';
89

910
export const buttonGroupProps = () => ({
1011
prefixCls: String,
@@ -14,12 +15,17 @@ export const buttonGroupProps = () => ({
1415
});
1516

1617
export type ButtonGroupProps = Partial<ExtractPropTypes<ReturnType<typeof buttonGroupProps>>>;
17-
18+
export const GroupSizeContext = createContext<{
19+
size: ComputedRef<SizeType>;
20+
}>();
1821
export default defineComponent({
1922
name: 'AButtonGroup',
2023
props: buttonGroupProps(),
2124
setup(props, { slots }) {
2225
const { prefixCls, direction } = useConfigInject('btn-group', props);
26+
GroupSizeContext.useProvide({
27+
size: computed(() => props.size),
28+
});
2329
const classes = computed(() => {
2430
const { size } = props;
2531
// large => lg
@@ -37,7 +43,7 @@ export default defineComponent({
3743
break;
3844
default:
3945
// eslint-disable-next-line no-console
40-
console.warn(new UnreachableException(size).error);
46+
devWarning(!size, 'Button.Group', 'Invalid prop `size`.');
4147
}
4248
return {
4349
[`${prefixCls.value}`]: true,

components/button/button.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ import LoadingIcon from './LoadingIcon';
1818

1919
import type { ButtonType } from './buttonTypes';
2020
import type { VNode, Ref } from 'vue';
21+
import { GroupSizeContext } from './button-group';
2122

2223
type Loading = boolean | number;
2324

2425
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
2526
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
2627

27-
function isUnborderedButtonType(type: ButtonType | undefined) {
28+
function isUnBorderedButtonType(type: ButtonType | undefined) {
2829
return type === 'text' || type === 'link';
2930
}
3031
export { buttonProps };
@@ -37,7 +38,7 @@ export default defineComponent({
3738
// emits: ['click', 'mousedown'],
3839
setup(props, { slots, attrs, emit }) {
3940
const { prefixCls, autoInsertSpaceInButton, direction, size } = useConfigInject('btn', props);
40-
41+
const { size: groupSize } = GroupSizeContext.useInject();
4142
const buttonNodeRef = ref<HTMLElement>(null);
4243
const delayTimeoutRef = ref(undefined);
4344
let isNeedInserted = false;
@@ -76,7 +77,7 @@ export default defineComponent({
7677
const pre = prefixCls.value;
7778

7879
const sizeClassNameMap = { large: 'lg', small: 'sm', middle: undefined };
79-
const sizeFullname = size.value;
80+
const sizeFullname = groupSize?.value || size.value;
8081
const sizeCls = sizeFullname ? sizeClassNameMap[sizeFullname] || '' : '';
8182

8283
return {
@@ -85,7 +86,7 @@ export default defineComponent({
8586
[`${pre}-${shape}`]: shape !== 'default' && shape,
8687
[`${pre}-${sizeCls}`]: sizeCls,
8788
[`${pre}-loading`]: innerLoading.value,
88-
[`${pre}-background-ghost`]: ghost && !isUnborderedButtonType(type),
89+
[`${pre}-background-ghost`]: ghost && !isUnBorderedButtonType(type),
8990
[`${pre}-two-chinese-chars`]: hasTwoCNChar.value && autoInsertSpace.value,
9091
[`${pre}-block`]: block,
9192
[`${pre}-dangerous`]: !!danger,
@@ -132,7 +133,7 @@ export default defineComponent({
132133

133134
watchEffect(() => {
134135
devWarning(
135-
!(props.ghost && isUnborderedButtonType(props.type)),
136+
!(props.ghost && isUnBorderedButtonType(props.type)),
136137
'Button',
137138
"`link` or `text` button can't be a `ghost` button.",
138139
);
@@ -149,7 +150,7 @@ export default defineComponent({
149150
const { icon = slots.icon?.() } = props;
150151
const children = flattenChildren(slots.default?.());
151152

152-
isNeedInserted = children.length === 1 && !icon && !isUnborderedButtonType(props.type);
153+
isNeedInserted = children.length === 1 && !icon && !isUnBorderedButtonType(props.type);
153154

154155
const { type, htmlType, disabled, href, title, target, onMousedown } = props;
155156

@@ -202,7 +203,7 @@ export default defineComponent({
202203
</button>
203204
);
204205

205-
if (isUnborderedButtonType(type)) {
206+
if (isUnBorderedButtonType(type)) {
206207
return buttonNode;
207208
}
208209

components/button/style/mixin.less

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -210,28 +210,6 @@
210210
.@{btnClassName}-icon-only {
211211
font-size: @font-size-base;
212212
}
213-
// size
214-
&-lg > .@{btnClassName},
215-
&-lg > span > .@{btnClassName} {
216-
.button-size(@btn-height-lg; @btn-padding-horizontal-lg; @btn-font-size-lg; 0);
217-
}
218-
&-lg .@{btnClassName}.@{btnClassName}-icon-only {
219-
.square(@btn-height-lg);
220-
padding-right: 0;
221-
padding-left: 0;
222-
}
223-
&-sm > .@{btnClassName},
224-
&-sm > span > .@{btnClassName} {
225-
.button-size(@btn-height-sm; @btn-padding-horizontal-sm; @font-size-base; 0);
226-
> .@{iconfont-css-prefix} {
227-
font-size: @font-size-base;
228-
}
229-
}
230-
&-sm .@{btnClassName}.@{btnClassName}-icon-only {
231-
.square(@btn-height-sm);
232-
padding-right: 0;
233-
padding-left: 0;
234-
}
235213
}
236214
// Base styles of buttons
237215
// --------------------------------------------------

0 commit comments

Comments
 (0)