From 98628032c4f370ab162fd2fce0027c1493943550 Mon Sep 17 00:00:00 2001 From: zjt Date: Wed, 6 Apr 2022 16:27:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AF=A6=E6=83=85=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8E=A5=E5=8F=A3=E4=B8=8B=E5=8F=91=E9=80=89?= =?UTF-8?q?=E9=A1=B9=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/detail/enum/index.tsx | 129 +++++++++++++++++++++++---- src/components/detail/text/index.tsx | 2 +- 2 files changed, 115 insertions(+), 16 deletions(-) diff --git a/src/components/detail/enum/index.tsx b/src/components/detail/enum/index.tsx index 658ff32..9ebfa19 100644 --- a/src/components/detail/enum/index.tsx +++ b/src/components/detail/enum/index.tsx @@ -1,11 +1,13 @@ import { config } from 'process' import React from 'react' -import { DetailField, DetailFieldConfig, DetailFieldError, DetailFieldProps, IDetailField } from '../common' +import { DetailField, DetailFieldProps, DetailFieldConfig, IDetailField } from '../common' +import InterfaceHelper, { InterfaceConfig } from '../../../util/interface' +import { getValue } from '../../../util/value' export interface EnumDetailConfig extends DetailFieldConfig { type: 'detail_enum' multiple: boolean | ArrayMultipleConfig | SplitMultipleConfig - options: ManualOptionsConfig + options: ManualOptionsConfig | InterfaceOptionsConfig } interface ArrayMultipleConfig { @@ -28,24 +30,53 @@ interface ManualOptionsConfig { getValue?: string } +export interface InterfaceOptionsConfig { + from: 'interface' + interface?: InterfaceConfig + format?: InterfaceOptionsListConfig | InterfaceOptionKVConfig +} + +export interface InterfaceOptionKVConfig { + type: 'kv' +} +export interface InterfaceOptionsListConfig { + type: 'list' + keyField: string + labelField: string +} + + export interface IEnumProps { value?: string | string[] } export default class EnumDetail extends DetailField implements IDetailField { + interfaceHelper = new InterfaceHelper() + reset: () => Promise = async () => { const defaults = await this.defaultValue() return (defaults === undefined) ? '/' : defaults } + state = { + value: '' + } + + constructor(props: DetailFieldProps) { + super(props) + } + renderComponent = (props: IEnumProps) => { return 您当前使用的UI版本没有实现EnumDetail组件。 } - - getValue = () => { + componentDidMount() { + this.getValue() + } + + getValue = async () => { const { value, config: { @@ -55,7 +86,17 @@ export default class EnumDetail extends DetailField option.value === value) - return option ? option.label : value.toString() + this.setState({ + value: option ? option.label : value.toString() + }) } else if (multiple === true || multiple.type) { if (Array.isArray(theValue)) { - return theValue.map((item) => { - const option = options.data.find((option) => { - return option.value === Number(item) - }) - return option ? option.label : item.toString() - }).join(',') + this.setState({ + value: theValue.map((item) => { + const option = options.data.find((option) => { + return option.value === Number(item) + }) + return option ? option.label : item.toString() + }).join(',') + }) } else { - return '-' + this.setState({ + value: '-' + }) } } + } else { + this.setState({ + value: value + }) + } + } else if (options && options.from === 'interface') { + if (options.interface) { + this.interfaceHelper.request( + options.interface, + {}, + { record: this.props.record, data: this.props.data, step: this.props.step }, + { loadDomain: this.props.loadDomain } + ).then((data) => { + + if (options.format) { + type OptionItem = { value: string, label: string } + let tempOptions: Array = [] + if (options.format.type === 'kv') { + tempOptions = Object.keys(data).map((key) => ({ + value: key, + label: data[key] + })) + this.setState({ + value: theValue.map((item: OptionItem) => { + const option = tempOptions.find((option) => { + return option.value === String(item) + }) + return option ? option.label : item.toString() + }).join(',') + }) + } else if (options.format.type === 'list') { + tempOptions = data.map((item: any) => { + if (options.format && options.format.type === 'list') { + return ({ + value: getValue(item, options.format.keyField), + label: getValue(item, options.format.labelField) + }) + } + }) + this.setState({ + value: theValue.map((item: OptionItem) => { + const option = tempOptions.find((option) => { + return option.value === String(item) + }) + return option ? option.label : item.toString() + }).join(',') + }) + } + } + }) } else { return value } - } else { + } + else { return value } } render = () => { - const value = this.getValue() + // const value = this.getValue() + const { value } = this.state return ( {this.renderComponent({ diff --git a/src/components/detail/text/index.tsx b/src/components/detail/text/index.tsx index 15192b8..a3de7db 100644 --- a/src/components/detail/text/index.tsx +++ b/src/components/detail/text/index.tsx @@ -7,7 +7,7 @@ export interface TextFieldConfig extends DetailFieldConfig { } export interface ITextField { - value: string + value: string | Array } export default class TextField extends DetailField implements IDetailField { -- Gitee