Merge pull request #5756 from sqzhou/combo-additem

feat:combo组件添加addItem动作
This commit is contained in:
hsm-lv 2022-11-15 11:10:41 +08:00 committed by GitHub
commit 2272fa46d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -921,6 +921,7 @@ combo 还有一个作用是增加层级,比如返回的数据是一个深层
| 动作名称 | 动作配置 | 说明 |
| -------- | --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| addItem | `item: object` 新增项的值 | 只有开启`multiple`模式才能使用, `multiple`模式下,给新增项添加默认值 |
| clear | - | 清空 |
| reset | - | 将值重置为`resetValue`,若没有配置`resetValue`,则清空 |
| setValue | `value: object \| Array<object>` 更新的值<br/>`index?: number` 指定更新的数据索引, 1.10.1 及以上版本引入 | 更新数据,对象数组针对开启`multiple`模式, `multiple`模式下可以通过指定`index`来更新指定索引的数据 |

View File

@ -447,13 +447,48 @@ export default class ComboControl extends React.Component<ComboProps> {
const actionType = action?.actionType as string;
const {onChange, resetValue} = this.props;
if (actionType === 'clear') {
if (actionType === 'addItem') {
this.addItemValue(args?.item ?? {});
}
else if (actionType === 'clear') {
onChange('');
} else if (actionType === 'reset') {
onChange(resetValue ?? '');
}
}
addItemValue(itemValue: any) {
const {
flat,
joinValues,
addattop,
delimiter,
disabled,
submitOnChange
} = this.props;
if (disabled) {
return;
}
let value = this.getValueAsArray();
this.keys.push(guid());
if (addattop === true) {
value.unshift(itemValue);
}
else {
value.push(itemValue);
}
if (flat && joinValues) {
value = value.join(delimiter || ',');
}
this.props.onChange(value, submitOnChange, true);
}
getValueAsArray(props = this.props) {
const {flat, joinValues, delimiter, type} = props;
let value = props.value;