You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/_util/props-util.js

296 lines
7.6 KiB

import isPlainObject from 'lodash/isPlainObject';
import classNames from 'classnames';
function getType(fn) {
const match = fn && fn.toString().match(/^\s*function (\w+)/);
return match ? match[1] : '';
}
const camelizeRE = /-(\w)/g;
const camelize = str => {
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''));
};
7 years ago
const parseStyleText = (cssText = '', camel) => {
const res = {};
const listDelimiter = /;(?![^(]*\))/g;
const propertyDelimiter = /:(.+)/;
cssText.split(listDelimiter).forEach(function(item) {
7 years ago
if (item) {
const tmp = item.split(propertyDelimiter);
7 years ago
if (tmp.length > 1) {
const k = camel ? camelize(tmp[0].trim()) : tmp[0].trim();
res[k] = tmp[1].trim();
7 years ago
}
7 years ago
}
});
return res;
};
7 years ago
7 years ago
const hasProp = (instance, prop) => {
const $options = instance.$options || {};
const propsData = $options.propsData || {};
return prop in propsData;
};
7 years ago
const slotHasProp = (slot, prop) => {
const $options = slot.componentOptions || {};
const propsData = $options.propsData || {};
return prop in propsData;
};
7 years ago
const filterProps = (props, propsData = {}) => {
const res = {};
Object.keys(props).forEach(k => {
7 years ago
if (k in propsData || props[k] !== undefined) {
res[k] = props[k];
7 years ago
}
});
return res;
};
const getSlots = ele => {
let componentOptions = ele.componentOptions || {};
7 years ago
if (ele.$vnode) {
componentOptions = ele.$vnode.componentOptions || {};
7 years ago
}
const children = ele.children || componentOptions.children || [];
const slots = {};
7 years ago
children.forEach(child => {
if (!isEmptyElement(child)) {
const name = (child.data && child.data.slot) || 'default';
slots[name] = slots[name] || [];
slots[name].push(child);
}
});
return slots;
};
const getAllChildren = ele => {
let componentOptions = ele.componentOptions || {};
if (ele.$vnode) {
componentOptions = ele.$vnode.componentOptions || {};
}
return ele.children || componentOptions.children || [];
};
const getSlotOptions = ele => {
if (ele.fnOptions) {
// 函数式组件
return ele.fnOptions;
}
let componentOptions = ele.componentOptions;
7 years ago
if (ele.$vnode) {
componentOptions = ele.$vnode.componentOptions;
7 years ago
}
return componentOptions ? componentOptions.Ctor.options || {} : {};
};
const getOptionProps = instance => {
7 years ago
if (instance.componentOptions) {
const componentOptions = instance.componentOptions;
const { propsData = {}, Ctor = {} } = componentOptions;
const props = (Ctor.options || {}).props || {};
const res = {};
7 years ago
for (const [k, v] of Object.entries(props)) {
const def = v.default;
if (def !== undefined) {
res[k] =
typeof def === 'function' && getType(v.type) !== 'Function' ? def.call(instance) : def;
7 years ago
}
}
return { ...res, ...propsData };
7 years ago
}
const { $options = {}, $props = {} } = instance;
return filterProps($props, $options.propsData);
};
7 years ago
const getComponentFromProp = (instance, prop, options = instance, execute = true) => {
7 years ago
if (instance.$createElement) {
const h = instance.$createElement;
const temp = instance[prop];
7 years ago
if (temp !== undefined) {
return typeof temp === 'function' && execute ? temp(h, options) : temp;
7 years ago
}
return (
instance.$slots[prop] ||
(instance.$scopedSlots[prop] && execute && instance.$scopedSlots[prop](options)) ||
instance.$scopedSlots[prop] ||
undefined
);
7 years ago
} else {
const h = instance.context.$createElement;
const temp = getPropsData(instance)[prop];
7 years ago
if (temp !== undefined) {
return typeof temp === 'function' && execute ? temp(h, options) : temp;
7 years ago
}
const slotsProp = [];
7 years ago
const componentOptions = instance.componentOptions || {};
(componentOptions.children || []).forEach(child => {
7 years ago
if (child.data && child.data.slot === prop) {
7 years ago
if (child.tag === 'template') {
slotsProp.push(child.children);
7 years ago
} else {
slotsProp.push(child);
7 years ago
}
7 years ago
}
});
return slotsProp.length ? slotsProp : undefined;
7 years ago
}
};
7 years ago
const getAllProps = ele => {
let data = ele.data || {};
let componentOptions = ele.componentOptions || {};
if (ele.$vnode) {
data = ele.$vnode.data || {};
componentOptions = ele.$vnode.componentOptions || {};
}
return { ...data.props, ...data.attrs, ...componentOptions.propsData };
};
const getPropsData = ele => {
let componentOptions = ele.componentOptions;
7 years ago
if (ele.$vnode) {
componentOptions = ele.$vnode.componentOptions;
7 years ago
}
return componentOptions ? componentOptions.propsData || {} : {};
};
7 years ago
const getValueByProp = (ele, prop) => {
return getPropsData(ele)[prop];
};
7 years ago
const getAttrs = ele => {
let data = ele.data;
7 years ago
if (ele.$vnode) {
data = ele.$vnode.data;
7 years ago
}
return data ? data.attrs || {} : {};
};
7 years ago
const getKey = ele => {
let key = ele.key;
7 years ago
if (ele.$vnode) {
key = ele.$vnode.key;
7 years ago
}
return key;
};
7 years ago
export function getEvents(child) {
let events = {};
7 years ago
if (child.componentOptions && child.componentOptions.listeners) {
events = child.componentOptions.listeners;
7 years ago
} else if (child.data && child.data.on) {
events = child.data.on;
7 years ago
}
return { ...events };
7 years ago
}
export function getClass(ele) {
let data = {};
7 years ago
if (ele.data) {
data = ele.data;
7 years ago
} else if (ele.$vnode && ele.$vnode.data) {
data = ele.$vnode.data;
7 years ago
}
const tempCls = data.class || {};
const staticClass = data.staticClass;
let cls = {};
staticClass &&
staticClass.split(' ').forEach(c => {
cls[c.trim()] = true;
});
if (typeof tempCls === 'string') {
tempCls.split(' ').forEach(c => {
cls[c.trim()] = true;
});
} else if (Array.isArray(tempCls)) {
classNames(tempCls)
.split(' ')
.forEach(c => {
cls[c.trim()] = true;
});
} else {
cls = { ...cls, ...tempCls };
7 years ago
}
return cls;
7 years ago
}
export function getStyle(ele, camel) {
let data = {};
7 years ago
if (ele.data) {
data = ele.data;
7 years ago
} else if (ele.$vnode && ele.$vnode.data) {
data = ele.$vnode.data;
7 years ago
}
let style = data.style || data.staticStyle;
if (typeof style === 'string') {
style = parseStyleText(style, camel);
} else if (camel && style) {
// 驼峰化
const res = {};
Object.keys(style).forEach(k => (res[camelize(k)] = style[k]));
return res;
}
return style;
7 years ago
}
export function getComponentName(opts) {
return opts && (opts.Ctor.options.name || opts.tag);
7 years ago
}
export function isEmptyElement(c) {
return !(c.tag || (c.text && c.text.trim() !== ''));
7 years ago
}
export function filterEmpty(children = []) {
return children.filter(c => !isEmptyElement(c));
7 years ago
}
7 years ago
const initDefaultProps = (propTypes, defaultProps) => {
Object.keys(defaultProps).forEach(k => {
if (propTypes[k]) {
propTypes[k].def && (propTypes[k] = propTypes[k].def(defaultProps[k]));
} else {
throw new Error(`not have ${k} prop`);
}
});
return propTypes;
};
export function mergeProps() {
const args = [].slice.call(arguments, 0);
const props = {};
args.forEach((p = {}, i) => {
for (const [k, v] of Object.entries(p)) {
props[k] = props[k] || {};
if (isPlainObject(v)) {
Object.assign(props[k], v);
} else {
props[k] = v;
}
}
});
return props;
}
function isValidElement(element) {
return (
element &&
typeof element === 'object' &&
'componentOptions' in element &&
'context' in element &&
element.tag !== undefined
); // remove text node
7 years ago
}
7 years ago
export {
hasProp,
filterProps,
getOptionProps,
getComponentFromProp,
getSlotOptions,
slotHasProp,
getPropsData,
getKey,
getAttrs,
7 years ago
getValueByProp,
7 years ago
parseStyleText,
7 years ago
initDefaultProps,
7 years ago
isValidElement,
7 years ago
camelize,
getSlots,
getAllProps,
getAllChildren,
};
export default hasProp;