Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tree): update parent item in values setter #496

Merged
merged 7 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/elements/src/list/elements/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enum Direction {
DOWN = 1
}

const valueFormatWarning = new WarningNotice('The specified \'values\' format does not conform to the required format.');
export const valueFormatWarning = new WarningNotice('The specified \'values\' format does not conform to the required format.');

/**
* Provides listing and immutable selection
Expand Down Expand Up @@ -162,7 +162,6 @@ export class List<T extends DataItem = ItemData> extends ControlElement {
* selected item values
* @type {string[]}
* @default []
* @readonly
*/
@property({ type: Array, attribute: false })
public get values (): string[] {
Expand Down
23 changes: 23 additions & 0 deletions packages/elements/src/tree/__test__/tree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,29 @@ describe('tree/Tree', () => {
expect(el.value).to.equal('1.1');
expect(el.values).to.deep.equal(['1.1', '1.2']);
});

it('Update the parent selected state correctly', async () => {
const el = await fixture('<ef-tree multiple></ef-tree>');
el.data = nestedData;
await elementUpdated(el);
const item = el.children[0];
expect(item.checkedState).to.equal(-1); // Indeterminate
el.values = [];
await elementUpdated(el);
expect(item.checkedState).to.equal(0); // Unchecked
el.values = ['1.1'];
await elementUpdated(el);
expect(item.checkedState).to.equal(-1); // Indeterminate
});

it('Should set values to empty array when set invalid values', async () => {
const el = await fixture('<ef-tree multiple></ef-tree>');
el.data = nestedData;
await elementUpdated(el);
el.values = '1.1';
await elementUpdated(el);
expect(el.values).to.deep.equal([]);
});
});

describe('Filter Tests', () => {
Expand Down
25 changes: 22 additions & 3 deletions packages/elements/src/tree/elements/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { property } from '@refinitiv-ui/core/decorators/property.js';
import { VERSION } from '../../version.js';
import { CollectionComposer } from '@refinitiv-ui/utils/collection.js';

import { List } from '../../list/index.js';
import { List, valueFormatWarning } from '../../list/index.js';
import { TreeRenderer } from '../helpers/renderer.js';
import { defaultFilter } from '../helpers/filter.js';
import type { TreeData, TreeDataItem, TreeFilter } from '../helpers/types';
Expand Down Expand Up @@ -425,8 +425,27 @@ export class Tree<T extends TreeDataItem = TreeDataItem> extends List<T> {
return this.composer.getItemPropertyValue(item, 'value') as string || '';
});
}
public set values (value: string[]) {
super.values = value;
public set values (values: string[]) {
if (!Array.isArray(values)) {
Nantawat-Poothong marked this conversation as resolved.
Show resolved Hide resolved
valueFormatWarning.show();
this.values = [];
}
else {
// Clone value arrays
const newValue = [...values].sort().toString();
const oldValue = [...this.values].sort().toString();

if (newValue !== oldValue) {
this.manager.uncheckAllItems();
values.some((value) => {
this.queryItemsByPropertyValue('value', value).forEach((item) => {
this.manager.checkItem(item);
});
return !this.multiple; // Only set the fist value if multiple is not enabled
});
this.requestUpdate('values', this.values);
}
}
}

/**
Expand Down