Skip to content

Commit 06dee72

Browse files
committed
test: supplement test case for keyboard operation
1 parent e489e17 commit 06dee72

File tree

2 files changed

+73
-20
lines changed

2 files changed

+73
-20
lines changed

src/OptionList.tsx

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
158158

159159
// >>> Disabled Strategy
160160
const disabledStrategy = (node: DataNode) => {
161-
if (node.disabled) {
162-
return true;
163-
}
161+
// if (node.disabled) {
162+
// return true;
163+
// }
164164

165165
if (isOverMaxCount) {
166166
const selectedValues = displayValues?.map(v => v.value) || [];
@@ -206,20 +206,8 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
206206
): EventDataNode<any> | null => {
207207
const availableNodes = availableNodesRef.current;
208208

209-
if (availableNodes.length === 0) {
210-
return null;
211-
}
212-
213-
if (!currentKey) {
214-
return availableNodes[0];
215-
}
216-
217209
const currentIndex = availableNodes.findIndex(node => node[fieldNames.value] === currentKey);
218210

219-
if (currentIndex === -1) {
220-
return availableNodes[0];
221-
}
222-
223211
const nextIndex =
224212
direction === 'next'
225213
? (currentIndex + 1) % availableNodes.length
@@ -333,11 +321,6 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
333321
setActiveKey(key);
334322
return;
335323
}
336-
337-
const nextNode = getNextMatchingNode(key);
338-
if (nextNode) {
339-
setActiveKey(nextNode[fieldNames.value]);
340-
}
341324
};
342325

343326
// ========================== Render ==========================

tests/Select.maxCount.spec.tsx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,76 @@ describe('TreeSelect.maxCount keyboard operations', () => {
206206
// verify only two options are selected
207207
expect(container.querySelectorAll('.rc-tree-select-tree-treenode-selected')).toHaveLength(2);
208208
});
209+
210+
it('should cycle through selected options when maxCount is reached', () => {
211+
const { container } = render(
212+
<TreeSelect treeData={treeData} multiple open maxCount={2} value={['0', '2']} />,
213+
);
214+
215+
const input = container.querySelector('input');
216+
217+
keyDown(input, KeyCode.DOWN);
218+
expect(
219+
container.querySelector('.rc-tree-select-tree-treenode.rc-tree-select-tree-treenode-active')
220+
?.textContent,
221+
).toBe('2 label');
222+
223+
// Move down again to cycle back to the first selected item
224+
keyDown(input, KeyCode.DOWN);
225+
expect(
226+
container.querySelector('.rc-tree-select-tree-treenode.rc-tree-select-tree-treenode-active')
227+
?.textContent,
228+
).toBe('0 label');
229+
});
230+
231+
it('should cycle through selected options in reverse when using UP key', () => {
232+
const { container } = render(
233+
<TreeSelect treeData={treeData} multiple open maxCount={2} value={['0', '2']} />,
234+
);
235+
236+
const input = container.querySelector('input');
237+
238+
// Initially activate the last selected item
239+
keyDown(input, KeyCode.UP);
240+
expect(
241+
container.querySelector('.rc-tree-select-tree-treenode.rc-tree-select-tree-treenode-active')
242+
?.textContent,
243+
).toBe('2 label');
244+
245+
// Move up again to cycle back to the first selected item
246+
keyDown(input, KeyCode.UP);
247+
expect(
248+
container.querySelector('.rc-tree-select-tree-treenode.rc-tree-select-tree-treenode-active')
249+
?.textContent,
250+
).toBe('0 label');
251+
252+
// Move up again to cycle back to the last selected item
253+
keyDown(input, KeyCode.UP);
254+
expect(
255+
container.querySelector('.rc-tree-select-tree-treenode.rc-tree-select-tree-treenode-active')
256+
?.textContent,
257+
).toBe('2 label');
258+
});
259+
260+
it('should handle LEFT/RIGHT keys correctly when maxCount is reached', () => {
261+
const { container } = render(
262+
<TreeSelect treeData={treeData} multiple open maxCount={2} value={['0', '2']} />,
263+
);
264+
265+
const input = container.querySelector('input');
266+
267+
keyDown(input, KeyCode.RIGHT);
268+
expect(
269+
container.querySelector('.rc-tree-select-tree-treenode.rc-tree-select-tree-treenode-active')
270+
?.textContent,
271+
).toBe('2 label');
272+
273+
keyDown(input, KeyCode.LEFT);
274+
expect(
275+
container.querySelector('.rc-tree-select-tree-treenode.rc-tree-select-tree-treenode-active')
276+
?.textContent,
277+
).toBe('0 label');
278+
});
209279
});
210280

211281
describe('TreeSelect.maxCount with different strategies', () => {

0 commit comments

Comments
 (0)