Skip to content

Commit

Permalink
Filter builder/add clear root group action (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
ViliusBa authored Dec 11, 2023
1 parent 19c09b8 commit b5fe7a8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/api/components-react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,7 @@ export function PropertyFilterBuilder(props: PropertyFilterBuilderProps): JSX.El
export class PropertyFilterBuilderActions {
constructor(setState: (setter: (prevState: PropertyFilterBuilderState) => PropertyFilterBuilderState) => void);
addItem(path: string[], itemType: "RULE_GROUP" | "RULE"): void;
removeAllItems(): void;
removeItem(path: string[]): void;
setRuleErrorMessages(ruleIdsAndErrorMessages: Map<string, string>): void;
setRuleGroupOperator(path: string[], operator: PropertyFilterRuleGroupOperator): void;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/components-react",
"comment": "Add ability to remove all items in PropertyFilterBuilderActions.",
"type": "patch"
}
],
"packageName": "@itwin/components-react"
}
5 changes: 5 additions & 0 deletions docs/changehistory/NextVersion.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Table of contents:
- [@itwin/components-react](#itwincomponents-react)
- [Fixes](#fixes)
- [Improvements](#improvements)
- [Additions](#additions)

## @itwin/appui-react

Expand All @@ -24,3 +25,7 @@ Table of contents:
### Improvements

- Reset to default operator when property of a rule item is changed in `FilterBuilderState`.

### Additions

- Add `removeAllItems` function which will clear all items in the filter state.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ export class PropertyFilterBuilderActions {
});
}

/** Removes all items from root group. */
public removeAllItems() {
this.updateState((state) => {
state.rootGroup = createEmptyRuleGroup();
});
}

/** Sets operator of rule group specified by the path. */
public setRuleGroupOperator(
path: string[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ describe("usePropertyFilterBuilder", () => {
});
});

it("removes all rules from root group", () => {
const { result } = renderHook(() => usePropertyFilterBuilder());
const { actions } = result.current;
let { rootGroup } = result.current;

actions.addItem([], "RULE");
actions.addItem([], "RULE_GROUP");

rootGroup = result.current.rootGroup;
expect(rootGroup.items).to.have.lengthOf(3);
actions.removeAllItems();

rootGroup = result.current.rootGroup;
expect(rootGroup.items).to.have.lengthOf(1);
expect(rootGroup).to.containSubset({
items: [{ groupId: rootGroup.id }],
});
});

it("clears rule instead of removing it when only one rule is left in the rule group", () => {
const { result } = renderHook(() => usePropertyFilterBuilder());
const { actions } = result.current;
Expand Down

0 comments on commit b5fe7a8

Please sign in to comment.