import React, { Component, useState, useEffect } from 'react'; import { Button, Overlay, Icon, Grid, Input, NumberPicker, Select } 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; const Option = Select.Option; interface IStatusOptionsSetterProps { value: any[]; items: any[]; 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; type valueProps = number | string | boolean | undefined | null; const StatusOptionsSetter = (props: IStatusOptionsSetterProps) => { const { defaultValue, value, initialValue, items, labelKey = 'label', valueKey = 'value', onChange, } = 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: valueProps, idx: number) => { let newInfo = [...options]; newInfo[idx][field] = val; setOptions(newInfo); onChange(newInfo); }; const handleAddClick = () => { const initValue = typeof initialValue === 'function' ? initialValue() : 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) => { 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 === 'select' && ( )} ); })}
); }; const renerOptions = () => { return options.map((option, idx) => { return ( ( <>
{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 = 'StatusOptionsSetter'; render() { return ; } }