Skip to content

Commit 1cec3e7

Browse files
authored
docs: added KMenu component English document
docs: added KMenu component English document
2 parents 65519ee + 50e6eb0 commit 1cec3e7

File tree

19 files changed

+1030
-89
lines changed

19 files changed

+1030
-89
lines changed

components/Menu/__test__/__snapshots__/menu.spec.ts.snap

+3-3
Large diffs are not rendered by default.

components/Menu/__test__/fixture/open-change.svelte

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
mode="vertical"
8585
id="open_change_test_vertical"
8686
on:openChange={handleClick}
87+
multiple={true}
8788
triggerSubMenuAction="click"
8889
ctxKey="11"
8990
>
@@ -92,6 +93,7 @@
9293
<KMenu
9394
mode="inline"
9495
id="open_change_test_inline"
96+
multiple={true}
9597
triggerSubMenuAction="click"
9698
on:openChange={handleClick}
9799
ctxKey="22"
@@ -102,6 +104,7 @@
102104
mode="horizontal"
103105
id="open_change_test_horizontal"
104106
triggerSubMenuAction="click"
107+
multiple={true}
105108
on:openChange={handleClick}
106109
ctxKey="33"
107110
>

components/Menu/__test__/fixture/select.svelte

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
mode="vertical"
8585
id="select_test_vertical"
8686
on:deSelect={handleClick}
87+
multiple={true}
8788
on:select={handleClick}
8889
triggerSubMenuAction="click"
8990
ctxKey="11"
@@ -95,6 +96,7 @@
9596
id="select_test_inline"
9697
triggerSubMenuAction="click"
9798
on:deSelect={handleClick}
99+
multiple={true}
98100
on:select={handleClick}
99101
ctxKey="22"
100102
>
@@ -105,6 +107,7 @@
105107
id="select_test_horizontal"
106108
triggerSubMenuAction="click"
107109
on:deSelect={handleClick}
110+
multiple={true}
108111
on:select={handleClick}
109112
ctxKey="22"
110113
>

components/Menu/src/index.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
export let selectedUids: KMenuProps['selectedUids'] = [];
1616
export let openUids: KMenuProps['openUids'] = [];
1717
export let show: KMenuProps['show'] = true;
18-
export let multiple: KMenuProps['multiple'] = true;
18+
export let multiple: KMenuProps['multiple'] = false;
1919
export let selectable: KMenuProps['selectable'] = true;
2020
export let inlineCollapsed: KMenuProps['inlineCollapsed'] = false;
2121
export let ctxKey: KMenuProps['ctxKey'] = '';

components/Menu/src/item.svelte

+74-10
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@
121121
}
122122
123123
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 }>,
127125
index: number
128126
) {
129127
const { selected, uid } = e.detail;
@@ -138,26 +136,77 @@
138136
139137
it.selected = !!it.selectedDeps.size;
140138
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+
141160
if (itemsList[index]) {
142161
itemsList[index] = it;
143162
} else {
144163
moreItem = it;
145164
}
146165
dispatch('selectedRecursion', {
147166
selected: it.selected,
148-
uid: it.uid
167+
uid: it.uid,
168+
isLeaf: e.detail.isLeaf,
169+
item: it
149170
});
150171
} else {
151172
dispatch('selectedRecursion', e.detail);
152173
}
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+
});
153190
}
154191
155192
function setOpenAndSelectStatus(it: SubMenuType, list = itemsList, parentOpen?: boolean) {
156193
return list.map((value) => {
157194
if (value.uid === it.uid && !isGroup(it)) {
158195
// 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;
161210
}
162211
163212
// set open
@@ -182,7 +231,9 @@
182231
*/
183232
dispatch('selectedRecursion', {
184233
selected: value.selected,
185-
uid: value.uid
234+
uid: value.uid,
235+
isLeaf: !hasSub(value),
236+
item: value
186237
});
187238
}
188239
}
@@ -474,8 +525,9 @@
474525
};
475526
if (it.disabled || it.disabledParent) {
476527
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
479531
};
480532
if (hasSub(it)) {
481533
it.children = it.children!.map((item) => {
@@ -580,9 +632,11 @@
580632
581633
const resolveDisabledTooltip = (it: SubMenuType, isInlineCollapsed?: boolean) => {
582634
// 有子节点的不显示
635+
// Nodes with child nodes are not displayed
583636
if ((it.children && it.children.length > 0 && it.type !== 'group') || !isInlineCollapsed) {
584637
return true;
585638
// 收起时且非水平模式
639+
// When folded and not in horizontal mode
586640
} else {
587641
return false;
588642
}
@@ -591,15 +645,19 @@
591645
const resolveTitle = (it: SubMenuType, isInlineCollapsed?: boolean, isTooltip?: boolean) => {
592646
let res = '';
593647
// 有子节点的不显示
648+
// Nodes with child nodes are not displayed
594649
if (it.children && it.children.length > 0 && it.type !== 'group') {
595650
res = '';
596651
// 收起时且非水平模式
652+
// Nodes with child nodes are not displayed
597653
} else if (isInlineCollapsed && ctxProps.mode !== 'horizontal') {
598654
res = it.title || it.label || '';
599655
if (!isTooltip) {
600656
res = '';
601657
}
602658
// 收起时或水平模式无默认值,只展示 title
659+
// When collapsed or in horizontal mode,
660+
// there is no default value and only the title is displayed.
603661
} else if (!isInlineCollapsed || ctxProps.mode === 'horizontal') {
604662
res = it.title || '';
605663
}
@@ -629,6 +687,12 @@
629687
if (it.disabled || it.disabledParent || e.detail) return;
630688
itemsList = clearVerticalOpenStatus(it);
631689
}
690+
691+
function onSubItemSelect(index: number) {
692+
if (level == 1 && !ctxProps.multiple) {
693+
popoverRef[index] && popoverRef[index].updateShow && popoverRef[index].updateShow(false);
694+
}
695+
}
632696
</script>
633697

634698
{#each itemsList as it, index (it.uid)}
@@ -903,7 +967,7 @@
903967
<svelte:fragment slot="triggerEl">
904968
{#if it.type !== 'divider'}
905969
<li
906-
on:click={(e) => handleSelect(it, e)}
970+
on:click={(e) => handleSelect(it, e, index)}
907971
aria-hidden="true"
908972
style:padding-left={`${getIndent(it, ctxProps.inlineCollapsed)}`}
909973
class={cnames(it)}

0 commit comments

Comments
 (0)