fix: add disabledRemoveFunc prop in AddSelect component

Add disabledRemoveFunc prop in AddSelect component to handle if disable the remove button and edit item

Change-Id: I10dc6ebc3d55305ac4829677c0edd20a549a8662
This commit is contained in:
zhangke 2022-12-13 18:43:55 +08:00
parent 3ada93aae8
commit 124fdc3c2a

View File

@ -15,7 +15,7 @@
import React, { Component } from 'react';
import { Select, Button, Input } from 'antd';
import { PlusCircleFilled, MinusCircleFilled } from '@ant-design/icons';
import { isArray, isEqual, isEmpty } from 'lodash';
import { isArray, isEqual, isEmpty, isFunction } from 'lodash';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { generateId } from 'utils/index';
@ -37,6 +37,7 @@ export default class index extends Component {
initValue: PropTypes.array,
readonlyKeys: PropTypes.array,
disableEditKeys: PropTypes.array,
disabledRemoveFunc: PropTypes.func,
};
static defaultProps = {
@ -50,6 +51,7 @@ export default class index extends Component {
initValue: [],
readonlyKeys: [],
disableEditKeys: [],
disabledRemoveFunc: null,
};
constructor(props) {
@ -111,7 +113,7 @@ export default class index extends Component {
// eslint-disable-next-line no-shadow
canRemove = (index, item) => {
const isDisabledKey = this.checkDisabledKey(item);
const isDisabledKey = this.checkItemRemoveDisabled(item);
const { minCount } = this.props;
return index >= minCount && !isDisabledKey;
};
@ -155,6 +157,15 @@ export default class index extends Component {
return options;
};
checkItemRemoveDisabled = (item) => {
const { items = [] } = this.state;
const { disabledRemoveFunc } = this.props;
if (isFunction(disabledRemoveFunc)) {
return disabledRemoveFunc({ item, items });
}
return this.checkDisabledKey(item);
};
checkDisabledKey = (item) => {
const { key = '' } = item.value || {};
const { disableEditKeys = [] } = this.props;
@ -208,7 +219,7 @@ export default class index extends Component {
const ItemComponent = itemComponent;
const { key = '' } = item.value || {};
const keyReadonly = readonlyKeys.indexOf(key) >= 0;
const isDisabledKey = this.checkDisabledKey(item);
const isDisabledKey = this.checkItemRemoveDisabled(item);
return (
<ItemComponent
{...this.props}