@ywfe/materials-design
Version:
YwDesign for lowcode
87 lines (80 loc) • 2.79 kB
text/typescript
export { get, set, has } from 'lodash';
// simple uuid
export function uuid() {
return ((Math.random() * 1e6) >> 0).toString(36);
}
export const mockId = () => `id-${uuid()}`;
export const hideProp = () => false;
export const showWithLabelAlign = (target: any) => {
const parentLabelAlign = target && target.parent.node.parent.getPropValue('labelAlign');
const labelAlign = parentLabelAlign || (target && target.parent.getPropValue('labelAlign'));
if (labelAlign === 'top') {
const enableLabelTip = target.parent.getPropValue('labelTip.enable');
if (enableLabelTip) {
return target.name.startsWith('labelTip');
}
return target.name === 'labelTip.enable';
}
if (labelAlign === 'inset') {
return target.name.startsWith('wrapper');
}
if (labelAlign === 'left') {
return !target.name.startsWith('labelTip');
}
return true;
};
export const showWithLabelAlignShortcut = (target: any) => {
const { labelAlign: parentLabelAlign } = target && target.parent.node.propsData;
const labelAlign = parentLabelAlign || (target && target.parent.getPropValue('labelAlign'));
if (labelAlign === 'top') {
const enableLabelTip = target.parent.getPropValue('labelTip.enable');
if (enableLabelTip) {
return target.name.startsWith('labelTip');
}
return target.name === 'labelTip.enable';
}
if (labelAlign === 'inset') {
return target.name.startsWith('wrapper');
}
if (labelAlign === 'left') {
return !target.name.startsWith('labelTip');
}
return true;
};
export const getParentValue = (target: any) => {
return target.getProps().node.parent.getPropValue(target.name);
};
// 获取同级节点 父节点值 同级 .x 父级 ..x
export const getPropValueWithPath = (_target: any, _path: string) => {
const targetPath = _path.split('.');
let target = _target;
const data = targetPath.reduce((prev, curr) => {
if (curr === '') {
target = target.parent;
return target.path;
} else {
return prev.concat([curr]);
}
}, _target.path);
const value = _target.top.node.getPropValue(data.join('.'));
return value;
};
export const getPropsByDependence = (items: object[], deps: string[], path: string) => {
return items.map((v) => {
return {
...v,
condition: (target: any) => {
return deps.includes(getPropValueWithPath(target, path));
},
};
});
};
// 组件基于 ref 注册联动事件
export const registerActions = (target: any, actions: Array<{ path: string; type: string }>) => {
target.parent.setPropValue('_actions', actions);
target.parent.setPropValue('_registerActions', {
type: 'JSFunction',
value:
'function(){return this.registerActions.apply(this,Array.prototype.slice.call(arguments).concat([])) }',
});
};