diff --git a/src/components/detail/custom/index.tsx b/src/components/detail/custom/index.tsx new file mode 100644 index 0000000000000000000000000000000000000000..19a582f4153ec1445ca969e4a1a5367d8568ae18 --- /dev/null +++ b/src/components/detail/custom/index.tsx @@ -0,0 +1,95 @@ +import React, { RefObject } from 'react' +import { DetailField, DetailFieldConfig, DetailFieldProps, IDetailField } from '../common' +import { loadMicroApp, MicroApp } from 'qiankun' +import moment from 'moment' +import { cloneDeep } from 'lodash' + +export interface CustomDetailConfig extends DetailFieldConfig { + type: 'custom' + entry: string +} + +export default class CustomDtail extends DetailField implements IDetailField { + identifier: string = '' + entry: string = '' + container: RefObject = React.createRef() + customField: MicroApp | null = null + _get: () => Promise = async () => this.props.value + + componentDidMount () { + this.loadCustomField(this.props.config.entry) + } + + getSnapshotBeforeUpdate () { + const snapshot: string[] = [] + if (this.entry !== this.props.config.entry) { + snapshot.push('entry') + } + return snapshot + } + + get = async (): Promise => { + return await this._get() + } + + bindGet = async (get: () => Promise): Promise => { + this._get = get + } + + componentDidUpdate (_: DetailFieldProps, __: {}, snapshot: string[]) { + if (snapshot.includes('entry')) { + this.loadCustomField(this.props.config.entry) + } else { + if (this.customField && this.customField.update) { + this.customField.update({ + value: this.props.value, + record: this.props.record, + data: cloneDeep(this.props.data), + step: this.props.step, + config: this.props.config, + detail: this.props.detail, + onChange: this.props.onChange, + onValueSet: this.props.onValueSet, + onValueUnset: this.props.onValueUnset, + onValueListAppend: this.props.onValueListAppend, + onValueListSplice: this.props.onValueListSplice, + base: this.props.baseRoute, + loadDomain: this.props.loadDomain + }) + } + } + } + + loadCustomField = (entry: string) => { + if (this.container.current && entry) { + this.entry = this.props.config.entry + this.identifier = `custom|${moment().format('x')}|${Math.floor(Math.random() * 1000)}` + this.customField = loadMicroApp({ + name: this.identifier, + entry, + container: this.container.current, + props: { + value: this.props.value, + record: this.props.record, + data: cloneDeep(this.props.data), + step: this.props.step, + config: this.props.config, + detail: this.props.detail, + onChange: this.props.onChange, + onValueSet: this.props.onValueSet, + onValueUnset: this.props.onValueUnset, + onValueListAppend: this.props.onValueListAppend, + onValueListSplice: this.props.onValueListSplice, + base: this.props.baseRoute, + loadDomain: this.props.loadDomain + } + }) + } + } + + render = () => { + return ( +
+ ) + } +} diff --git a/src/components/detail/index.tsx b/src/components/detail/index.tsx index 899730d1cb4e7c1ba90b701348dc1c61654f1e8f..b6eed93902da66fce27cadc9ee6e99d3617b1bde 100644 --- a/src/components/detail/index.tsx +++ b/src/components/detail/index.tsx @@ -2,7 +2,7 @@ import TextField, { TextFieldConfig } from './text' import EnumDetail, { EnumDetailConfig } from './enum' import StatementDetail, { StatementDetailConfig } from './statement' import ImageDetail, { ImageDetailConfig } from './image' - +import CustomDetail, { CustomDetailConfig } from './custom' import GroupField, { GroupFieldConfig } from './group' import ImportSubformField, { ImportSubformFieldConfig } from './importSubform' import InfoDetail, { InfoDetailConfig } from './detailInfo' @@ -21,7 +21,8 @@ export type DetailFieldConfigs = ImportSubformFieldConfig | InfoDetailConfig | ColorDetailConfig | - TableFieldConfig + TableFieldConfig | + CustomDetailConfig export type componentType = 'text' | @@ -32,7 +33,8 @@ export type componentType = 'import_subform' | 'detail_info' | 'detail_color' | - 'table' + 'table' | + 'custom' export default { group: GroupField, @@ -43,5 +45,6 @@ export default { statement: StatementDetail, detail_info: InfoDetail, detail_color: ColorDetail, - table: TableField + table: TableField, + custom: CustomDetail } diff --git a/src/components/formFields/tabs/index.tsx b/src/components/formFields/tabs/index.tsx index 169b9598f4ccf0bf4fd36c83ed9f13889e62d50a..083ba7752e9976c08e3b48341876ae4f0b8de1e4 100644 --- a/src/components/formFields/tabs/index.tsx +++ b/src/components/formFields/tabs/index.tsx @@ -128,7 +128,6 @@ export default class TabsField extends Field extends Field 0) { const errTips = `${this.props.config.label || ''}子项中存在错误。\n ${childrenErrorMsg.map(err => `${err.name}:${err.msg}`).join('; ')}` errors.push(new FieldError(errTips)) diff --git a/src/index.tsx b/src/index.tsx index 20bdfa15924f589d0f5fd8cd1066d73de328f894..e66ae3d778d7051a2016ed4b2affad561deb6bc9 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -65,6 +65,7 @@ export { default as DetailImportSubformField } from './components/detail/importS export { default as DetailInfoField } from './components/detail/detailInfo' export { default as DetailColorField } from './components/detail/detailColor' export { default as DetailTableField } from './components/detail/table' +export { default as CustomDetail } from './components/detail/custom' export { default as HeaderStep } from './steps/header' diff --git a/src/steps/detail/index.tsx b/src/steps/detail/index.tsx index 1e294d64b0513954553297314f76221efc01fa66..5e9608273a8bd285a7862cdd4d3ca77b92cf637d 100644 --- a/src/steps/detail/index.tsx +++ b/src/steps/detail/index.tsx @@ -398,6 +398,7 @@ export default class DetailStep extends Step { value={detailFieldConfig.field !== undefined ? getValue(detailValue, detailFieldConfig.field) || detailFieldConfig.defaultValue : undefined} record={detailValue} data={cloneDeep(data)} + detail={this} step={step} config={detailFieldConfig} detail={this} diff --git a/src/steps/form/index.tsx b/src/steps/form/index.tsx index 47006bab228ba7dc61b52b1fe741e4bd20fa5cb3..df2917cfdc7381ffe53186715396d3806e2577cc 100644 --- a/src/steps/form/index.tsx +++ b/src/steps/form/index.tsx @@ -316,7 +316,6 @@ export default class FormStep extends Step { if (formField && formFieldConfig && !formFieldConfig.disabled) { const value = await formField.get() const validation = await formField.validate(value) - if (validation !== true) { console.warn('表单项中存在问题', value, formFieldConfig) this.formData[formFieldIndex] = { status: 'error', message: validation[0].message, name: formFieldConfig.label }