UNPKG

@schema-render/form-render-react

Version:

Out-of-the-box form rendering library based on Core and Antd.

85 lines (84 loc) 2.5 kB
import { utils } from "@schema-render/core-react"; import { ACTIONS_RENDER_TYPE } from "../constants"; const { isArray, isNil, isObject } = utils; /** * 添加操作行为 Schema */ export function addActionsSchema(schema, actionsRestSchema) { const filed = `${ACTIONS_RENDER_TYPE}_${Date.now()}`; return { ...schema, properties: { ...schema === null || schema === void 0 ? void 0 : schema.properties, [filed]: { ...actionsRestSchema, renderType: ACTIONS_RENDER_TYPE } } }; } /** * 计算 normal 模式下 actions 左偏移值 */ export function calcActionsMarginLeft({ itemLayout, labelWidth, labelGap }) { if (itemLayout === 'horizontal' && utils.isNumber(labelWidth)) { return labelWidth + labelGap; } return 0; } /** * 标准化遍历 Antd options 数据,支持跳出遍历 */ function forEachOptions(options = [], callback) { for(let i = 0; i < options.length; i++){ // 标准化 item 数据 const item = options[i]; const objectItem = isObject(item) ? item : { label: item, value: item }; const breakEach = callback(objectItem); if (breakEach) { return; } } } /** * 获取 Antd options 选项 label 数据 * @param options Antd options * @param checkedValues label 对应的 value 值,可以多个 */ export function getOptionsLabels(options, checkedValues) { const labels = []; if (!isArray(options) || !isArray(checkedValues)) { return labels; } checkedValues.forEach((value)=>{ if (isNil(value)) { return; } forEachOptions(options, (item)=>{ // 找到一项,跳出遍历 if (value === item.value) { labels.push(item.label); return true; } }); }); return labels; } /** * 查找选中的 Antd options 选项数据 */ export function getCheckedOptions(options, checkedValues) { const result = []; if (!isArray(options) || !isArray(checkedValues)) { return result; } forEachOptions(options, (item)=>{ if (checkedValues.includes(item.value)) { result.push(item); } }); return result; } /** * 校验是否是空值:null、undefined 和空字符串判断为空 */ export function isEmpty(value) { return !!(isNil(value) || value === ''); }