From 0c00d876d24a6835798d57ad3cef1f706f6926f7 Mon Sep 17 00:00:00 2001 From: liaoxuezhi Date: Tue, 22 Oct 2019 16:55:38 +0800 Subject: [PATCH] =?UTF-8?q?Picker=20=E5=88=9D=E5=A7=8B=E5=80=BC=E4=B8=AD?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E9=80=89=E9=A1=B9=E7=9A=84=E8=AF=9D,?= =?UTF-8?q?=E4=BB=8E=20source=20=E6=8E=A5=E5=8F=A3=E4=B8=AD=E6=8B=89?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderers/Form/Picker.tsx | 38 ++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/renderers/Form/Picker.tsx b/src/renderers/Form/Picker.tsx index 529580961..9585a66d0 100644 --- a/src/renderers/Form/Picker.tsx +++ b/src/renderers/Form/Picker.tsx @@ -4,7 +4,7 @@ import cx from 'classnames'; import Button from '../../components/Button'; import {SchemaNode, Schema, Action} from '../../types'; import find = require('lodash/find'); -import {anyChanged, autobind, getVariable, noop} from '../../utils/helper'; +import {anyChanged, autobind, getVariable, noop, createObject} from '../../utils/helper'; import findIndex = require('lodash/findIndex'); import Html from '../../components/Html'; import {filter} from '../../utils/tpl'; @@ -59,6 +59,10 @@ export default class PickerControl extends React.PureComponent input: React.RefObject = React.createRef(); + componentDidMount() { + this.fetchOptions(); + } + componentWillReceiveProps(nextProps: PickerProps) { const props = this.props; @@ -69,6 +73,38 @@ export default class PickerControl extends React.PureComponent } } + componentDidUpdate(prevProps: PickerProps) { + const props = this.props; + + if (props.value !== prevProps.value) { + this.fetchOptions(); + } + } + + fetchOptions() { + const {value, formItem, valueField, labelField, source, data} = this.props; + + if ( + !source || + !formItem || + !formItem.selectedOptions.length || + formItem.selectedOptions[0][valueField || 'value'] !== formItem.selectedOptions[0][labelField || 'label'] + ) { + return; + } + + formItem.loadOptions( + source, + createObject(data, { + value: value, + op: 'loadOptions' + }), + { + autoAppend: true + } + ); + } + buildSchema(props: PickerProps) { return { ...props.pickerSchema,