Skip to content

Commit 6218479

Browse files
committed
use required argument "mode" instead of optional "deselect"
1 parent 6999235 commit 6218479

4 files changed

Lines changed: 53 additions & 36 deletions

File tree

packages/devextreme/js/__internal/grids/grid_core/ai_assistant/commands/__tests__/selection.test.ts

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -271,49 +271,62 @@ describe('selectByIndexesCommand', () => {
271271
afterEach(() => afterTest());
272272

273273
describe('schema', () => {
274-
it('accepts an array of positive integers with deselect', () => {
274+
it('accepts an array of positive integers with mode deselect', () => {
275275
expect(selectByIndexesCommand.schema.safeParse({
276-
indexes: [1, 2, 3], deselect: true,
276+
indexes: [1, 2, 3], mode: 'deselect',
277+
}).success).toBe(true);
278+
});
279+
280+
it('accepts mode select', () => {
281+
expect(selectByIndexesCommand.schema.safeParse({
282+
indexes: [1], mode: 'select',
277283
}).success).toBe(true);
278284
});
279285

280286
it('rejects when indexes is missing', () => {
281-
expect(selectByIndexesCommand.schema.safeParse({}).success).toBe(false);
287+
expect(selectByIndexesCommand.schema.safeParse({ mode: 'select' }).success).toBe(false);
282288
});
283289

284-
it('accepts when deselect is omitted (optional)', () => {
290+
it('rejects when mode is missing', () => {
285291
expect(selectByIndexesCommand.schema.safeParse({
286292
indexes: [1],
287-
}).success).toBe(true);
293+
}).success).toBe(false);
294+
});
295+
296+
it('rejects an invalid mode value', () => {
297+
expect(selectByIndexesCommand.schema.safeParse({
298+
indexes: [1], mode: 'toggle',
299+
}).success).toBe(false);
288300
});
289301

290302
it('rejects when indexes is an empty array', () => {
291303
expect(selectByIndexesCommand.schema.safeParse({
292-
indexes: [],
304+
indexes: [], mode: 'select',
293305
}).success).toBe(false);
294306
});
295307

296308
it('rejects zero (indexes are 1-based)', () => {
297309
expect(selectByIndexesCommand.schema.safeParse({
298-
indexes: [0],
310+
indexes: [0], mode: 'select',
299311
}).success).toBe(false);
300312
});
301313

302314
it('rejects negative indexes', () => {
303315
expect(selectByIndexesCommand.schema.safeParse({
304-
indexes: [-1],
316+
indexes: [-1], mode: 'select',
305317
}).success).toBe(false);
306318
});
307319

308320
it('rejects non-integer indexes', () => {
309321
expect(selectByIndexesCommand.schema.safeParse({
310-
indexes: [1.5],
322+
indexes: [1.5], mode: 'select',
311323
}).success).toBe(false);
312324
});
313325

314326
it('rejects unknown properties', () => {
315327
expect(selectByIndexesCommand.schema.safeParse({
316328
indexes: [1],
329+
mode: 'select',
317330
extra: 1,
318331
}).success).toBe(false);
319332
});
@@ -326,7 +339,7 @@ describe('selectByIndexesCommand', () => {
326339
const callbacks = createCallbacks();
327340

328341
const result = await selectByIndexesCommand.execute(instance, callbacks)({
329-
indexes: [1],
342+
indexes: [1], mode: 'select',
330343
});
331344

332345
expect(result.status).toBe('failure');
@@ -340,7 +353,7 @@ describe('selectByIndexesCommand', () => {
340353

341354
// Three rows in createGrid; 1-based index 100 has no row on the current page.
342355
const result = await selectByIndexesCommand.execute(instance, callbacks)({
343-
indexes: [1, 100],
356+
indexes: [1, 100], mode: 'select',
344357
});
345358

346359
expect(result.status).toBe('failure');
@@ -360,7 +373,7 @@ describe('selectByIndexesCommand', () => {
360373
const callbacks = createCallbacks();
361374

362375
const result = await selectByIndexesCommand.execute(instance, callbacks)({
363-
indexes: [1],
376+
indexes: [1], mode: 'select',
364377
});
365378

366379
expect(result.status).toBe('failure');
@@ -373,21 +386,21 @@ describe('selectByIndexesCommand', () => {
373386
const callbacks = createCallbacks();
374387

375388
const result = await selectByIndexesCommand.execute(instance, callbacks)({
376-
indexes: [1, 3],
389+
indexes: [1, 3], mode: 'select',
377390
});
378391

379392
expect(selectSpy).toHaveBeenCalledWith([0, 2]);
380393
expect(result.status).toBe('success');
381394
});
382395

383-
it('selects when deselect is omitted (defaults to selecting)', async () => {
396+
it('selects when mode is select', async () => {
384397
const instance = await createGrid();
385398
const selectSpy = jest.spyOn(instance, 'selectRowsByIndexes').mockReturnValue(Promise.resolve([]) as never);
386399
const deselectSpy = jest.spyOn(instance, 'deselectRows');
387400
const callbacks = createCallbacks();
388401

389402
const result = await selectByIndexesCommand.execute(instance, callbacks)({
390-
indexes: [1, 3],
403+
indexes: [1, 3], mode: 'select',
391404
});
392405

393406
expect(selectSpy).toHaveBeenCalledWith([0, 2]);
@@ -402,7 +415,7 @@ describe('selectByIndexesCommand', () => {
402415
const callbacks = createCallbacks();
403416

404417
const result = await selectByIndexesCommand.execute(instance, callbacks)({
405-
indexes: [1], deselect: true,
418+
indexes: [1], mode: 'deselect',
406419
});
407420

408421
expect(deselectSpy).toHaveBeenCalledWith([1]);
@@ -418,7 +431,7 @@ describe('selectByIndexesCommand', () => {
418431
const callbacks = createCallbacks();
419432

420433
const result = await selectByIndexesCommand.execute(instance, callbacks)({
421-
indexes: [1],
434+
indexes: [1], mode: 'select',
422435
});
423436

424437
expect(result.status).toBe('failure');
@@ -431,7 +444,7 @@ describe('selectByIndexesCommand', () => {
431444
const callbacks = createCallbacks();
432445

433446
const result = await selectByIndexesCommand.execute(instance, callbacks)({
434-
indexes: [1],
447+
indexes: [1], mode: 'select',
435448
});
436449

437450
expect(result.status).toBe('failure');
@@ -444,7 +457,7 @@ describe('selectByIndexesCommand', () => {
444457
const callbacks = createCallbacks();
445458

446459
const result = await selectByIndexesCommand.execute(instance, callbacks)({
447-
indexes: [1], deselect: true,
460+
indexes: [1], mode: 'deselect',
448461
});
449462

450463
expect(result.status).toBe('failure');
@@ -458,7 +471,7 @@ describe('selectByIndexesCommand', () => {
458471
const callbacks = createCallbacks();
459472

460473
await selectByIndexesCommand.execute(instance, callbacks)({
461-
indexes: [1, 3],
474+
indexes: [1, 3], mode: 'select',
462475
});
463476

464477
expect(callbacks.success).toHaveBeenCalledWith('Select row(s) number 1, 3 on the current page.');
@@ -470,7 +483,7 @@ describe('selectByIndexesCommand', () => {
470483
const callbacks = createCallbacks();
471484

472485
await selectByIndexesCommand.execute(instance, callbacks)({
473-
indexes: [1], deselect: true,
486+
indexes: [1], mode: 'deselect',
474487
});
475488

476489
expect(callbacks.success).toHaveBeenCalledWith('Deselect row(s) number 1 on the current page.');
@@ -481,7 +494,7 @@ describe('selectByIndexesCommand', () => {
481494
const callbacks = createCallbacks();
482495

483496
await selectByIndexesCommand.execute(instance, callbacks)({
484-
indexes: [1],
497+
indexes: [1], mode: 'select',
485498
});
486499

487500
expect(callbacks.failure).toHaveBeenCalledWith('Select row(s) number 1 on the current page.');

packages/devextreme/js/__internal/grids/grid_core/ai_assistant/commands/selection.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { z } from 'zod';
33

44
import { defineGridCommand } from './defineGridCommand';
55
import {
6-
// eslint-disable-next-line spellcheck/spell-checker
7-
compositeKeyPairSchema, isKeyShapeValid, normalizeKey, optionalNullish,
6+
compositeKeyPairSchema, isKeyShapeValid, normalizeKey,
87
} from './utils';
98

109
const selectByKeysCommandSchema = z.object({
@@ -48,22 +47,21 @@ export const selectByKeysCommand = defineGridCommand({
4847

4948
const selectByIndexesCommandSchema = z.object({
5049
indexes: z.array(z.number().int().min(1)).min(1),
51-
// eslint-disable-next-line spellcheck/spell-checker
52-
deselect: optionalNullish(z.boolean()),
50+
mode: z.enum(['select', 'deselect']),
5351
}).strict();
5452

5553
export const selectByIndexesCommand = defineGridCommand({
5654
name: 'selectByIndexes',
5755
description: 'Select or deselect specific rows by their 1-based indexes within the current page. '
5856
+ 'Index 1 is the first row on the visible page; group/header rows are not addressable. '
59-
+ 'Set deselect to true to remove the listed rows from the current selection (e.g. "unselect row 1"); omit it or set it to false to select them. '
60-
+ 'When deselect is false or omitted, the listed rows replace the current selection. '
57+
+ 'Set mode to "deselect" to remove the listed rows from the current selection (e.g. "unselect row 1"); set it to "select" to select them. '
58+
+ 'When mode is "select", the listed rows replace the current selection. '
6159
+ 'To target rows that are not on the current page, use selectByKeys, or call pageIndex first to switch the page. '
6260
+ 'To clear selection only within the current selectAll scope, use deselectAll; to clear selection across all pages regardless of selectAllMode, use clearSelection.',
6361
schema: selectByIndexesCommandSchema,
6462
execute: (component, { success, failure }) => async (args): Promise<CommandResult> => {
6563
const rowIndexes = args.indexes.join(', ');
66-
const action = args.deselect ? 'Deselect' : 'Select';
64+
const action = args.mode === 'deselect' ? 'Deselect' : 'Select';
6765
const defaultMessage = `${action} row(s) number ${rowIndexes} on the current page.`;
6866

6967
if (component.option('selection.mode') === 'none') {
@@ -81,11 +79,17 @@ export const selectByIndexesCommand = defineGridCommand({
8179
}
8280

8381
try {
84-
if (args.deselect) {
85-
const itemKeys = normalizedRowIndexes.map((index) => items[index].key);
86-
await component.deselectRows(itemKeys);
87-
} else {
88-
await component.selectRowsByIndexes(normalizedRowIndexes);
82+
switch (args.mode) {
83+
case 'deselect': {
84+
const itemKeys = normalizedRowIndexes.map((index) => items[index].key);
85+
await component.deselectRows(itemKeys);
86+
break;
87+
}
88+
case 'select':
89+
await component.selectRowsByIndexes(normalizedRowIndexes);
90+
break;
91+
default:
92+
return failure(defaultMessage);
8993
}
9094

9195
return success(defaultMessage);

packages/devextreme/js/common/grids.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export type PredefinedCommands = {
222222
};
223223
selectByIndexes: {
224224
indexes: number[];
225-
deselect?: boolean;
225+
mode: 'select' | 'deselect';
226226
};
227227
selectAll: {};
228228
deselectAll: {};

packages/devextreme/ts/dx.all.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6611,7 +6611,7 @@ declare module DevExpress.common.grids {
66116611
};
66126612
selectByIndexes: {
66136613
indexes: number[];
6614-
deselect?: boolean;
6614+
mode: 'select' | 'deselect';
66156615
};
66166616
selectAll: {};
66176617
deselectAll: {};

0 commit comments

Comments
 (0)