Skip to content

Commit 4bb135a

Browse files
committed
Updated selectableItem props
1 parent b9aabb5 commit 4bb135a

File tree

7 files changed

+52
-46
lines changed

7 files changed

+52
-46
lines changed

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ The main idea of this fork is to eliminate render during selection caused by sta
2121

2222
## Usage
2323

24-
Package exports 5 entities `{ TSelectableItemProps, SelectableGroup, createSelectable, SelectAll, DeselectAll }`.
24+
Package exports 5 entities
25+
26+
```ts
27+
export { TSelectableItemProps, SelectableGroup, createSelectable, SelectAll, DeselectAll }
28+
```
29+
2530
To make other components selectable wrap them using HoC `createSelectable`, add passed `selectableRef` prop to the target node and put a list of seletable items under `SelectableGroup`.
2631

27-
```js
32+
```ts
2833
import React, { Component } from 'react'
2934
import { SelectableGroup } from 'react-selectable-fast'
3035

@@ -52,16 +57,22 @@ class App extends Component {
5257
}
5358
```
5459

55-
```js
60+
```ts
5661
import React from 'react'
57-
import { createSelectable } from 'react-selectable-fast'
62+
import { TSelectableItemProps, createSelectable } from 'react-selectable-fast'
5863

59-
const SomeComponent = ({ selectableRef, selected, selecting }) => <div ref={selectableRef}>...</div>
64+
class SomeComponent extends Component<TSelectableItemProps> {
65+
render() {
66+
const { selectableRef, isSelected, isSelecting } = this.props
67+
68+
return <div ref={selectableRef}>...</div>
69+
}
70+
}
6071

6172
export default createSelectable(SomeComponent)
6273
```
6374

