import snippets from './snippets'; import { IPublicModelSettingField } from '@alilc/lowcode-types'; import { RequiredSetter, RsArraySetter, RsObjectSetter, RsStringSetter } from '../_setters'; import { isPageDesigner, isDev, uuid } from '../_utils/utils'; import { checkParentWhitelist } from '../_utils/nestingRules'; const { version } = require('../../package.json'); export default { snippets: snippets, group: '基础组件', componentName: 'RsCollapse', title: '折叠面板', category: '容器元素', priority: 25, // 用于描述组件在统一category中的排序 npm: { package: 'components-page', version, exportName: 'RsCollapse', main: 'src\\index.tsx', destructuring: true, subName: '', }, props: [ { name: 'rs_id', title: { label: '组件ID' }, propType: 'string', setter: { componentName: RequiredSetter, initialValue: () => 'collapse_' + uuid(), }, }, { name: 'bordered', title: { label: '显示边框', tip: '带边框风格的折叠面板' }, propType: 'bool', defaultValue: false, setter: 'BoolSetter', supportVariable: true, condition: () => false, extraProps: { setValue(target: IPublicModelSettingField, value: boolean): void { // 显示边框时关闭透明无边框 value && target.getProps().setPropValue('ghost', !value); }, }, }, { name: 'accordion', title: { label: '手风琴模式', tip: '手风琴模式' }, propType: 'bool', defaultValue: false, setter: 'BoolSetter', supportVariable: true, }, { name: 'collapsible', title: { label: '折叠触发区域', tip: '所有子面板是否可折叠或指定可折叠触发区域' }, setter: { componentName: 'RadioGroupSetter', props: { options: [ { title: '页眉', value: 'header' }, { title: '图标', value: 'icon' }, { title: '禁用', value: 'disabled' }, ], }, initialValue: 'header', }, propType: { type: 'oneOf', value: ['header', 'icon', 'disabled'], }, extraProps: { setValue(target: IPublicModelSettingField, value: string): void { value && target.getProps().setPropValue('collapsible', value); }, }, }, // { // name: 'expandIcon', // title: { label: '自定义切换图标', tip: '自定义切换图标' }, // propType: 'func', // }, { name: 'expandIconPosition', title: { label: '折叠图标位置', tip: '设置图标位置' }, setter: { componentName: 'RadioGroupSetter', props: { options: [ { title: '左侧', value: 'start' }, { title: '右侧', value: 'end' }, ], }, initialValue: 'end', }, propType: { type: 'oneOf', value: ['start', 'end'], }, }, { name: 'destroyOnHidden', title: { label: '隐藏时销毁', tip: '销毁折叠隐藏的面板' }, propType: 'bool', defaultValue: false, setter: 'BoolSetter', supportVariable: true, }, { name: 'ghost', title: { label: '透明无边框', tip: '使折叠面板透明且无边框' }, propType: 'bool', defaultValue: true, setter: 'BoolSetter', supportVariable: true, }, { name: 'collapses', title: '折叠项', setter: { componentName: RsArraySetter, initialValue: () => { return []; }, props: { itemSetter: { componentName: RsObjectSetter, props: { config: { items: [ { name: 'header', title: { label: '面板标题', tip: '面板标题' }, setter: RsStringSetter, propType: 'string', initialValue: '折叠项', isRequired: true, }, { name: 'key', title: { label: '面板ID', tip: '面板ID' }, propType: 'string', setter: RsStringSetter, initialValue: (val: string) => val || uuid(), isRequired: true, }, ], }, }, initialValue: () => { // collapses 默认值 return { key: uuid(), header: '折叠项', showArrow: true, collapsible: 'header', forceRender: false, }; }, }, }, }, extraProps: { getValue( target: IPublicModelSettingField, fieldValue: any, ): Array<{ key: string; header?: string; showArrow?: boolean; collapsible?: string; forceRender?: boolean; extra?: string; }> { const map = target?.node?.children?.map((child) => { const key = child.getPropValue('key') ? String(child.getPropValue('key')) : child.id; return { key, header: child.getPropValue('header'), showArrow: child.getPropValue('showArrow'), collapsible: child.getPropValue('collapsible'), forceRender: child.getPropValue('forceRender'), extra: child.getPropValue('extra'), }; }); return map || []; }, setValue( target: IPublicModelSettingField, value: Array<{ key: string; header?: string; showArrow?: boolean; collapsible?: string; forceRender?: boolean; extra?: string; }>, ): void { const { node } = target; const map: Record< string, { key: string; header?: string; showArrow?: boolean; collapsible?: string; forceRender?: boolean; extra?: string; } > = {}; if (!Array.isArray(value)) { value = []; } // 检测并处理重复的key值 const usedKeys = new Set(); const processedValue = value.map((item, index) => { const tabItem = Object.assign({}, item); let currentKey = item.key; // 如果key已经存在,为第二个及后续重复项生成新的key if (usedKeys.has(currentKey)) { currentKey = `${item.key}-${uuid().slice(0, 8)}`; tabItem.key = currentKey; } usedKeys.add(currentKey); return tabItem; }); processedValue.forEach((item) => { (map as Record)[item.key] = item; }); node?.children?.mergeChildren( (child) => { const key = String(child.getPropValue('key')); if (Object.hasOwnProperty.call(map, key)) { child.setPropValue('key', map[key].key); child.setPropValue('header', map[key].header); child.setPropValue('showArrow', map[key].showArrow); child.setPropValue('collapsible', map[key].collapsible); child.setPropValue('forceRender', map[key].forceRender); child.setPropValue('extra', map[key].extra); delete map[key]; return false; } return true; }, () => { const items = []; for (const key in map) { if (Object.hasOwnProperty.call(map, key)) { items.push({ componentName: 'RsCollapse.Panel', props: map[key], }); } } return items; }, (child1, child2) => { const a = processedValue.findIndex( (item) => String(item.key) === String(child1.getPropValue('key')), ); const b = processedValue.findIndex( (item) => String(item.key) === String(child2.getPropValue('key')), ); return a - b; }, ); }, }, }, { name: 'defaultActiveKey', title: { label: '默认展开面板ID', tip: '填写面板ID后,默认展开对应的面板' }, propType: { type: 'oneOfType', value: [ 'string', { type: 'arrayOf', value: 'string' }, 'number', { type: 'arrayOf', value: 'number' }, ], }, }, { name: 'activeKey', title: { label: '当前激活 tab 面板的 key', tip: '当前激活 tab 面板的 key', }, condition: () => false, propType: { type: 'oneOfType', value: [ 'string', { type: 'arrayOf', value: 'string' }, 'number', { type: 'arrayOf', value: 'number' }, ], }, }, ], configure: { supports: { style: true, events: [ { name: 'onChange', template: "onChange(${extParams}){\n\t\t// 切换面板的回调\n\t\tconsole.log('onChange');\n\t}\n", }, ], }, component: { isContainer: true, nestingRule: { childWhitelist: ['RsCollapse.Panel'], parentWhitelist: (testNode: IPublicModelSettingField) => { return checkParentWhitelist(testNode); }, }, }, }, };