From 05a392443da2be6907dfc9e0e8d213eb9b61cf77 Mon Sep 17 00:00:00 2001 From: zk-phi Date: Mon, 1 Jun 2020 16:29:28 +0900 Subject: [PATCH 1/3] Add methods to select / deselect by predicate --- src/SelectableGroup.tsx | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/SelectableGroup.tsx b/src/SelectableGroup.tsx index de4dabe..127aac9 100644 --- a/src/SelectableGroup.tsx +++ b/src/SelectableGroup.tsx @@ -451,29 +451,38 @@ export class SelectableGroup extends Component { return null } - clearSelection = () => { - for (const item of this.selectedItems.values()) { - item.setState({ isSelected: false }) - this.selectedItems.delete(item) + deselectItemsByPredicate = (pred: (item: TSelectableItem) => boolean) => { + for (const item of this.registry.values()) { + if (item.state.isSelected && pred(item)) { + item.setState({ isSelected: false }) + this.selectedItems.delete(item) + } } - this.setState({ selectionMode: false }) this.props.onSelectionFinish!([...this.selectedItems]) - this.props.onSelectionClear!() } - selectAll = () => { + selectItemsByPredicate = (pred: (item: TSelectableItem) => boolean) => { this.removeIgnoredItemsFromRegistry() for (const item of this.registry.values()) { - if (!item.state.isSelected) { + if (!item.state.isSelected && pred(item)) { item.setState({ isSelected: true }) this.selectedItems.add(item) } } - this.setState({ selectionMode: true }) - this.props.onSelectionFinish!([...this.selectedItems]) + this.setState({ selectionMode: true }) + this.props.onSelectionFinish!([...this.selectedItems]) + } + + clearSelection = () => { + this.deselectItemsByPredicate(() => true) + this.props.onSelectionClear!() + } + + selectAll = () => { + this.selectItemsByPredicate(() => true) } isInIgnoreList(target: HTMLElement | null) { From b46bc22c9d8ec6ea978041e9d4e55a31fc2de1f1 Mon Sep 17 00:00:00 2001 From: zk-phi Date: Mon, 1 Jun 2020 18:23:34 +0900 Subject: [PATCH 2/3] Fix indentation --- src/SelectableGroup.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SelectableGroup.tsx b/src/SelectableGroup.tsx index 127aac9..d43211c 100644 --- a/src/SelectableGroup.tsx +++ b/src/SelectableGroup.tsx @@ -472,9 +472,9 @@ export class SelectableGroup extends Component { } } - this.setState({ selectionMode: true }) - this.props.onSelectionFinish!([...this.selectedItems]) - } + this.setState({ selectionMode: true }) + this.props.onSelectionFinish!([...this.selectedItems]) + } clearSelection = () => { this.deselectItemsByPredicate(() => true) From d9e3cbc9c6a8266d2a085908b07fe9077484fffe Mon Sep 17 00:00:00 2001 From: zk-phi Date: Fri, 5 Jun 2020 16:30:19 +0900 Subject: [PATCH 3/3] Fix: Apply appropreate selectionMode value --- src/SelectableGroup.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SelectableGroup.tsx b/src/SelectableGroup.tsx index d43211c..ce568f0 100644 --- a/src/SelectableGroup.tsx +++ b/src/SelectableGroup.tsx @@ -458,7 +458,7 @@ export class SelectableGroup extends Component { this.selectedItems.delete(item) } } - this.setState({ selectionMode: false }) + this.toggleSelectionMode() this.props.onSelectionFinish!([...this.selectedItems]) } @@ -472,7 +472,7 @@ export class SelectableGroup extends Component { } } - this.setState({ selectionMode: true }) + this.toggleSelectionMode() this.props.onSelectionFinish!([...this.selectedItems]) }