fix: transfer组件disabled的选项 在全选父级的时候,仍然可以被选到

This commit is contained in:
sqzhou 2023-07-17 10:49:20 +08:00
parent c893fdd667
commit 1f38fde1dc
2 changed files with 132 additions and 8 deletions

View File

@ -492,14 +492,37 @@ export class TreeSelector extends React.Component<
// 父级选中的时候,子节点也都选中,但是自己不选中
!~idx && children.length && value.pop();
while (children.length) {
let child = children.shift();
let index = value.indexOf(child);
// 取消下选择
if (
flattenTree(children)
.filter(item => !item?.disabled)
.some(v => ~value.indexOf(v))
) {
while (children.length) {
let child = children.shift();
let index = value.indexOf(child);
if (child.children && child.children.length) {
children.push.apply(children, child.children);
} else if (!~index && child.value !== 'undefined') {
value.push(child);
if (child.children && child.children.length) {
children.push.apply(children, child.children);
}
if (~index && children.value !== 'undefined' && !child.disabled) {
value.splice(index, 1);
}
}
} else {
while (children.length) {
let child = children.shift();
let index = value.indexOf(child);
if (child.children && child.children.length) {
children.push.apply(children, child.children);
} else if (
!~index &&
child.value !== 'undefined' &&
!child?.disabled
) {
value.push(child);
}
}
}
} else {
@ -557,7 +580,7 @@ export class TreeSelector extends React.Component<
while (children.length) {
let child = children.shift();
let index = value.indexOf(child);
if (~index) {
if (~index && !child?.disabled) {
value.splice(index, 1);
}
if (child.children && child.children.length) {

View File

@ -430,3 +430,104 @@ test('Tree: add child & cancel', async () => {
expect(!!container.querySelector('[icon="close"]')).toBeFalsy()
);
});
test('Tree: item disabled', async () => {
const onSubmit = jest.fn();
const {container, findByText, findByPlaceholderText} = render(
amisRender(
{
"type": "form",
"api": "/api/mock2/form/saveForm",
"body": [
{
"label": "树型展示",
"type": "transfer",
"name": "transfer",
"selectMode": "tree",
"searchable": true,
"options": [
{
"label": "法师",
"children": [
{
"label": "诸葛亮",
"value": "zhugeliang"
}
]
},
{
"label": "战士",
"children": [
{
"label": "曹操",
"value": "caocao"
},
{
"label": "曹操1",
"value": "caocao1",
"children": [
{
"label": "李白1",
"value": "libai1"
},
{
"label": "韩信1",
"value": "hanxin1"
},
{
"label": "云中君1",
"value": "yunzhongjun1"
}
]
},
{
"disabled": true,
"label": "钟无艳",
"value": "zhongwuyan"
}
]
},
{
"label": "打野",
"children": [
{
"label": "李白",
"value": "libai"
},
{
"label": "韩信",
"value": "hanxin"
},
{
"label": "云中君",
"value": "yunzhongjun"
}
]
}
]
}
]
},
{onSubmit},
makeEnv({})
)
);
const node = await findByText('战士');
const submitBtn = await findByText('提交');
fireEvent.click(node);
fireEvent.click(submitBtn);
await wait(100);
expect(onSubmit.mock.calls[0][0]).toEqual({
transfer: 'caocao,libai1,hanxin1,yunzhongjun1'
});
fireEvent.click(node);
fireEvent.click(submitBtn);
await wait(100);
expect(onSubmit.mock.calls[1][0]).toEqual({
transfer: ''
});
});