Skip to content

refactor(switch): use composition api #3885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions components/_util/props-util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import isValid from '../isValid';
// return match ? match[1] : '';
// }

const splitAttrs = (attrs) => {
const splitAttrs = attrs => {
const allAttrs = Object.keys(attrs);
const eventAttrs = {};
const onEvents = {};
Expand All @@ -28,7 +28,7 @@ const parseStyleText = (cssText = '', camel) => {
const res = {};
const listDelimiter = /;(?![^(]*\))/g;
const propertyDelimiter = /:(.+)/;
cssText.split(listDelimiter).forEach(function (item) {
cssText.split(listDelimiter).forEach(function(item) {
if (item) {
const tmp = item.split(propertyDelimiter);
if (tmp.length > 1) {
Expand All @@ -48,18 +48,18 @@ const slotHasProp = (slot, prop) => {
return hasProp(slot, prop);
};

const getScopedSlots = (ele) => {
const getScopedSlots = ele => {
return (ele.data && ele.data.scopedSlots) || {};
};

const getSlots = (ele) => {
const getSlots = ele => {
let componentOptions = ele.componentOptions || {};
if (ele.$vnode) {
componentOptions = ele.$vnode.componentOptions || {};
}
const children = ele.children || componentOptions.children || [];
const slots = {};
children.forEach((child) => {
children.forEach(child => {
if (!isEmptyElement(child)) {
const name = (child.data && child.data.slot) || 'default';
slots[name] = slots[name] || [];
Expand All @@ -72,7 +72,7 @@ const getSlots = (ele) => {
const flattenChildren = (children = [], filterEmpty = true) => {
const temp = Array.isArray(children) ? children : [children];
const res = [];
temp.forEach((child) => {
temp.forEach(child => {
if (Array.isArray(child)) {
res.push(...flattenChildren(child, filterEmpty));
} else if (child && child.type === Fragment) {
Expand Down Expand Up @@ -105,7 +105,7 @@ const getSlot = (self, name = 'default', options = {}) => {
}
};

const getAllChildren = (ele) => {
const getAllChildren = ele => {
let componentOptions = ele.componentOptions || {};
if (ele.$vnode) {
componentOptions = ele.$vnode.componentOptions || {};
Expand All @@ -115,18 +115,18 @@ const getAllChildren = (ele) => {
const getSlotOptions = () => {
throw Error('使用 .type 直接取值');
};
const findDOMNode = (instance) => {
const findDOMNode = instance => {
let node = instance && (instance.$el || instance);
while (node && !node.tagName) {
node = node.nextSibling;
}
return node;
};
const getOptionProps = (instance) => {
const getOptionProps = instance => {
const res = {};
if (instance.$ && instance.$.vnode) {
const props = instance.$.vnode.props || {};
Object.keys(instance.$props).forEach((k) => {
Object.keys(instance.$props).forEach(k => {
const v = instance.$props[k];
const hyphenateKey = hyphenate(k);
if (v !== undefined || hyphenateKey in props) {
Expand All @@ -136,11 +136,11 @@ const getOptionProps = (instance) => {
} else if (isVNode(instance) && typeof instance.type === 'object') {
const originProps = instance.props || {};
const props = {};
Object.keys(originProps).forEach((key) => {
Object.keys(originProps).forEach(key => {
props[camelize(key)] = originProps[key];
});
const options = instance.type.props || {};
Object.keys(options).forEach((k) => {
Object.keys(options).forEach(k => {
const v = resolvePropValue(options, props, k, props[k]);
if (v !== undefined || k in props) {
res[k] = v;
Expand Down Expand Up @@ -202,7 +202,7 @@ const getComponentFromProp = (instance, prop, options = instance, execute = true
}
const slotsProp = [];
const componentOptions = instance.componentOptions || {};
(componentOptions.children || []).forEach((child) => {
(componentOptions.children || []).forEach(child => {
if (child.data && child.data.slot === prop) {
if (child.data.attrs) {
delete child.data.attrs.slot;
Expand All @@ -218,7 +218,7 @@ const getComponentFromProp = (instance, prop, options = instance, execute = true
}
};

const getAllProps = (ele) => {
const getAllProps = ele => {
let props = getOptionProps(ele);
if (ele.$) {
props = { ...props, ...this.$attrs };
Expand All @@ -228,17 +228,17 @@ const getAllProps = (ele) => {
return props;
};

const getPropsData = (ins) => {
const getPropsData = ins => {
const vnode = ins.$ ? ins.$ : ins;
const res = {};
const originProps = vnode.props || {};
const props = {};
Object.keys(originProps).forEach((key) => {
Object.keys(originProps).forEach(key => {
props[camelize(key)] = originProps[key];
});
const options = isPlainObject(vnode.type) ? vnode.type.props : {};
options &&
Object.keys(options).forEach((k) => {
Object.keys(options).forEach(k => {
const v = resolvePropValue(options, props, k, props[k]);
if (k in props) {
// 仅包含 props,不包含默认值
Expand All @@ -251,15 +251,15 @@ const getValueByProp = (ele, prop) => {
return getPropsData(ele)[prop];
};

const getAttrs = (ele) => {
const getAttrs = ele => {
let data = ele.data;
if (ele.$vnode) {
data = ele.$vnode.data;
}
return data ? data.attrs || {} : {};
};

const getKey = (ele) => {
const getKey = ele => {
let key = ele.key;
return key;
};
Expand Down Expand Up @@ -297,13 +297,13 @@ export function getClass(ele) {
let tempCls = props.class || {};
let cls = {};
if (typeof tempCls === 'string') {
tempCls.split(' ').forEach((c) => {
tempCls.split(' ').forEach(c => {
cls[c.trim()] = true;
});
} else if (Array.isArray(tempCls)) {
classNames(tempCls)
.split(' ')
.forEach((c) => {
.forEach(c => {
cls[c.trim()] = true;
});
} else {
Expand All @@ -319,7 +319,7 @@ export function getStyle(ele, camel) {
} else if (camel && style) {
// 驼峰化
const res = {};
Object.keys(style).forEach((k) => (res[camelize(k)] = style[k]));
Object.keys(style).forEach(k => (res[camelize(k)] = style[k]));
return res;
}
return style;
Expand Down Expand Up @@ -347,7 +347,7 @@ export function isStringElement(c) {

export function filterEmpty(children = []) {
const res = [];
children.forEach((child) => {
children.forEach(child => {
if (Array.isArray(child)) {
res.push(...child);
} else if (child.type === Fragment) {
Expand All @@ -356,10 +356,10 @@ export function filterEmpty(children = []) {
res.push(child);
}
});
return res.filter((c) => !isEmptyElement(c));
return res.filter(c => !isEmptyElement(c));
}
const initDefaultProps = (propTypes, defaultProps) => {
Object.keys(defaultProps).forEach((k) => {
Object.keys(defaultProps).forEach(k => {
if (propTypes[k]) {
propTypes[k].def && (propTypes[k] = propTypes[k].def(defaultProps[k]));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Switch should has click wave effect 1`] = `
<button class="ant-switch ant-switch-checked" type="button" role="switch" aria-checked="true">
<button prefixcls="ant-switch" class="ant-switch ant-switch-checked" type="button" role="switch" aria-checked="true">
<!----><span class="ant-switch-inner"><!----></span>
</button>
`;
13 changes: 11 additions & 2 deletions components/switch/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ describe('Switch', () => {
mountTest(Switch);

it('should has click wave effect', async () => {
const wrapper = mount(Switch);
const wrapper = mount({
render() {
return <Switch />;
},
});
wrapper.find('.ant-switch').trigger('click');
await new Promise(resolve => setTimeout(resolve, 0));
expect(wrapper.html()).toMatchSnapshot();
Expand All @@ -19,7 +23,12 @@ describe('Switch', () => {
resetWarned();

const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
mount(Switch, { props: { value: '' } });
mount({
render() {
return <Switch value="" />;
},
});
console.log(errorSpy);
expect(errorSpy).toHaveBeenCalledWith(
'Warning: [antdv: Switch] `value` is not validate prop, do you mean `checked`?',
);
Expand Down
2 changes: 1 addition & 1 deletion components/switch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const Switch = defineComponent({
);
}
warning(
'checked' in props || !('value' in attrs),
!('value' in attrs),
'Switch',
'`value` is not validate prop, do you mean `checked`?',
);
Expand Down