Skip to content

Commit fa68691

Browse files
Merge pull request #51 from i-like-robots/bugfix/should-expand-collapse-prev-value
[Bugfix] Allow overriding value passed to expand/collapse callbacks
2 parents de2d9a4 + 48d9e6b commit fa68691

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ React Tag Autocomplete is a simple, accessible, tagging component ready to drop
1515
- [Styling](#styling)
1616
- [Development](#development)
1717

18-
_Please note:_ This repository is for v7 and above. To view the previous versions of React Tag Autocomplete see the [old repository](https://github.com/i-like-robots/react-tags). If you're upgrading to v7 from v6 please refer to [the migration guide](migration-guide.md)
18+
_Please note:_ This repository is for v7 and above. To view the previous versions of the `react-tag-autocomplete` package see the [old repository](https://github.com/i-like-robots/react-tags). If you're upgrading to v7 from v6 please refer to [the migration guide](migration-guide.md)
1919

2020
## Installation
2121

src/hooks/useInput.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import type React from 'react'
12
import { useContext, useMemo } from 'react'
23
import { KeyNames, VoidFn } from '../constants'
34
import { GlobalContext } from '../contexts'
45
import { inputId, isCaretAtEnd, isCaretAtStart, labelId, listBoxId, optionId } from '../lib'
5-
import type React from 'react'
66

77
export type UseInputArgs = {
88
allowBackspace: boolean
@@ -35,7 +35,7 @@ export function useInput({
3535
managerRef.current.updateInputValue(value)
3636

3737
if (document.activeElement === inputRef.current) {
38-
managerRef.current.listBoxExpand()
38+
managerRef.current.listBoxExpand(value)
3939
}
4040
}
4141

src/hooks/useManager.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import type {
1717
} from '../sharedTypes'
1818

1919
export type ManagerAPI = {
20-
listBoxCollapse(): void
21-
listBoxExpand(): void
20+
listBoxCollapse(value?: string): void
21+
listBoxExpand(value?: string): void
2222
updateActiveIndex(index: number): void
2323
updateInputValue(value: string): void
2424
selectTag(tag?: Tag): void
@@ -92,7 +92,7 @@ export function useManager({
9292
if (value) {
9393
if (allowNew) {
9494
opts.push({
95-
disabled: typeof onValidate === 'function' ? !onValidate(value) : false,
95+
disabled: onValidate ? !onValidate(value) : false,
9696
label: newOptionText,
9797
value: NewOptionValue,
9898
})
@@ -129,19 +129,19 @@ export function useManager({
129129
}
130130

131131
const api: ManagerAPI = {
132-
listBoxCollapse() {
133-
const shouldCollapse = isExpanded && (onShouldCollapse?.(state.value) ?? true)
132+
listBoxCollapse(value?: string) {
133+
if (!isExpanded) return
134134

135-
if (shouldCollapse) {
135+
if (onShouldCollapse ? onShouldCollapse(value ?? state.value) : true) {
136136
setIsExpanded(false)
137137
setLastActiveOption(null)
138138
onCollapse?.()
139139
}
140140
},
141-
listBoxExpand() {
142-
const shouldExpand = !isExpanded && (onShouldExpand?.(state.value) ?? true)
141+
listBoxExpand(value?: string) {
142+
if (isExpanded) return
143143

144-
if (shouldExpand) {
144+
if (onShouldExpand ? onShouldExpand(value ?? state.value) : true) {
145145
setIsExpanded(true)
146146
setLastActiveOption(options[activeIndex])
147147
onExpand?.()

0 commit comments

Comments
 (0)