|
121 | 121 | }
|
122 | 122 |
|
123 | 123 | function handleSelectedRecursion(
|
124 |
| - e: |
125 |
| - | CustomEvent<{ selected: boolean; uid: string }> |
126 |
| - | { detail: { selected: boolean; uid: string } }, |
| 124 | + e: CustomEvent<{ selected: boolean; uid: string; isLeaf: boolean; item: SubMenuType }>, |
127 | 125 | index: number
|
128 | 126 | ) {
|
129 | 127 | const { selected, uid } = e.detail;
|
|
138 | 136 |
|
139 | 137 | it.selected = !!it.selectedDeps.size;
|
140 | 138 |
|
| 139 | + // 重置当前点击层所在树的其他项选择状态 |
| 140 | + // Reset the selection status of other items in the tree where the current clicked layer is located |
| 141 | + if (!ctxProps.multiple && e.detail.isLeaf) { |
| 142 | + if (it.selected && itemsList[index]) { |
| 143 | + itemsList = cancelSelected(itemsList, it); |
| 144 | + if (hasSub(it)) { |
| 145 | + it.children = cancelSelected(it.children!, e.detail.item); |
| 146 | + } |
| 147 | + } |
| 148 | + if (level === 1) { |
| 149 | + // 到第一层时如果是单选,则遍历取消其他项的选择状态 |
| 150 | + // If it is a single selection at the first level, |
| 151 | + // traverse and cancel the selection status of other items |
| 152 | + if (itemsList[index]) { |
| 153 | + itemsList = cancelSelected(itemsList, it); |
| 154 | + } else { |
| 155 | + itemsList = cancelSelected(itemsList, e.detail.item); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +
|
141 | 160 | if (itemsList[index]) {
|
142 | 161 | itemsList[index] = it;
|
143 | 162 | } else {
|
144 | 163 | moreItem = it;
|
145 | 164 | }
|
146 | 165 | dispatch('selectedRecursion', {
|
147 | 166 | selected: it.selected,
|
148 |
| - uid: it.uid |
| 167 | + uid: it.uid, |
| 168 | + isLeaf: e.detail.isLeaf, |
| 169 | + item: it |
149 | 170 | });
|
150 | 171 | } else {
|
151 | 172 | dispatch('selectedRecursion', e.detail);
|
152 | 173 | }
|
| 174 | + e.detail.isLeaf && onSubItemSelect(index); |
| 175 | + } |
| 176 | +
|
| 177 | + function cancelSelected(list: SubMenuType[], it: SubMenuType) { |
| 178 | + return list.map((value) => { |
| 179 | + if (!ctxProps.multiple && value.uid !== it.uid && !isGroup(it)) { |
| 180 | + value.selected = false; |
| 181 | + value.selectedDeps && value.selectedDeps.clear(); |
| 182 | + menuCtx.syncUids(value.uid!, 'selected', 'delete'); |
| 183 | + menuCtx.syncSelectedItems(value, 'delete'); |
| 184 | + if (hasSub(value)) { |
| 185 | + value.children = cancelSelected(value.children!, it); |
| 186 | + } |
| 187 | + } |
| 188 | + return value; |
| 189 | + }); |
153 | 190 | }
|
154 | 191 |
|
155 | 192 | function setOpenAndSelectStatus(it: SubMenuType, list = itemsList, parentOpen?: boolean) {
|
156 | 193 | return list.map((value) => {
|
157 | 194 | if (value.uid === it.uid && !isGroup(it)) {
|
158 | 195 | // set selected
|
159 |
| - if (ctxProps.selectable) { |
160 |
| - value.selected = !value.selected; |
| 196 | + const resolveSelected = !value.selected; |
| 197 | + if (ctxProps.selectable && !hasSub(it)) { |
| 198 | + // 重置当前点击层其他项选择状态 |
| 199 | + // Reset the selection status of other items in the current click layer |
| 200 | + if (!ctxProps.multiple && resolveSelected) { |
| 201 | + list = cancelSelected(list, it); |
| 202 | +
|
| 203 | + if (moreItem.children && moreItem.children.length > 0) { |
| 204 | + moreItem.children = cancelSelected(moreItem.children, it); |
| 205 | + moreItem.selected = false; |
| 206 | + moreItem.selectedDeps && moreItem.selectedDeps.clear(); |
| 207 | + } |
| 208 | + } |
| 209 | + value.selected = resolveSelected; |
161 | 210 | }
|
162 | 211 |
|
163 | 212 | // set open
|
|
182 | 231 | */
|
183 | 232 | dispatch('selectedRecursion', {
|
184 | 233 | selected: value.selected,
|
185 |
| - uid: value.uid |
| 234 | + uid: value.uid, |
| 235 | + isLeaf: !hasSub(value), |
| 236 | + item: value |
186 | 237 | });
|
187 | 238 | }
|
188 | 239 | }
|
|
474 | 525 | };
|
475 | 526 | if (it.disabled || it.disabledParent) {
|
476 | 527 | basicCls = {
|
477 |
| - [`${prefixCls}-disabled`]: true, |
478 |
| - [`${prefixCls}-disabled__dark`]: isDark(it) |
| 528 | + [`${prefixCls}-disabled`]: level !== 1, |
| 529 | + [`${prefixCls}-disabled__dark`]: isDark(it), |
| 530 | + [`${prefixCls}-disabled-l1`]: level === 1 |
479 | 531 | };
|
480 | 532 | if (hasSub(it)) {
|
481 | 533 | it.children = it.children!.map((item) => {
|
|
580 | 632 |
|
581 | 633 | const resolveDisabledTooltip = (it: SubMenuType, isInlineCollapsed?: boolean) => {
|
582 | 634 | // 有子节点的不显示
|
| 635 | + // Nodes with child nodes are not displayed |
583 | 636 | if ((it.children && it.children.length > 0 && it.type !== 'group') || !isInlineCollapsed) {
|
584 | 637 | return true;
|
585 | 638 | // 收起时且非水平模式
|
| 639 | + // When folded and not in horizontal mode |
586 | 640 | } else {
|
587 | 641 | return false;
|
588 | 642 | }
|
|
591 | 645 | const resolveTitle = (it: SubMenuType, isInlineCollapsed?: boolean, isTooltip?: boolean) => {
|
592 | 646 | let res = '';
|
593 | 647 | // 有子节点的不显示
|
| 648 | + // Nodes with child nodes are not displayed |
594 | 649 | if (it.children && it.children.length > 0 && it.type !== 'group') {
|
595 | 650 | res = '';
|
596 | 651 | // 收起时且非水平模式
|
| 652 | + // Nodes with child nodes are not displayed |
597 | 653 | } else if (isInlineCollapsed && ctxProps.mode !== 'horizontal') {
|
598 | 654 | res = it.title || it.label || '';
|
599 | 655 | if (!isTooltip) {
|
600 | 656 | res = '';
|
601 | 657 | }
|
602 | 658 | // 收起时或水平模式无默认值,只展示 title
|
| 659 | + // When collapsed or in horizontal mode, |
| 660 | + // there is no default value and only the title is displayed. |
603 | 661 | } else if (!isInlineCollapsed || ctxProps.mode === 'horizontal') {
|
604 | 662 | res = it.title || '';
|
605 | 663 | }
|
|
629 | 687 | if (it.disabled || it.disabledParent || e.detail) return;
|
630 | 688 | itemsList = clearVerticalOpenStatus(it);
|
631 | 689 | }
|
| 690 | +
|
| 691 | + function onSubItemSelect(index: number) { |
| 692 | + if (level == 1 && !ctxProps.multiple) { |
| 693 | + popoverRef[index] && popoverRef[index].updateShow && popoverRef[index].updateShow(false); |
| 694 | + } |
| 695 | + } |
632 | 696 | </script>
|
633 | 697 |
|
634 | 698 | {#each itemsList as it, index (it.uid)}
|
|
903 | 967 | <svelte:fragment slot="triggerEl">
|
904 | 968 | {#if it.type !== 'divider'}
|
905 | 969 | <li
|
906 |
| - on:click={(e) => handleSelect(it, e)} |
| 970 | + on:click={(e) => handleSelect(it, e, index)} |
907 | 971 | aria-hidden="true"
|
908 | 972 | style:padding-left={`${getIndent(it, ctxProps.inlineCollapsed)}`}
|
909 | 973 | class={cnames(it)}
|
|
0 commit comments