import { IPublicModelSettingField } from '@alilc/lowcode-types'; import { material } from '@alilc/lowcode-engine'; import { stateProps, telescopeTableProps, telescopeTreeProps, selectOptionsProp, switchTypeProps, telescopeBasicProps, telescopeQueryProps, } from '../../_props'; import { DateSetter, RadioGroupSetter, SelectSetter, ValidationSetter, RequiredSetter, RsObjectSetter, RsArraySetter, } from '../../_setters'; import { ValidationType, Rs_QUERY_FILTER } from '@/constants'; import { uuid } from '../../_utils/utils'; import defaultValueProps from './default-value'; import { commonDefaultProps, compDefaultProps } from '@/constants/query-props'; import inputNumberFormatProps from '../../_props/input-number-format'; // 获取所有的属性名称 export const getAllUniqueProps = () => { const uniqueProps = new Set(commonDefaultProps); Object.values(compDefaultProps).forEach((props: string[]) => { props.forEach((prop: string) => uniqueProps.add(prop)); }); return Array.from(uniqueProps); }; const allUniqueProps = getAllUniqueProps(); const getInitialProp = (propsData: any, defaultValue: any = {}, component: any) => { if (propsData && Array.isArray(propsData) && propsData.length) { propsData.forEach((item) => { if (!item) return; if (item.name) { if (item.setter && typeof item.setter === 'object' && !Array.isArray(item.setter)) { // eslint-disable-next-line no-prototype-builtins if (item.setter.hasOwnProperty('initialValue')) { if (typeof item.setter.initialValue !== 'function') { // eslint-disable-next-line no-param-reassign defaultValue[item.name] = item.setter.initialValue; } else { // eslint-disable-next-line no-param-reassign defaultValue[item.name] = item.setter.initialValue(); } } // eslint-disable-next-line no-prototype-builtins } else if (item.hasOwnProperty('defaultValue') && typeof item.defaultValue !== 'function') { // eslint-disable-next-line no-param-reassign defaultValue[item.name] = item.defaultValue; } } else if (item.type === 'type' && item.items && item.items.length) { // eslint-disable-next-line @typescript-eslint/no-unused-vars getInitialProp(item.items, defaultValue, component); } }); } }; const getInitialPropsForFormItem = (componentName: any) => { const component: any = material ?.getAssets() ?.components.filter((item) => item.componentName === componentName)[0]; let defaultProps: any = {}; if (!component && !component?.props && !component?.configure) return defaultProps; const propsData = component?.props || []; const configureProps = component?.configure.props || []; getInitialProp([...propsData, ...configureProps], defaultProps, component); if (component.snippets && component.snippets.length) { const { schema } = component.snippets[0]; if (schema && schema.props) { defaultProps = { ...defaultProps, ...schema.props, }; } } return defaultProps; }; const baseFormItemProps = [ { name: 'key', title: { label: 'key', }, setter: 'StringSetter', propType: 'string', condition: () => { return false; }, }, { name: 'label', title: { label: { type: 'i18n', zh_CN: '标题', en_US: 'label', }, tip: { type: 'i18n', zh_CN: '属性: label | 说明: 标题', en_US: 'prop: label | description: form item label', }, }, defaultValue: '表单项', setter: 'StringSetter', important: true, }, { name: 'rs_id', title: { label: { type: 'i18n', zh_CN: '数据字段', en_US: 'rs_id', }, tip: { type: 'i18n', zh_CN: '属性: rs_id | 说明: 数据字段', en_US: 'prop: rs_id | description: rs_id', }, }, propType: 'string', setter: { componentName: RequiredSetter, initialValue: () => uuid(), }, important: true, setValue: (target: IPublicModelSettingField, value: any) => { value && target.parent.setPropValue('key', value); }, }, { name: 'componentName', title: { label: { type: 'i18n', zh_CN: '数据类型', en_US: 'componentName', }, tip: { type: 'i18n', zh_CN: '属性: componentName | 说明: 数据类型', en_US: 'prop: componentName | description: componentName', }, }, display: 'inline', defaultValue: 'RsInput', important: true, setter: () => { return { componentName: 'SelectSetter', props: { // 枚举所有的表单项组件 options: [ { label: '输入框', value: 'RsInput' }, { label: '数字输入框', value: 'RsInputNumber' }, { label: '日期选择器', value: 'RsDatePicker' }, { label: '日期区间', value: 'RsRangePicker' }, { label: '下拉框', value: 'RsSelect' }, { label: '望远镜', value: 'RsTelescope' }, { label: '开关', value: 'RsSwitch' }, ].map((item) => { return { title: item.label, value: item.value, }; }), }, }; }, extraProps: { setValue: (target: IPublicModelSettingField, value: any) => { allUniqueProps.forEach((item) => { if (!commonDefaultProps.includes(item) && !compDefaultProps[value]?.includes(item)) { target.parent.setPropValue(item, null); } else if (!target.parent.getPropValue(item) && defaultItem[item]) { target.parent.setPropValue(item, defaultItem[item]); } }); }, }, }, { name: 'hasLabel', defaultValue: true, title: { label: { type: 'i18n', zh_CN: '显示标题', en_US: 'hasLabel', }, tip: { type: 'i18n', zh_CN: '属性: hasLabel | 说明: 是否显示标题', en_US: 'prop: hasLabel | description: hasLabel', }, }, setter: 'BoolSetter', condition: () => false, }, { name: 'showTip', defaultValue: false, title: { label: { type: 'i18n', zh_CN: '标题提示', en_US: 'showTip', }, tip: { type: 'i18n', zh_CN: '属性: showTip | 说明: 是否有标题提示', en_US: 'prop: showTip | description: show tip', }, }, setter: 'BoolSetter', }, { name: 'tooltip', title: { label: { type: 'i18n', zh_CN: '提示内容', en_US: 'tooltip', }, tip: { type: 'i18n', zh_CN: '属性: tooltip | 说明: 标题提示内容', en_US: 'prop: tooltip | description: tooltip', }, }, setter: 'StringSetter', condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('showTip'); }, }, { name: 'placeholder', title: { label: { type: 'i18n', zh_CN: '占位提示', en_US: 'Placeholder', }, tip: { type: 'i18n', zh_CN: '属性: placeholder | 说明: 占位提示', en_US: 'prop: placeholder | description: placeholder', }, }, defaultValue: '请输入', setter: 'StringSetter', }, { ...stateProps, extraProps: { setValue: (target: IPublicModelSettingField, value: string) => { if (value === 'disabled') { target.parent.setPropValue('disabled', true); } else { target.parent.setPropValue('disabled', false); } }, }, }, { name: 'type', title: { label: { type: 'i18n', zh_CN: '类型', en_US: 'type', }, tip: { type: 'i18n', zh_CN: '属性: type | 说明: 标题位置\n@enumdesc 单行、多行', en_US: 'prop: type | description: labelAlign \n@enumdesc text、textarea', }, }, defaultValue: 'text', setter: { componentName: 'RadioGroupSetter', initialValue: 'text', mode: 'single', props: { options: [ { value: 'text', title: '单行', }, { value: 'search', title: '搜索', }, ], }, }, condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsInput'; }, }, { name: 'showCount', defaultValue: false, condition: (target: IPublicModelSettingField) => { return ( target.parent.getPropValue('componentName') === 'RsInput' && ['text', 'textarea'].includes(target.parent.getPropValue('type')) ); }, title: { label: { type: 'i18n', zh_CN: '展示字数', en_US: 'showCount', }, tip: { type: 'i18n', zh_CN: '属性: showCount | 说明: 展示字数', en_US: 'prop: showCount | description: showCount', }, }, setter: 'BoolSetter', }, { name: 'allowClear', defaultValue: true, title: { label: { type: 'i18n', zh_CN: '显示清除', en_US: 'allowClear', }, tip: { type: 'i18n', zh_CN: '属性: allowClear | 说明: 是否出现清除按钮', en_US: 'prop: allowClear | description: show clear icon', }, }, setter: 'BoolSetter', condition: (target: IPublicModelSettingField) => { return ['RsInput', 'RsDatePicker'].includes(target.parent.getPropValue('componentName')); }, }, ...inputNumberFormatProps .map((item: Record) => { return { ...item, condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsInputNumber'; }, }; }) .filter(Boolean), ...defaultValueProps(), { name: 'format', title: { label: '日期格式', }, defaultValue: 'YYYY-MM-DD', setter: { componentName: SelectSetter, initialValue: 'YYYY-MM-DD', props: { mode: 'single', options: [ { label: 'YYYY', value: 'YYYY', }, { label: 'YYYY-MM', value: 'YYYY-MM', }, { label: 'YYYY-MM-DD', value: 'YYYY-MM-DD', }, { label: 'YYYY-MM-DD HH:mm', value: 'YYYY-MM-DD HH:mm', }, { label: 'YYYY-MM-DD HH:mm:ss', value: 'YYYY-MM-DD HH:mm:ss', }, ], }, }, condition: (target: IPublicModelSettingField) => { return ['RsDatePicker', 'RsRangePicker'].includes( target.parent.getPropValue('componentName'), ); }, }, { name: 'returnType', title: { label: '返回类型', tip: 'returnType | 返回值类型', }, defaultValue: 'string', setter: { componentName: RadioGroupSetter, initialValue: 'string', props: { options: [ { label: '字符串', value: 'string', }, { label: '时间戳', value: 'timestamp', }, ], }, }, condition: (target: IPublicModelSettingField) => { return ['RsDatePicker', 'RsRangePicker'].includes( target.parent.getPropValue('componentName'), ); }, }, { name: 'selectableDate', title: { label: '可选择时间', tip: 'selectableDate | 可选择的时间范围', }, defaultValue: 'all', setter: { componentName: SelectSetter, initialValue: 'all', props: { options: [ { label: '无限制', value: 'all', }, { label: '今天之后(含今天)', value: 'sinceToday', }, { label: '今天之前(含今天)', value: 'beforeToday', }, { label: '不可选区间', value: 'disabledRange', }, ], }, }, condition: (target: IPublicModelSettingField) => { return ['RsDatePicker', 'RsRangePicker'].includes( target.parent.getPropValue('componentName'), ); }, }, { name: 'rangeStart', title: { label: '开始日期', tip: '属性 rangeStart', }, condition: (target: IPublicModelSettingField) => { return ( ['RsDatePicker', 'RsRangePicker'].includes(target.parent.getPropValue('componentName')) && target.parent.getPropValue('selectableDate') === 'disabledRange' ); }, setter: DateSetter, }, { name: 'rangeEnd', title: { label: '结束日期', tip: '属性 rangeEnd', }, condition: (target: IPublicModelSettingField) => { return ( ['RsDatePicker', 'RsRangePicker'].includes(target.parent.getPropValue('componentName')) && target.parent.getPropValue('selectableDate') === 'disabledRange' ); }, setter: DateSetter, }, { name: 'splitFields', title: { label: '拆分字段', tip: 'splitFields | 是否将日期范围拆分为开始和结束两个字段', }, propType: 'bool', defaultValue: false, setter: 'BoolSetter', condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsRangePicker'; }, }, { name: 'startFieldName', title: { label: '开始日期字段', tip: 'startFieldName | 开始日期的字段名称', }, propType: 'string', setter: 'StringSetter', condition: (target: IPublicModelSettingField) => { return ( target.parent.getPropValue('componentName') === 'RsRangePicker' && target.parent.getPropValue('splitFields') === true ); }, }, { name: 'endFieldName', title: { label: '结束日期字段', tip: 'endFieldName | 结束日期的字段名称', }, propType: 'string', setter: 'StringSetter', condition: (target: IPublicModelSettingField) => { return ( target.parent.getPropValue('componentName') === 'RsRangePicker' && target.parent.getPropValue('splitFields') === true ); }, }, { name: 'presets', title: { label: '快捷方式', tip: 'presets | 显示预设时间范围快捷选择', }, defaultValue: true, setter: 'BoolSetter', condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsRangePicker'; }, }, { name: 'allowClear', title: { label: '清除按钮开关', tip: '默认打开' }, propType: 'bool', defaultValue: true, setter: 'BoolSetter', condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsSelect'; }, }, { name: 'showSearch', title: { label: '可搜索开关', tip: '默认打开' }, propType: 'bool', defaultValue: true, setter: 'BoolSetter', condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsSelect'; }, }, { name: 'mode', title: { label: '类型' }, defaultValue: 'single', setter: { componentName: 'RadioGroupSetter', initialValue: 'single', props: { options: [ { title: '单选', value: 'single', }, { title: '多选', value: 'multiple', }, ], }, }, condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsSelect'; }, }, { name: 'typeOptions', title: { label: '选项类型', }, defaultValue: 'custom', setter: { componentName: 'SelectSetter', initialValue: 'custom', props: { options: [ { label: '固定值', value: 'custom', disabled: false, }, ], }, }, condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsSelect'; }, }, { ...selectOptionsProp, condition: (target: IPublicModelSettingField) => { return ( target.parent.getPropValue('componentName') === 'RsSelect' && target.parent.getPropValue('typeOptions') === 'custom' ); }, }, ...telescopeBasicProps .filter((item) => item.name !== 'isBackfillToMutilRecord') .map((item: any) => ({ ...item, condition: (target: IPublicModelSettingField) => { const dataType = target.parent.getPropValue('componentName'); if (item.name === 'pageSize') { return dataType === 'RsTelescope' && target.parent.getPropValue('isPagination'); } return dataType === 'RsTelescope'; }, })), { ...telescopeQueryProps(Rs_QUERY_FILTER), condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsTelescope'; }, }, { ...telescopeTableProps(Rs_QUERY_FILTER), condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsTelescope'; }, }, { ...telescopeTreeProps, items: [ ...telescopeTreeProps.items.map((item) => { if (item.name === 'showTree') return { ...item, defaultValue: false }; return { ...item, condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('showTree'); }, }; }), ], condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsTelescope'; }, }, ...switchTypeProps.map((item: Record) => { if (item.name === 'switchType') { return { ...item, defaultValue: 'switch', title: { label: '开关类型', tip: '开关类型' }, condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsSwitch'; }, }; } if (item.name === 'switchValueType') { return { ...item, defaultValue: '0', condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsSwitch'; }, }; } return item; }), { name: 'checked', title: { label: '是否选中', tip: '当前是否选中' }, propType: 'bool', defaultValue: true, setter: 'BoolSetter', supportVariable: true, condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsSwitch'; }, }, { name: 'autoFocus', title: { label: '自动聚焦', tip: '组件自动获取焦点' }, propType: 'bool', defaultValue: false, setter: 'BoolSetter', condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsSwitch'; }, }, { name: 'checkedChildren', title: { label: '选中时内容', tip: '选中时的内容' }, propType: 'string', setter: 'StringSetter', condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsSwitch'; }, }, { name: 'unCheckedChildren', title: { label: '非选中时内容', tip: '非选中时的内容' }, propType: 'string', setter: 'StringSetter', condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsSwitch'; }, }, { name: 'loading', title: { label: '加载中', tip: '加载中' }, propType: 'bool', defaultValue: false, setter: 'BoolSetter', condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsSwitch'; }, }, { name: 'size', title: { label: '尺寸', tip: '开关大小' }, propType: { type: 'oneOf', value: ['default', 'small'] }, setter: { componentName: 'RadioGroupSetter', props: { options: [ { title: '默认', value: 'default', }, { title: '小', value: 'small', }, ], }, initialValue: 'default', }, defaultValue: 'default', condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsSwitch'; }, }, { title: '校验', display: 'block', type: 'group', items: [ { name: 'required', title: { label: { type: 'i18n', zh_CN: '必填', en_US: 'Required' }, }, setter: { componentName: ValidationSetter, }, condition: (target: IPublicModelSettingField) => { return !['RsSwitch'].includes(target.parent.getPropValue('componentName')); }, }, { name: 'maxLength', title: { label: { type: 'i18n', zh_CN: '最大长度', en_US: 'Max Length' }, }, setter: { componentName: ValidationSetter, props: { type: ValidationType.MAX_LENGTH, }, }, condition: (target: IPublicModelSettingField) => { return !['RsInputNumber', 'RsDatePicker', 'RsSelect', 'RsTelescope', 'RsSwitch'].includes( target.parent.getPropValue('componentName'), ); }, }, { name: 'minValue', title: { label: { type: 'i18n', zh_CN: '最小值', en_US: 'Min Value' }, }, setter: { componentName: ValidationSetter, props: { type: ValidationType.MIN_VALUE, }, }, condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsInputNumber'; }, }, { name: 'maxValue', title: { label: { type: 'i18n', zh_CN: '最大值', en_US: 'Max Value' }, }, setter: { componentName: ValidationSetter, props: { type: ValidationType.MAX_VALUE, }, }, condition: (target: IPublicModelSettingField) => { return target.parent.getPropValue('componentName') === 'RsInputNumber'; }, }, { name: 'customFunction', title: { label: { type: 'i18n', zh_CN: '自定义函数', en_US: 'Custom Function' }, tip: { type: 'i18n', zh_CN: 'customFunction | 自定义函数', en_US: 'customFunction | Custom Function', }, }, propType: 'func', setter: { componentName: ValidationSetter, props: { type: ValidationType.CUSTOM_FUNCTION, }, }, }, ], }, ]; const getAllDefaultValue = (props: any[]): any => { const initProps: any = {}; const getDefaultValue = (items: any[]) => { items.forEach((item: any) => { if (item.type === 'group' && Array.isArray(item.items)) { getDefaultValue(item.items); } else if (item.name && item.defaultValue !== undefined) { if (typeof item.defaultValue === 'function') { initProps[item.name] = item.defaultValue(); } else { initProps[item.name] = item.defaultValue; } } }); }; getDefaultValue(props); return initProps; }; const getAllDefaultItems = () => { const initProps = getAllDefaultValue(baseFormItemProps); const newComponentProps = getInitialPropsForFormItem('RsInput'); return { componentName: 'RsInput', ...newComponentProps, ...initProps, }; }; // const getUniqueDefaultItems = (componentsName: string, defaultItem: Record) => { // // 获取所有需要的属性名称 // const neededProps = new Set([...commonDefaultProps, ...(compDefaultProps[componentsName] || [])]); // // 从 defaultItem 中筛选出需要的属性 // const result: Record = {}; // Object.entries(defaultItem).forEach(([key, value]) => { // if (neededProps.has(key)) { // result[key] = value; // } // }); // return result; // }; const defaultItem = getAllDefaultItems() || {}; // const defaultRsInputItem = getUniqueDefaultItems('RsInput', defaultItem); export const formItemsProps = { name: 'items', title: '查询项', display: 'accordion', virtual: true, setter: { componentName: RsArraySetter, initialValue: () => { const uuid1 = uuid(); const uuid2 = uuid(); return [ { ...defaultItem, rs_id: uuid1, key: uuid1, }, { ...defaultItem, rs_id: uuid2, key: uuid2, }, ]; }, props: { itemSetter: { componentName: RsObjectSetter, props: { config: { items: [...baseFormItemProps], }, }, initialValue: () => { return { ...defaultItem }; }, }, }, }, };