Skip to content

Commit e6ba367

Browse files
feat: optionally disable popping on backspace (#551)
* Adding disablePoppingOnBackspace prop/implementing * Adding test for disablePoppingOnBackspace * Updating snapshot index.test.js.md * Updating snapshot index.test.js.md * Updating docs with disablePoppingOnBackspaceProp * Removing snapshot * Adding snapshot back * Removing redundant assertion
1 parent bd97f0a commit e6ba367

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ A lightweight and fast control to render a select component that can display hie
6666
- [searchPredicate](#searchpredicate)
6767
- [inlineSearchInput](#inlinesearchinput)
6868
- [tabIndex](#tabIndex)
69+
- [disablePoppingOnBackspace](#disablePoppingOnBackspace)
6970
- [Styling and Customization](#styling-and-customization)
7071
- [Using default styles](#default-styles)
7172
- [Customizing with Bootstrap, Material Design styles](#customizing-styles)
@@ -421,6 +422,12 @@ Type: `number` (default: `0`)
421422

422423
`tabIndex=0` attribute indicates that its element can be focused, and where it participates in sequential keyboard navigation.
423424

425+
### disablePoppingOnBackspace
426+
427+
Type: `bool` (default: `false`)
428+
429+
`disablePoppingOnBackspace=true` attribute indicates that when a user triggers a 'backspace' keyDown in the empty search bar, the tree will not deselect nodes.
430+
424431
## Styling and Customization
425432

426433
### Default styles

__snapshots__/src/index.test.js.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ Generated by [AVA](https://ava.li).
462462
},
463463
]
464464
}
465+
disablePoppingOnBackspace={false}
465466
id="rdts"
466467
inlineSearchInput={false}
467468
mode="radioSelect"
@@ -617,6 +618,7 @@ Generated by [AVA](https://ava.li).
617618
},
618619
]
619620
}
621+
disablePoppingOnBackspace={false}
620622
id="rdts"
621623
inlineSearchInput={false}
622624
onAction={Function onAction {}}
43 Bytes
Binary file not shown.

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class DropdownTreeSelect extends Component {
4949
searchPredicate: PropTypes.func,
5050
inlineSearchInput: PropTypes.bool,
5151
tabIndex: PropTypes.number,
52+
disablePoppingOnBackspace: PropTypes.bool,
5253
}
5354

5455
static defaultProps = {
@@ -60,6 +61,7 @@ class DropdownTreeSelect extends Component {
6061
showDropdown: 'default',
6162
inlineSearchInput: false,
6263
tabIndex: 0,
64+
disablePoppingOnBackspace: false,
6365
}
6466

6567
constructor(props) {
@@ -236,7 +238,7 @@ class DropdownTreeSelect extends Component {
236238
}
237239

238240
onKeyboardKeyDown = e => {
239-
const { readOnly, mode } = this.props
241+
const { readOnly, mode, disablePoppingOnBackspace } = this.props
240242
const { showDropdown, tags, searchModeOn, currentFocus } = this.state
241243
const tm = this.treeManager
242244
const tree = searchModeOn ? tm.matchTree : tm.tree
@@ -271,7 +273,12 @@ class DropdownTreeSelect extends Component {
271273
this.handleClick()
272274
}
273275
return
274-
} else if (e.key === 'Backspace' && tags.length && this.searchInput.value.length === 0) {
276+
} else if (
277+
!disablePoppingOnBackspace &&
278+
e.key === 'Backspace' &&
279+
tags.length &&
280+
this.searchInput.value.length === 0
281+
) {
275282
const lastTag = tags.pop()
276283
this.onCheckboxChange(lastTag._id, false)
277284
} else {

src/index.keyboardNav.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ test('can delete tags with backspace/delete on keyboardNavigation', t => {
146146
t.deepEqual(wrapper.state().tags.length, 0)
147147
})
148148

149+
test('cannot delete tags on empty search input with backspace on keyboardNavigation when disablePoppingOnBackspace is true', t => {
150+
const data = [{ ...node('a', 'a'), checked: true }, { ...node('b', 'b'), checked: true }]
151+
const wrapper = mount(<DropdownTreeSelect data={data} disablePoppingOnBackspace={true} />)
152+
wrapper.instance().searchInput.value = 'x'
153+
triggerOnKeyboardKeyDown(wrapper, 'Backspace')
154+
t.deepEqual(wrapper.state().tags.length, 2)
155+
156+
wrapper.instance().searchInput.value = ''
157+
triggerOnKeyboardKeyDown(wrapper, 'Backspace')
158+
t.deepEqual(wrapper.state().tags.length, 2)
159+
})
160+
149161
test('remembers current focus between prop updates', t => {
150162
const wrapper = mount(<DropdownTreeSelect data={tree} showDropdown="initial" />)
151163
t.false(wrapper.find('li.focused').exists())

0 commit comments

Comments
 (0)