-
Notifications
You must be signed in to change notification settings - Fork 89
feat(filter): Add filter method #7127
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
Changes from all commits
4f794d5
c8a2518
4d1f1c0
9620145
9f54ead
8b4d8a8
4592095
9414f6b
1ca7937
fd4f7e2
42ff266
96ec73d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,7 +61,7 @@ export class Filter | |
|
|
||
| @Watch("items") | ||
| watchItemsHandler(): void { | ||
| this.filter(this.value); | ||
| this.filterDebounced(this.value); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -112,7 +112,7 @@ export class Filter | |
|
|
||
| @Watch("value") | ||
| valueHandler(value: string): void { | ||
| this.filter(value); | ||
| this.filterDebounced(value); | ||
| } | ||
|
|
||
| // -------------------------------------------------------------------------- | ||
|
|
@@ -169,6 +169,7 @@ export class Filter | |
| disconnectedCallback(): void { | ||
| disconnectLocalized(this); | ||
| disconnectMessages(this); | ||
| this.filterDebounced.cancel(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Would you need to filter again on
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could address that if it ever comes up? Since we don't know if a component is moved vs removed we would have to maintain state like this that isn't set via a prop. It would be something to discuss across all components that have some async method or state to maintain.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For sure, we can keep an eye out for it. I think this is a very edge use case anyways since the filter is a supporting component and it'd be odd for a component to move it around during filtering. 🤔 |
||
| } | ||
|
|
||
| componentDidLoad(): void { | ||
|
|
@@ -181,6 +182,22 @@ export class Filter | |
| // | ||
| // -------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Performs a filter on the component. | ||
| * | ||
| * This method can be useful because filtering is delayed and asynchronous. | ||
| * | ||
| * @param {string} value - The filter text value. | ||
| * @returns {Promise<void>} | ||
| */ | ||
| @Method() | ||
| async filter(value: string = this.value): Promise<void> { | ||
| return new Promise((resolve) => { | ||
| this.value = value; | ||
| this.filterDebounced(value, false, resolve); | ||
| }); | ||
| } | ||
|
|
||
| /** Sets focus on the component. */ | ||
| @Method() | ||
| async setFocus(): Promise<void> { | ||
|
|
@@ -195,15 +212,16 @@ export class Filter | |
| // | ||
| // -------------------------------------------------------------------------- | ||
|
|
||
| private filter = debounce( | ||
| (value: string, emit = false): void => this.updateFiltered(filter(this.items, value), emit), | ||
| private filterDebounced = debounce( | ||
| (value: string, emit = false, onFilter?: () => void): void => | ||
| this.updateFiltered(filter(this.items, value), emit, onFilter), | ||
| DEBOUNCE_TIMEOUT | ||
| ); | ||
|
|
||
| inputHandler = (event: CustomEvent): void => { | ||
| const target = event.target as HTMLCalciteInputElement; | ||
| this.value = target.value; | ||
| this.filter(target.value, true); | ||
| this.filterDebounced(target.value, true); | ||
| }; | ||
|
|
||
| keyDownHandler = (event: KeyboardEvent): void => { | ||
|
|
@@ -219,16 +237,16 @@ export class Filter | |
|
|
||
| clear = (): void => { | ||
| this.value = ""; | ||
| this.filter("", true); | ||
| this.filterDebounced("", true); | ||
| this.setFocus(); | ||
| }; | ||
|
|
||
| updateFiltered(filtered: any[], emit = false): void { | ||
| this.filteredItems.length = 0; | ||
| this.filteredItems = this.filteredItems.concat(filtered); | ||
| updateFiltered(filtered: object[], emit = false, callback?: () => void): void { | ||
|
driskull marked this conversation as resolved.
|
||
| this.filteredItems = filtered; | ||
| if (emit) { | ||
| this.calciteFilterChange.emit(); | ||
| } | ||
| callback?.(); | ||
| } | ||
|
|
||
| // -------------------------------------------------------------------------- | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.