import React, { Component, useState, useEffect } from 'react'; import { Button, Checkbox, Radio, Overlay, Icon, Grid, Switch, Input, NumberPicker, } from '@alifd/next'; import { EditSvg, DeleteSvg, DragSvg } from './images/svg'; import { SortListDndKit, SortItemDndKit } from './dnd-drag'; import './index.scss'; const { Popup } = Overlay; const { Row, Col } = Grid; interface ICustomOptionsSetterProps { value: any[]; items: any[]; multiple: boolean; // 多选 单选 labelKey?: string; valueKey?: string; defaultValue?: any[]; initialValue: object | (() => object); onChange: (value: any[]) => void; isRsSelect?: boolean; selected?: any; field?: any; } const leftSpan = 6; const rightSpan = 18; const CustomOptionsSetter = (props: ICustomOptionsSetterProps) => { const { onChange, defaultValue, value, initialValue, multiple, items, labelKey = 'label', valueKey = 'value', selected, field: fieldProps, isRsSelect, } = props; const [visibles, setVisibles] = useState(new Array(99999).fill(false)); const [options, setOptions] = useState([]); // 布局方式 useEffect(() => { const originValue = value || defaultValue; let initValue: any[] = []; if (originValue) { initValue = Array.isArray(originValue) ? originValue : [originValue]; } setOptions(initValue); onChange(initValue); }, []); const handleValueChange = ( field: string, val: number | string | boolean | undefined, idx: number, ) => { let newInfo = [...options]; if ( field === 'checked' && (!multiple || (isRsSelect && (selected?.getPropValue('mode') === 'single' || fieldProps?.parent?.getPropValue('mode') === 'single'))) ) { newInfo = newInfo.map((t) => { return { ...t, [field]: false, }; }); } newInfo[idx][field] = val; setOptions(newInfo); onChange(newInfo); }; const handleAddClick = () => { const initValue = typeof initialValue === 'function' ? initialValue(options) : initialValue; const newOptions = [...options, initValue || {}]; handelEdit(newOptions.length - 1); handleChange(newOptions); }; const handleChange = (list: any) => { setOptions(list); onChange(list); }; const handleDelete = (idx: number) => { options.splice(idx, 1); handleChange([...options]); }; const handelEdit = (idx: number) => { const list = [...visibles]; list[idx] = true; setVisibles(list); }; const changePosition = (list: any) => { console.log('list', list); handleChange(list); }; const renderEdit = (option: any, idx: number) => { return ( e.stopPropagation()} onClick={() => { handelEdit(idx); }} > {EditSvg} } triggerType="click" onVisibleChange={(visible: boolean, type: string | object) => { const list = [...visibles]; list[idx] = visible; setVisibles(list); }} >
{ const list = [...visibles]; list[idx] = false; setVisibles(list); }} /> {items.map((item) => { return ( {item.title} {item.type === 'string' && ( handleValueChange(item.name, e, idx)} /> )} {item.type === 'number' && ( handleValueChange(item.name, value, idx)} /> )} {item.type === 'boolean' && ( handleValueChange(item.name, e, idx)} /> )} ); })}
); }; const renerOptions = () => { return options.map((option, idx) => { return ( ( <> {items.some((item) => item.name === 'checked') && (
handleValueChange('checked', val, idx)} />
)}
{option[labelKey]}
{renderEdit(option, idx)} e.stopPropagation()} onClick={() => { handleDelete(idx); }} > {DeleteSvg}
)} />
); }); }; return (
{renerOptions()}
); }; interface IItemProps { key: string; attributes: any; listeners: any; renderDom: any; } const Item = ({ key, attributes, listeners, renderDom }: IItemProps) => { return (
{DragSvg} {renderDom()}
); }; // 因为下面这个问题,setter必须使用class组件 export default class extends Component { static displayName = 'CustomOptionsSetter'; render() { return ; } }