import * as React from 'react'; import { Input, Message } from '@alifd/next'; import { IPublicModelSettingField } from '@alilc/lowcode-types'; import './index.less'; interface RequiredSetterProps { isPreview?: boolean; hasBorder?: boolean; value: string; defaultValue: string; placeholder: string; onChange: (val: string) => void; onBlur: (val: React.FocusEvent) => void; allowClear?: boolean; field?: IPublicModelSettingField; } export default class RequiredSetter extends React.PureComponent { static displayName = 'RequiredSetter'; constructor(props: RequiredSetterProps) { super(props); } state = { status: '', }; onBlur = (e: React.FocusEvent) => { // 如果还需要执行从props传入的onBlur,可以在这里调用 // this.props?.onBlur(e); const value = e.target.value; const { field } = this.props; const label = this.getLabel(field); if (!value) { Message.show({ type: 'error', title: `必填项「${label}」不得为空`, // content: placeholder, hasMask: true, closeable: true, duration: 5000, // 0 表示一直显示, 默认 3000ms }); this.setState({ status: 'error', }); return; } else { this.setState({ status: 'success', }); } }; // 获取标签文本 label getLabel = (field?: IPublicModelSettingField) => { if (!field?.title) return ''; const title = field.title as any; // title: { label: '组件ID' } if (typeof title.label === 'string') { return title.label; } // title: { label: { type: 'i18n', zh_CN: '组件ID', en_US: 'id' } } if (title.label && typeof title.label === 'object') { return title.label.zh_CN || title.label.en_US || ''; } return ''; }; render() { const { onChange, placeholder, value, isPreview = false, hasBorder = true, field } = this.props; // console.log('🚀 >>> RequiredSetter >>> render >>> this.props:', this.props); const label = this.getLabel(field); // console.log('🚀 >>> RequiredSetter >>> render >>> label:', label); const { status } = this.state; return ( onChange(val)} onBlur={this.onBlur} style={{ width: '100%' }} state={status as 'error' | 'success' | 'warning' | 'loading'} /> ); } }