|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { DeleteOutlined, PlusSquareOutlined } from '@ant-design/icons'; |
| 3 | +import { Col, Input, Row, Space } from 'antd'; |
| 4 | + |
| 5 | +import './index.scss'; |
| 6 | + |
| 7 | +interface IComponentType { |
| 8 | + /** |
| 9 | + * 自定义组件类型 |
| 10 | + */ |
| 11 | + type: string; |
| 12 | + /** |
| 13 | + * 组件 |
| 14 | + */ |
| 15 | + Component: React.ComponentClass<any, any> | React.FC<any>; |
| 16 | +} |
| 17 | + |
| 18 | +interface ICustomItem extends IComponentType { |
| 19 | + /** |
| 20 | + * 自定义参数名 |
| 21 | + */ |
| 22 | + label: string; |
| 23 | + /** |
| 24 | + * 自定义参数值 |
| 25 | + */ |
| 26 | + value: string; |
| 27 | + /** |
| 28 | + * 自定义参数是否合法 |
| 29 | + */ |
| 30 | + status: boolean; |
| 31 | +} |
| 32 | + |
| 33 | +export type ICustomValue = Omit<ICustomItem, 'Component'>; |
| 34 | + |
| 35 | +interface ICustomParameterProps { |
| 36 | + /** |
| 37 | + * 自定义参数名-value对应的key名 |
| 38 | + */ |
| 39 | + labelKey?: string; |
| 40 | + /** |
| 41 | + * 自定义参数值-value对应的key名 |
| 42 | + */ |
| 43 | + valueKey?: string; |
| 44 | + /** |
| 45 | + * 已存在的keys |
| 46 | + */ |
| 47 | + existingKeys: string[]; |
| 48 | + /** |
| 49 | + * 自定义参数列表 |
| 50 | + */ |
| 51 | + value?: Record<string, string>[]; |
| 52 | + /** |
| 53 | + * 自定义参数改变触发函数 |
| 54 | + * @param value |
| 55 | + * @returns |
| 56 | + */ |
| 57 | + onChange?: (value: ICustomValue[]) => void; |
| 58 | +} |
| 59 | + |
| 60 | +const DEFAULT_FORM_ITEM: IComponentType = { |
| 61 | + type: 'INPUT', |
| 62 | + Component: Input, |
| 63 | +}; |
| 64 | + |
| 65 | +export default function CustomParameter({ |
| 66 | + labelKey = 'label', |
| 67 | + valueKey = 'value', |
| 68 | + existingKeys, |
| 69 | + value, |
| 70 | + onChange, |
| 71 | +}: ICustomParameterProps) { |
| 72 | + const [customParamRows, setCustomParamRows] = useState<ICustomItem[]>([]); |
| 73 | + |
| 74 | + const handleCustomParameterAdd = () => { |
| 75 | + const customItem: ICustomItem = { |
| 76 | + type: DEFAULT_FORM_ITEM.type, |
| 77 | + Component: DEFAULT_FORM_ITEM.Component, |
| 78 | + label: '', |
| 79 | + value: '', |
| 80 | + status: false, |
| 81 | + }; |
| 82 | + |
| 83 | + setCustomParamRows([...customParamRows, customItem]); |
| 84 | + }; |
| 85 | + |
| 86 | + const handleCustomParameterDelete = (index: number) => { |
| 87 | + const paramRows = customParamRows.filter((_, i) => i !== index); |
| 88 | + setCustomParamRows(paramRows); |
| 89 | + onChange?.( |
| 90 | + paramRows?.map((item) => ({ |
| 91 | + label: item.label, |
| 92 | + value: item.value, |
| 93 | + type: item.type, |
| 94 | + status: item.status, |
| 95 | + })) |
| 96 | + ); |
| 97 | + }; |
| 98 | + |
| 99 | + const handleIsSame = (item: ICustomItem) => |
| 100 | + existingKeys.includes(item.label) || customParamRows.filter((cIt) => cIt.label === item.label).length > 1; |
| 101 | + |
| 102 | + console.log(existingKeys); |
| 103 | + |
| 104 | + useEffect(() => { |
| 105 | + setCustomParamRows( |
| 106 | + value?.map((item) => { |
| 107 | + return { |
| 108 | + type: item.type || DEFAULT_FORM_ITEM.type, |
| 109 | + label: item[labelKey], |
| 110 | + value: item[valueKey], |
| 111 | + Component: DEFAULT_FORM_ITEM.Component, |
| 112 | + status: false, |
| 113 | + }; |
| 114 | + }) || [] |
| 115 | + ); |
| 116 | + }, []); |
| 117 | + |
| 118 | + return ( |
| 119 | + <div className="dtc-custom-parameter"> |
| 120 | + {customParamRows.map((item, index) => ( |
| 121 | + <Row className="dtc-custom-parameter__item" align={'middle'} key={index}> |
| 122 | + <Col span={8}> |
| 123 | + <Input |
| 124 | + value={item.label} |
| 125 | + style={{ |
| 126 | + width: 'calc(100% - 18px)', |
| 127 | + }} |
| 128 | + status={!item.label ? 'error' : ''} |
| 129 | + onChange={(e: any) => { |
| 130 | + const params = customParamRows.map((item, i) => { |
| 131 | + if (i === index) item.label = e.target.value; |
| 132 | + return item; |
| 133 | + }); |
| 134 | + setCustomParamRows(params); |
| 135 | + onChange?.( |
| 136 | + params?.map((item) => ({ |
| 137 | + label: item.label, |
| 138 | + value: item.value, |
| 139 | + type: item.type, |
| 140 | + status: Boolean(item.label && item.value && !handleIsSame(item)), |
| 141 | + })) |
| 142 | + ); |
| 143 | + }} |
| 144 | + /> |
| 145 | + <span className="dtc-custom-parameter__gap">:</span> |
| 146 | + </Col> |
| 147 | + <Col span={12}> |
| 148 | + <item.Component |
| 149 | + value={item.value} |
| 150 | + status={!item.value ? 'error' : ''} |
| 151 | + onChange={(e: any) => { |
| 152 | + const params = customParamRows.map((item, i) => { |
| 153 | + if (i === index) item.value = e.target.value; |
| 154 | + return item; |
| 155 | + }); |
| 156 | + setCustomParamRows(params); |
| 157 | + onChange?.( |
| 158 | + params?.map((item) => ({ |
| 159 | + label: item.label, |
| 160 | + value: item.value, |
| 161 | + type: item.type, |
| 162 | + status: Boolean(item.label && item.value && !handleIsSame(item)), |
| 163 | + })) |
| 164 | + ); |
| 165 | + }} |
| 166 | + /> |
| 167 | + </Col> |
| 168 | + <Col span={4}> |
| 169 | + <div className="dtc-custom-parameter__delete"> |
| 170 | + <Space> |
| 171 | + <DeleteOutlined |
| 172 | + style={{ fontSize: 18 }} |
| 173 | + onClick={() => handleCustomParameterDelete(index)} |
| 174 | + /> |
| 175 | + {handleIsSame(item) ? ( |
| 176 | + <span className="dtc-custom-parameter__exist">已存在</span> |
| 177 | + ) : null} |
| 178 | + </Space> |
| 179 | + </div> |
| 180 | + </Col> |
| 181 | + </Row> |
| 182 | + ))} |
| 183 | + <Col span={16} offset={8}> |
| 184 | + <Space className="dtc-custom-parameter__add" onClick={handleCustomParameterAdd}> |
| 185 | + <PlusSquareOutlined /> |
| 186 | + <span>{'添加自定义参数'}</span> |
| 187 | + </Space> |
| 188 | + </Col> |
| 189 | + </div> |
| 190 | + ); |
| 191 | +} |
0 commit comments