Skip to content

Commit c68ecfa

Browse files
Merge pull request #76 from valerybugakov/vb/fix/onSelectionFinish
Added `onSelectedItemUnmount` callback to fix `onSelectionFinish `
2 parents 6d7d935 + dffd954 commit c68ecfa

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class App extends Component {
4848
duringSelection={this.handleSelecting}
4949
onSelectionClear={this.handleSelectionClear}
5050
onSelectionFinish={this.handleSelectionFinish}
51+
onSelectedItemUnmount={this.handleSelectedItemUnmount}
5152
ignoreList={['.not-selectable', '.item:nth-child(10)', '.item:nth-child(27)']}
5253
>
5354
<List items={this.props.items} />
@@ -123,6 +124,7 @@ The `<SelectableGroup />` component accepts a few optional props:
123124
- `duringSelection` (Function) Callback fired rapidly during selection (while the selector is being dragged). Passes an array containing selectable items currently under the selector to the callback function.
124125
- `onSelectionFinish` (Function) Callback.
125126
- `onSelectionClear` (Function) Callback.
127+
- `onSelectedItemUnmount` (Function) Callback.
126128
- `enableDeselect` (Boolean) Enables deselect with selectbox.
127129
- `mixedDeselect` (Boolean) When enabled items can be selected and deselected with selectbox at the same time, `enableDeselect` should be set to `true`.
128130
- `scrollContainer` (String) Selector of scroll container which will be used to calculate selectbox position. If not specified SelectableGroup element will be used as scroll container.

example/src/App.tsx

+36-17
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ type TAppProps = {
1212
type TAppState = {
1313
disableFirstRow: boolean
1414
reversed: boolean
15+
showSelectableGroup: boolean
1516
}
1617

1718
class App extends Component<TAppProps, TAppState> {
1819
state = {
1920
disableFirstRow: false,
20-
reversed: false
21+
reversed: false,
22+
showSelectableGroup: true
2123
}
2224

2325
countersRef = createRef<Counters>()
@@ -34,6 +36,12 @@ class App extends Component<TAppProps, TAppState> {
3436
this.setState(state => ({ reversed: !state.reversed }))
3537
}
3638

39+
toggleSelectableGroup = () => {
40+
this.setState(state => ({
41+
showSelectableGroup: !state.showSelectableGroup
42+
}))
43+
}
44+
3745
handleSelecting = (selectingItems: TAlbumItem) => {
3846
this.countersRef.current.handleSelecting(selectingItems)
3947
}
@@ -42,13 +50,18 @@ class App extends Component<TAppProps, TAppState> {
4250
this.countersRef.current.handleSelectionFinish(selectedItems)
4351
}
4452

53+
handleSelectedItemUnmount = (unmountedItem, selectedItems) => {
54+
console.log('hadneleSelectedItemUnmount')
55+
this.countersRef.current.handleSelectionFinish(selectedItems)
56+
}
57+
4558
handleSelectionClear() {
4659
console.log('Cancel selection')
4760
}
4861

4962
render() {
5063
const { items } = this.props
51-
const { disableFirstRow, reversed } = this.state
64+
const { disableFirstRow, reversed, showSelectableGroup } = this.state
5265

5366
const itemsToRender = disableFirstRow ? items.slice(5) : items
5467
const orderedItems = reversed ? itemsToRender.slice().reverse() : itemsToRender
@@ -62,21 +75,27 @@ class App extends Component<TAppProps, TAppState> {
6275
<button className="btn" type="button" onClick={this.toggleOrder}>
6376
Toggle order
6477
</button>
65-
<SelectableGroup
66-
ref={this.getSelectableGroupRef}
67-
className="main"
68-
clickClassName="tick"
69-
enableDeselect={true}
70-
tolerance={0}
71-
deselectOnEsc={true}
72-
allowClickWithoutSelected={false}
73-
duringSelection={this.handleSelecting}
74-
onSelectionClear={this.handleSelectionClear}
75-
onSelectionFinish={this.handleSelectionFinish}
76-
ignoreList={['.not-selectable']}
77-
>
78-
<List items={orderedItems} />
79-
</SelectableGroup>
78+
<button className="btn" type="button" onClick={this.toggleSelectableGroup}>
79+
Toggle group
80+
</button>
81+
{showSelectableGroup && (
82+
<SelectableGroup
83+
ref={this.getSelectableGroupRef}
84+
className="main"
85+
clickClassName="tick"
86+
enableDeselect={true}
87+
tolerance={0}
88+
deselectOnEsc={true}
89+
allowClickWithoutSelected={false}
90+
duringSelection={this.handleSelecting}
91+
onSelectionClear={this.handleSelectionClear}
92+
onSelectionFinish={this.handleSelectionFinish}
93+
onSelectedItemUnmount={this.handleSelectedItemUnmount}
94+
ignoreList={['.not-selectable']}
95+
>
96+
<List items={orderedItems} />
97+
</SelectableGroup>
98+
)}
8099
</div>
81100
)
82101
}

src/SelectableGroup.tsx

+13-14
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ export type TSelectableGroupProps = {
4444
selectboxClassName?: string
4545
style?: object
4646
selectionModeClass?: string
47+
// Event that will fire when items are selected. Passes an array of keys.
48+
onSelectionFinish?: Function
4749
onSelectionClear?: Function
50+
onSelectedItemUnmount?: Function
4851
enableDeselect?: boolean
4952
mixedDeselect?: boolean
5053
deselectOnEsc?: boolean
@@ -56,9 +59,7 @@ export type TSelectableGroupProps = {
5659
allowMetaClick?: boolean
5760
allowShiftClick?: boolean
5861
selectOnClick?: boolean
59-
/**
60-
* Scroll container selector
61-
*/
62+
// Scroll container selector
6263
scrollContainer?: string
6364

6465
/**
@@ -67,14 +68,7 @@ export type TSelectableGroupProps = {
6768
*/
6869
duringSelection?: Function
6970

70-
/**
71-
* Event that will fire when items are selected. Passes an array of keys.
72-
*/
73-
onSelectionFinish?: Function
74-
75-
/**
76-
* The component that will represent the Selectable DOM node
77-
*/
71+
// The component that will represent the Selectable DOM node
7872
component?: ReactComponentLike
7973

8074
/**
@@ -103,6 +97,7 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
10397
duringSelection: noop,
10498
onSelectionFinish: noop,
10599
onSelectionClear: noop,
100+
onSelectedItemUnmount: noop,
106101
allowClickWithoutSelected: true,
107102
selectionModeClass: 'in-selection-mode',
108103
resetOnStart: false,
@@ -189,6 +184,10 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
189184
}
190185

191186
this.removeTempEventListeners()
187+
188+
// Prevent onSelectedItemUnmount calls
189+
this.selectedItems.clear()
190+
this.selectingItems.clear()
192191
}
193192

194193
removeTempEventListeners() {
@@ -225,15 +224,15 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
225224
unregisterSelectable = (selectableItem: TSelectableItem) => {
226225
this.registry.delete(selectableItem)
227226

228-
const isHandled =
227+
const isRemoved =
229228
this.selectedItems.has(selectableItem) || this.selectingItems.has(selectableItem)
230229

231230
this.selectedItems.delete(selectableItem)
232231
this.selectingItems.delete(selectableItem)
233232

234-
if (isHandled) {
233+
if (isRemoved) {
235234
// Notify third-party dev that component did unmount and handled item probably should be deleted
236-
this.props.onSelectionFinish!([...this.selectedItems])
235+
this.props.onSelectedItemUnmount!(selectableItem, [...this.selectedItems])
237236
}
238237
}
239238

0 commit comments

Comments
 (0)