import SealInput from '@/components/seal-form/seal-input'; import { Global } from '@/config/global'; import { MinusOutlined } from '@ant-design/icons'; import { useIntl } from '@umijs/max'; import { Button, Tooltip } from 'antd'; import _ from 'lodash'; import React, { useState } from 'react'; import './styles/label-item.less'; interface LabelItemProps { label: { key: string; value: string; }; labelKey?: string; labelValue?: string; keyAddon?: React.ReactNode; valueAddon?: React.ReactNode; seperator?: string; labelList: { key: string; value: string }[]; disabled?: boolean; onDelete?: () => void; onChange?: (params: { key: string; value: string }) => void; onPaste?: (e: any) => void; onBlur?: (e: any, type: string) => void; } const LabelItem: React.FC = ({ label, labelList, seperator, keyAddon, valueAddon, disabled, onChange, onDelete, onPaste, onBlur }) => { const intl = useIntl(); const [open, setOpen] = useState(false); const handleOnValueChange = (e: any) => { const value = e.target.value; onChange?.({ key: label.key, value: value }); }; const handleOnKeyChange = (e: any) => { const key = e.target.value; onChange?.({ key, value: label.value }); }; const handleKeyOnBlur = (e: any, type: string) => { const val = e.target.value; // has duplicate key const duplicates = _.filter( labelList, (item: Global.BaseListItem) => val && val === item.key ); if (duplicates.length > 1) { setOpen(true); onChange?.({ key: '', value: label.value }); setTimeout(() => { setOpen(false); }, 1000); } else { setOpen(false); } onBlur?.(e, type); }; return (
{keyAddon ?? ( handleKeyOnBlur(e, 'key')} onPaste={onPaste} > )}
{seperator && {seperator}}
{valueAddon ?? ( onBlur?.(e, 'value')} > )}
{!disabled && ( )}
); }; export default LabelItem;