vue-rise-realize
Version:
一款服务于 Rise 前端低代码平台的表单渲染器
191 lines (171 loc) • 4.07 kB
JavaScript
// 基础工具
import {
isEmptyObject,
dataTypeOf,
isNumberStr,
debounce,
DEEP_CLONE,
FIND_ITEM,
FIND_REAL_ITEM,
FIND_PARENT,
SMARTS_OBJECT,
Export_Excel,
} from './utils.d';
// 组件安装工具
import {
MOUNT_DEFAULT_FORM,
MOUNT_DEFAULT,
UNMOUNT_DEFAULT,
UNMOUNT_DEFAULT_FORM,
GET_COMPONENT,
GET_PLACE_HOLDER,
GET_BIND_DATA,
DEL_WIDGET,
ADD_WIDGET,
} from './widgetUtils.d';
// 格式化工具
import {
titleCase,
camelCase,
formatDate,
rangeDate,
thousandSeparator,
smallToBig,
} from './format';
// 校验工具
import * as Validate from './validate';
// 环境标识
const ENV = 'RISE_REALIZE';
/**
* 渲染器组工具
*/
// 获取校验规则
const GET_WIDGET_RULES = (widgetDisabled, configure, formRealize) => {
let rules = [],
formItem = configure.formItem || {};
if (widgetDisabled || formItem.disabled) {
return null;
}
if (formItem.rules) {
rules = [...formItem.rules];
}
if (rules.length === 0 && !formItem.required) {
return null;
}
rules.forEach((item) => {
//正则装配
if (typeof item.pattern == 'string') {
let arr = item.pattern.split('/');
if (arr[2] != '') {
item.pattern = new RegExp(arr[1], arr[2]);
} else {
item.pattern = new RegExp(arr[1]);
}
}
//校验函数装配
if (typeof item.validator == 'string') {
item.validator = new Function(
'rule',
'value',
'callback',
item.validator,
).bind(formRealize);
}
//内置校验函数装配
if (typeof item.validatorCite == 'string') {
item.validator = (rule, value, callback) => {
if (value === '' || value === undefined || value === null) {
callback();
}
Validate[item.validatorCite](rule, value, callback);
};
}
if (!item.trigger) {
item.trigger = 'blur';
}
});
let dataType = 'string';
if (
configure.type === 'form-address' ||
configure.type === 'checkbox-group' ||
configure.type === 'form-upload'
) {
dataType = 'array';
} else if (configure.type === 'checkbox') {
dataType = 'boolean';
} else if (configure.type === 'form-select' && configure.multiple) {
dataType = 'array';
} else if (configure.type === 'cascader') {
dataType = 'array';
} else if (configure.type === 'smooth-signature') {
dataType = 'array';
} else if (
configure.type === 'date-picker' &&
configure.attrType.indexOf('range') != -1
) {
dataType = 'array';
} else if (configure.type === 'time-picker' && configure.isRange) {
dataType = 'array';
} else if (
configure.type === 'input-number' ||
configure.attrType === 'number'
) {
dataType = 'number';
} else if (configure.type === 'self') {
let temporary = rules;
if (rules.length == 0 && formItem.required) {
temporary = [
{
required: true,
message: '为必填项',
},
];
}
return temporary;
}
let label = formItem.label ? formItem.label : configure.name;
return [
{
label: '必填规则',
required: formItem.required,
type: dataType,
message: configure.type.includes('input')
? `【${label}】为必填项!`
: `【${label}】为必选项!`,
trigger: 'change',
},
...rules,
];
};
export {
ENV,
//基础工具
isEmptyObject,
dataTypeOf,
isNumberStr,
debounce,
SMARTS_OBJECT,
DEEP_CLONE,
FIND_ITEM,
FIND_REAL_ITEM,
FIND_PARENT,
Export_Excel,
//安装工具
MOUNT_DEFAULT_FORM,
MOUNT_DEFAULT,
UNMOUNT_DEFAULT,
UNMOUNT_DEFAULT_FORM,
GET_COMPONENT,
GET_PLACE_HOLDER,
GET_BIND_DATA,
DEL_WIDGET,
ADD_WIDGET,
GET_WIDGET_RULES,
//格式化工具
titleCase,
camelCase,
formatDate,
rangeDate,
thousandSeparator,
smallToBig,
};