64-
```js
75+
```ts
6576
import React from 'react'
6677
import { SelectAll, DeselectAll } from 'react-selectable-fast'
6778
import SelectableComponent from './SomeComponent'
@@ -95,7 +106,7 @@ polyfill in your bundled application, such as [core-js](https://github.com/zloir
95106
A polyfilled environment for React-Selectable-Fast using [core-js](https://github.com/zloirock/core-js) to support older browsers
96107
might look like:
97108

98-
```js
109+
```ts
99110
import 'core-js/fn/object/assign'
100111
import 'core-js/fn/array/from'
101112
import 'core-js/fn/array/is-array'

example/src/Album.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ const DISABLED_CARD_YEARS = [10, 22, 27, 54, 82, 105, 150]
1212

1313
class Album extends Component<TAlbumProps> {
1414
render() {
15-
const { selectableRef, selected, selecting, title, year } = this.props
15+
const { selectableRef, isSelected, isSelecting, title, year } = this.props
1616

1717
const classNames = [
1818
'item',
1919
DISABLED_CARD_YEARS.includes(year) && 'not-selectable',
20-
selecting && 'selecting',
21-
selected && 'selected'
20+
isSelecting && 'selecting',
21+
isSelected && 'selected'
2222
]
2323
.filter(Boolean)
2424
.join(' ')
@@ -28,7 +28,7 @@ class Album extends Component<TAlbumProps> {
2828
<div className="tick">+</div>
2929
<h2>{title}</h2>
3030
<small>{year}</small>
31-
<Label selected={selected} selecting={selecting} />
31+
<Label isSelected={isSelected} isSelecting={isSelecting} />
3232
</div>
3333
)
3434
}

example/src/Label.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import React, { Component } from 'react'
22

33
type TLabelProps = {
4-
selecting: boolean
5-
selected: boolean
4+
isSelecting: boolean
5+
isSelected: boolean
66
}
77

88
class Label extends Component<TLabelProps> {
99
render() {
10-
const { selecting, selected } = this.props
10+
const { isSelecting, isSelected } = this.props
1111

1212
return (
1313
<div className="album-label">
14-
Selecting: <span>{`${selecting}`}</span>
14+
Selecting: <span>{`${isSelecting}`}</span>
1515
<br />
16-
Selected: <span>{`${selected}`}</span>
16+
Selected: <span>{`${isSelected}`}</span>
1717
</div>
1818
)
1919
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-selectable-fast",
3-
"version": "2.4.0",
3+
"version": "3.0.0",
44
"description": "Enable other React components to be selectable by drawing a box with your mouse/touch",
55
"repository": {
66
"type": "git",

src/CreateSelectable.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ const createSelectable = (WrappedComponent: ComponentType<any>) =>
1010
static contextType = SelectableGroupContext
1111

1212
static propTypes = {
13-
selected: bool
13+
isSelected: bool
1414
}
1515

1616
static defaultProps = {
17-
selected: false
17+
isSelected: false
1818
}
1919

2020
state = {
21-
selected: this.props.selected,
22-
selecting: false
21+
isSelected: this.props.isSelected,
22+
isSelecting: false
2323
}
2424

2525
node: HTMLElement | null = null
@@ -44,12 +44,7 @@ const createSelectable = (WrappedComponent: ComponentType<any>) =>
4444

4545
render() {
4646
return (
47-
<WrappedComponent
48-
{...this.props}
49-
selected={this.state.selected}
50-
selecting={this.state.selecting}
51-
selectableRef={this.getSelectableRef}
52-
/>
47+
<WrappedComponent {...this.props} {...this.state} selectableRef={this.getSelectableRef} />
5348
)
5449
}
5550
}

src/Selectable.types.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export type TSelectableGroupContext = {
1515
}
1616

1717
export type TSelectableItemState = {
18-
selected: boolean
19-
selecting: boolean
18+
isSelected: boolean
19+
isSelecting: boolean
2020
}
2121

2222
export type TSelectableItem = Component & {
@@ -27,8 +27,6 @@ export type TSelectableItem = Component & {
2727
bounds: Maybe<TComputedBounds>
2828
}
2929

30-
export type TSelectableItemProps = {
31-
selected: boolean
32-
selecting: boolean
30+
export type TSelectableItemProps = TSelectableItemState & {
3331
selectableRef(node: HTMLElement | null): void
3432
}

src/SelectableGroup.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
191191

192192
registerSelectable = (selectableItem: TSelectableItem) => {
193193
this.registry.add(selectableItem)
194-
if (selectableItem.state.selected) {
194+
if (selectableItem.state.isSelected) {
195195
this.selectedItems.add(selectableItem)
196196
}
197197
}
@@ -200,6 +200,8 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
200200
this.registry.delete(selectableItem)
201201
this.selectedItems.delete(selectableItem)
202202
this.selectingItems.delete(selectableItem)
203+
204+
this.props.onSelectionFinish!([...this.selectedItems])
203205
}
204206

205207
toggleSelectionMode() {
@@ -343,23 +345,23 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
343345
}
344346

345347
const isCollided = doObjectsCollide(selectboxBounds, item.bounds!, tolerance, this.props.delta)
346-
const { selecting, selected } = item.state
348+
const { isSelecting, isSelected } = item.state
347349

348350
if (isFromClick && isCollided) {
349-
if (selected) {
351+
if (isSelected) {
350352
this.selectedItems.delete(item)
351353
} else {
352354
this.selectedItems.add(item)
353355
}
354356

355-
item.setState({ selected: !selected })
357+
item.setState({ isSelected: !isSelected })
356358

357359
return (this.clickedItem = item)
358360
}
359361

360362
if (!isFromClick && isCollided) {
361-
if (selected && enableDeselect && (!this.selectionStarted || mixedDeselect)) {
362-
item.setState({ selected: false })
363+
if (isSelected && enableDeselect && (!this.selectionStarted || mixedDeselect)) {
364+
item.setState({ isSelected: false })
363365
item.deselected = true
364366

365367
this.deselectionStarted = true
@@ -369,8 +371,8 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
369371

370372
const canSelect = mixedDeselect ? !item.deselected : !this.deselectionStarted
371373

372-
if (!selecting && !selected && canSelect) {
373-
item.setState({ selecting: true })
374+
if (!isSelecting && !isSelected && canSelect) {
375+
item.setState({ isSelecting: true })
374376

375377
this.selectionStarted = true
376378
this.selectingItems.add(item)
@@ -379,9 +381,9 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
379381
}
380382
}
381383

382-
if (!isFromClick && !isCollided && selecting) {
384+
if (!isFromClick && !isCollided && isSelecting) {
383385
if (this.selectingItems.has(item)) {
384-
item.setState({ selecting: false })
386+
item.setState({ isSelecting: false })
385387

386388
this.selectingItems.delete(item)
387389

@@ -394,7 +396,7 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
394396

395397
clearSelection = () => {
396398
for (const item of this.selectedItems.values()) {
397-
item.setState({ selected: false })
399+
item.setState({ isSelected: false })
398400
this.selectedItems.delete(item)
399401
}
400402

@@ -407,8 +409,8 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
407409
this.updateWhiteListNodes()
408410

409411
for (const item of this.registry.values()) {
410-
if (!this.isInIgnoreList(item.node) && !item.state.selected) {
411-
item.setState({ selected: true })
412+
if (!this.isInIgnoreList(item.node) && !item.state.isSelected) {
413+
item.setState({ isSelected: true })
412414
this.selectedItems.add(item)
413415
}
414416
}
@@ -532,7 +534,7 @@ class SelectableGroup extends Component<TSelectableGroupProps> {
532534
this.handleClick(evt, pageY, pageX)
533535
} else {
534536
for (const item of this.selectingItems.values()) {
535-
item.setState({ selected: true, selecting: false })
537+
item.setState({ isSelected: true, isSelecting: false })
536538
}
537539
this.selectedItems = new Set([...this.selectedItems, ...this.selectingItems])
538540
this.selectingItems.clear()

0 commit comments

Comments
 (0)