diff --git a/src/renderers/Form/Combo.tsx b/src/renderers/Form/Combo.tsx index a0dbf3d2..f6839409 100644 --- a/src/renderers/Form/Combo.tsx +++ b/src/renderers/Form/Combo.tsx @@ -102,20 +102,22 @@ export default class ComboControl extends React.Component { componentWillMount() { const { store, + value, minLength, maxLength } = this.props; store.config({ minLength, - maxLength + maxLength, + length: this.getValueAsArray().length }); } componentWillReceiveProps(nextProps:ComboProps) { const props = this.props; - if (anyChanged(['minLength', 'maxLength'], props, nextProps)) { + if (anyChanged(['minLength', 'maxLength', 'value'], props, nextProps)) { const { store, minLength, @@ -124,11 +126,30 @@ export default class ComboControl extends React.Component { store.config({ minLength, - maxLength + maxLength, + length: this.getValueAsArray(nextProps).length }); } } + getValueAsArray(props = this.props) { + const { + flat, + joinValues, + delimiter, + } = props; + let value = props.value; + + if (joinValues && flat && typeof value === 'string') { + value = value.split(delimiter || ','); + } else if (!Array.isArray(value)) { + value = []; + } else { + value = value.concat(); + } + return value; + } + addItem() { const { flat, @@ -137,19 +158,12 @@ export default class ComboControl extends React.Component { scaffold, disabled } = this.props; - let value = this.props.value; if (disabled) { return; } - if (joinValues && flat && typeof value === 'string') { - value = value.split(delimiter || ','); - } else if (!Array.isArray(value)) { - value = []; - } else { - value = value.concat(); - } + let value = this.getValueAsArray(); value.push(flat ? scaffold || '' : { ...scaffold @@ -179,15 +193,7 @@ export default class ComboControl extends React.Component { return; } - let value = this.props.value; - - if (joinValues && flat && typeof value === 'string') { - value = value.split(delimiter || ','); - } else if (!Array.isArray(value)) { - value = []; - } else { - value = value.concat(); - } + let value = this.getValueAsArray(); if (deleteApi) { const ctx = createObject(data, value[key]); @@ -220,18 +226,15 @@ export default class ComboControl extends React.Component { flat, store, joinValues, - delimiter + delimiter, + disabled } = this.props; - let value = this.props.value; - if (joinValues && flat && typeof value === 'string') { - value = value.split(delimiter || ','); - } else if (!Array.isArray(value)) { - value = []; - } else { - value = value.concat(); + if (disabled) { + return; } + let value = this.getValueAsArray(); value[index] = flat ? values.flat : {...values}; if (flat && joinValues) { @@ -357,7 +360,8 @@ export default class ComboControl extends React.Component { formatValue(value:any) { const { - flat } = this.props; + flat + } = this.props; if (flat) { return { @@ -401,6 +405,10 @@ export default class ComboControl extends React.Component { value = value.split(delimiter || ','); } + const finnalRemovable = store.removable !== false // minLength ? + && !disabled // 控件自身是否禁用 + && removable !== false; // 是否可以删除 + return (
{ } if ( - !disabled // 控件自身是否禁用 - && removable !== false // 是否可以删除 + finnalRemovable && ( // 表达式判断单条是否可删除 !itemRemovableOn || evalExpression(itemRemovableOn, value) !== false diff --git a/src/store/combo.ts b/src/store/combo.ts index 2c4394c1..86870b7d 100644 --- a/src/store/combo.ts +++ b/src/store/combo.ts @@ -29,11 +29,12 @@ export const ComboStore = iRendererStore uniques: types.map(UniqueGroup), forms: types.array(types.reference(types.late(() => FormStore))), minLength: 0, - maxLength: 0 + maxLength: 0, + length: 0 }) .views(self => ({ get addable() { - if (self.maxLength && self.forms.length >= self.maxLength) { + if (self.maxLength && self.length >= self.maxLength) { return false; } @@ -61,7 +62,7 @@ export const ComboStore = iRendererStore }, get removable() { - if (self.minLength && self.minLength >= self.forms.length) { + if (self.minLength && self.minLength >= self.length) { return false; } @@ -69,9 +70,14 @@ export const ComboStore = iRendererStore } })) .actions(self => { - function config(setting:Partial) { + function config(setting: { + minLength?: number; + maxLength?: number; + length?: number; + }) { typeof setting.minLength !== 'undefined' && (self.minLength = setting.minLength); typeof setting.maxLength !== 'undefined' && (self.maxLength = setting.maxLength); + typeof setting.length !== 'undefined' && (self.length = setting.length); } function bindUniuqueItem(item:IFormItemStore) {