ant-design-vue/components/config-provider/index.jsx

76 lines
1.9 KiB
Vue
Raw Normal View History

2019-03-15 15:12:28 +08:00
import Vue from 'vue';
2019-01-12 11:33:27 +08:00
import PropTypes from '../_util/vue-types';
2019-04-10 13:28:07 +08:00
import { filterEmpty, getComponentFromProp } from '../_util/props-util';
import defaultRenderEmpty from './renderEmpty';
import Base from '../base';
2019-01-02 20:13:25 +08:00
2019-03-15 15:12:28 +08:00
function getWatch(keys = []) {
const watch = {};
keys.forEach(k => {
watch[k] = function() {
this._proxyVm._data[k] = value;
};
});
return watch;
}
2019-01-02 20:13:25 +08:00
const ConfigProvider = {
name: 'AConfigProvider',
props: {
getPopupContainer: PropTypes.func,
prefixCls: PropTypes.string,
renderEmpty: PropTypes.any,
csp: PropTypes.any,
autoInsertSpaceInButton: PropTypes.bool,
2019-01-02 20:13:25 +08:00
},
2019-01-12 11:33:27 +08:00
provide() {
2019-03-15 15:12:28 +08:00
const _self = this;
this._proxyVm = new Vue({
data() {
return {
..._self.$props,
getPrefixCls: _self.getPrefixCls,
renderEmpty: _self.renderEmptyComponent,
};
},
});
2019-01-02 20:13:25 +08:00
return {
2019-03-15 15:12:28 +08:00
configProvider: this._proxyVm._data,
2019-01-12 11:33:27 +08:00
};
2019-01-02 20:13:25 +08:00
},
2019-03-15 15:12:28 +08:00
watch: {
2019-05-28 11:37:38 +08:00
...getWatch(['prefixCls', 'csp', 'autoInsertSpaceInButton']),
2019-03-15 15:12:28 +08:00
},
2019-03-15 10:04:14 +08:00
methods: {
2019-09-11 22:35:25 +08:00
renderEmptyComponent(h, name) {
const renderEmpty =
getComponentFromProp(this, 'renderEmpty', {}, false) || defaultRenderEmpty;
return renderEmpty(h, name);
2019-03-14 21:21:58 +08:00
},
getPrefixCls(suffixCls, customizePrefixCls) {
const { prefixCls = 'ant' } = this.$props;
if (customizePrefixCls) return customizePrefixCls;
return suffixCls ? `${prefixCls}-${suffixCls}` : prefixCls;
},
},
2019-01-12 11:33:27 +08:00
render() {
return this.$slots.default ? filterEmpty(this.$slots.default) : null;
},
};
export const ConfigConsumerProps = {
getPrefixCls: (suffixCls, customizePrefixCls) => {
if (customizePrefixCls) return customizePrefixCls;
return `ant-${suffixCls}`;
2019-01-02 20:13:25 +08:00
},
2019-03-14 21:21:58 +08:00
renderEmpty: defaultRenderEmpty,
2019-01-12 11:33:27 +08:00
};
2019-01-02 20:13:25 +08:00
/* istanbul ignore next */
2019-01-12 11:33:27 +08:00
ConfigProvider.install = function(Vue) {
Vue.use(Base);
2019-01-12 11:33:27 +08:00
Vue.component(ConfigProvider.name, ConfigProvider);
};
2019-01-02 20:13:25 +08:00
2019-01-12 11:33:27 +08:00
export default ConfigProvider;