feat: update configprovider

pull/2324/head
tanjinzhou 2020-05-28 19:45:50 +08:00
parent cfe61108c5
commit 242b5a3d99
8 changed files with 73 additions and 42 deletions

@ -1 +1 @@
Subproject commit adbfcd30aeb6c125defa35102ed659f1be03c672 Subproject commit f3b049411f17e556d8795f40d41bfadb471ae630

View File

@ -5,6 +5,23 @@ import classNames from 'classnames';
// return match ? match[1] : ''; // return match ? match[1] : '';
// } // }
const onRE = /^on[^a-z]/;
export const isOn = key => onRE.test(key);
const splitAttrs = attrs => {
const allAttrs = Object.keys(attrs);
const eventAttrs = [];
const extraAttrs = [];
for (let i = 0, l = allAttrs.length; i < l; i++) {
const key = allAttrs[i];
if (isOn(key)) {
eventAttrs.push({ [key[2].toLowerCase() + key.slice(3)]: attrs[key] });
} else {
extraAttrs.push({ [key]: attrs[key] });
}
}
return { events: eventAttrs, extraAttrs };
};
const camelizeRE = /-(\w)/g; const camelizeRE = /-(\w)/g;
const camelize = str => { const camelize = str => {
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : '')); return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''));
@ -261,7 +278,7 @@ export function getComponentName(opts) {
} }
export function isEmptyElement(c) { export function isEmptyElement(c) {
return !(c.tag || (c.text && c.text.trim() !== '')); return typeof c.type === 'symbol' && c.children.trim() === '';
} }
export function isStringElement(c) { export function isStringElement(c) {
@ -309,6 +326,7 @@ function isValidElement(element) {
} }
export { export {
splitAttrs,
hasProp, hasProp,
filterProps, filterProps,
getOptionProps, getOptionProps,

View File

@ -1,7 +1,8 @@
import { inject } from 'vue';
import Wave from '../_util/wave'; import Wave from '../_util/wave';
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined'; import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
import buttonTypes from './buttonTypes'; import buttonTypes from './buttonTypes';
import { filterEmpty, getListeners } from '../_util/props-util'; import { filterEmpty } from '../_util/props-util';
import { ConfigConsumerProps } from '../config-provider'; import { ConfigConsumerProps } from '../config-provider';
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/; const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
@ -12,8 +13,11 @@ export default {
inheritAttrs: false, inheritAttrs: false,
__ANT_BUTTON: true, __ANT_BUTTON: true,
props, props,
inject: { setup() {
configProvider: { default: () => ConfigConsumerProps }, const configProvider = inject('configProvider') || ConfigConsumerProps;
return {
configProvider,
};
}, },
data() { data() {
return { return {
@ -63,6 +67,7 @@ export default {
block, block,
icon, icon,
$slots, $slots,
$attrs,
} = this; } = this;
const getPrefixCls = this.configProvider().getPrefixCls; const getPrefixCls = this.configProvider().getPrefixCls;
const prefixCls = getPrefixCls('btn', customizePrefixCls); const prefixCls = getPrefixCls('btn', customizePrefixCls);
@ -84,6 +89,7 @@ export default {
const iconType = sLoading ? 'loading' : icon; const iconType = sLoading ? 'loading' : icon;
const children = filterEmpty($slots.default()); const children = filterEmpty($slots.default());
return { return {
[$attrs.class]: $attrs.class,
[`${prefixCls}`]: true, [`${prefixCls}`]: true,
[`${prefixCls}-${type}`]: type, [`${prefixCls}-${type}`]: type,
[`${prefixCls}-${shape}`]: shape, [`${prefixCls}-${shape}`]: shape,
@ -139,19 +145,14 @@ export default {
const classes = this.getClasses(); const classes = this.getClasses();
const { type, htmlType, icon, disabled, handleClick, sLoading, $slots, $attrs } = this; const { type, htmlType, icon, disabled, handleClick, sLoading, $slots, $attrs } = this;
const buttonProps = { const buttonProps = {
attrs: { ...$attrs,
...$attrs, disabled,
disabled,
},
class: classes, class: classes,
on: { onClick: handleClick,
...getListeners(this),
click: handleClick,
},
}; };
const iconNode = sLoading ? <LoadingOutlined /> : icon; const iconNode = sLoading ? <LoadingOutlined /> : icon;
const children = $slots.default(); const children = $slots.default();
const autoInsertSpace = this.configProvider().autoInsertSpaceInButton !== false; const autoInsertSpace = this.configProvider.autoInsertSpaceInButton !== false;
const kids = children.map(child => const kids = children.map(child =>
this.insertSpace(child, this.isNeedInserted() && autoInsertSpace), this.insertSpace(child, this.isNeedInserted() && autoInsertSpace),
); );

View File

@ -1,6 +1,6 @@
import Vue from 'vue'; import { reactive, provide } from 'vue';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import { filterEmpty, getComponentFromProp } from '../_util/props-util'; import { getComponentFromProp } from '../_util/props-util';
import defaultRenderEmpty from './renderEmpty'; import defaultRenderEmpty from './renderEmpty';
import Base from '../base'; import Base from '../base';
import LocaleProvider, { ANT_MARK } from '../locale-provider'; import LocaleProvider, { ANT_MARK } from '../locale-provider';
@ -28,21 +28,21 @@ const ConfigProvider = {
pageHeader: PropTypes.object, pageHeader: PropTypes.object,
transformCellText: PropTypes.func, transformCellText: PropTypes.func,
}, },
provide() { setup(props, context) {
const _self = this; provide(
this._proxyVm = new Vue({ 'configProvider',
data() { reactive({
return { ...props,
..._self.$props, getPrefixCls: context.getPrefixCls,
getPrefixCls: _self.getPrefixCls, renderEmpty: context.renderEmptyComponent,
renderEmpty: _self.renderEmptyComponent, }),
}; );
},
});
return {
configProvider: this._proxyVm._data,
};
}, },
// provide() {
// return {
// configProvider: this._proxyVm,
// };
// },
watch: { watch: {
...getWatch([ ...getWatch([
'prefixCls', 'prefixCls',
@ -67,18 +67,14 @@ const ConfigProvider = {
renderProvider(legacyLocale) { renderProvider(legacyLocale) {
return ( return (
<LocaleProvider locale={this.locale || legacyLocale} _ANT_MARK__={ANT_MARK}> <LocaleProvider locale={this.locale || legacyLocale} _ANT_MARK__={ANT_MARK}>
{this.$slots.default ? filterEmpty(this.$slots.default)[0] : null} {this.$slots.default ? this.$slots.default() : null}
</LocaleProvider> </LocaleProvider>
); );
}, },
}, },
render() { render() {
return ( return <LocaleReceiver children={(_, __, legacyLocale) => this.renderProvider(legacyLocale)} />;
<LocaleReceiver
scopedSlots={{ default: (_, __, legacyLocale) => this.renderProvider(legacyLocale) }}
/>
);
}, },
}; };

View File

@ -36,8 +36,8 @@ export default {
}, },
render() { render() {
const { $scopedSlots } = this; const { $slots } = this;
const children = this.children || $scopedSlots.default; const children = this.children || $slots.default;
const { antLocale } = this.localeData; const { antLocale } = this.localeData;
return children(this.getLocale(), this.getLocaleCode(), antLocale); return children(this.getLocale(), this.getLocaleCode(), antLocale);
}, },

View File

@ -69,7 +69,7 @@ const LocaleProvider = {
changeConfirmLocale(); changeConfirmLocale();
}, },
render() { render() {
return this.$slots.default ? this.$slots.default[0] : null; return this.$slots.default ? this.$slots.default() : null;
}, },
}; };

View File

@ -1,6 +1,15 @@
<template> <template>
<div> <a-config-provider prefixCls="ss">
<a-button type="primary" block @click="onClick"> jdjj
<div>ddd</div>
<a-button
:class="['test']"
class="aaa"
style="display: inline"
block
@click="onClick"
:type="type"
>
Primary Primary
</a-button> </a-button>
<a-button block> <a-button block>
@ -15,12 +24,17 @@
<a-button type="link" block> <a-button type="link" block>
Link Link
</a-button> </a-button>
</div> </a-config-provider>
</template> </template>
<script> <script>
export default { export default {
name: 'Demo', name: 'Demo',
data() {
return {
type: 'primary',
};
},
methods: { methods: {
onClick() { onClick() {
console.log(1); console.log(1);

View File

@ -2,8 +2,10 @@ import '@babel/polyfill';
import { createApp } from 'vue'; import { createApp } from 'vue';
import App from './App.vue'; import App from './App.vue';
import Button from 'ant-design-vue/button'; import Button from 'ant-design-vue/button';
import ConfigProvider from 'ant-design-vue/config-provider';
import 'ant-design-vue/button/style/index.less'; import 'ant-design-vue/button/style/index.less';
createApp(App) createApp(App)
.use(Button) .use(Button)
.use(ConfigProvider)
.mount('#app'); .mount('#app');