Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 7491e57

Browse files
authored
fix: use and mock lodash debounce in Carousel and Dropdown (#2203)
* use lodash debounce and mock it * changelog * cleanup * mock in Carousel tests as well * cancel the debounced function in carousel * mock lodash in file
1 parent 540c9a2 commit 7491e57

File tree

6 files changed

+25
-26
lines changed

6 files changed

+25
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
3030
- Fix `toggle` changing width during animation in Teams theme @mnajdova ([#2189](https://github.com/microsoft/fluent-ui-react/pull/2189))
3131
- Fix `Popup` positioning in multiple cases @layershifter ([#2187](https://github.com/microsoft/fluent-ui-react/pull/2187))
3232
- Fix click outside in `Popup` when `trigger` is not defined @layershifter ([#2202](https://github.com/microsoft/fluent-ui-react/pull/2202))
33+
- Use `debounce` from `lodash` in `Dropdown` and `Carouel` @silviuavram ([#2203](https://github.com/microsoft/fluent-ui-react/pull/2203))
3334

3435
<!--------------------------------[ v0.42.0 ]------------------------------- -->
3536
## [v0.42.0](https://github.com/microsoft/fluent-ui-react/tree/v0.42.0) (2019-12-12)

packages/react/src/components/Carousel/Carousel.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
ChildrenComponentProps,
1616
getOrGenerateIdFromShorthand,
1717
AutoControlledComponent,
18-
debounce,
1918
} from '../../utils'
2019
import {
2120
WithAsProp,
@@ -171,6 +170,10 @@ class Carousel extends AutoControlledComponent<WithAsProp<CarouselProps>, Carous
171170
}
172171
}
173172

173+
componentWillUnmount() {
174+
this.focusItemAtIndex.cancel()
175+
}
176+
174177
actionHandlers = {
175178
showNextSlideByKeyboardNavigation: e => {
176179
e.preventDefault()
@@ -213,8 +216,7 @@ class Carousel extends AutoControlledComponent<WithAsProp<CarouselProps>, Carous
213216
itemRefs = [] as React.RefObject<HTMLElement>[]
214217
paddleNextRef = React.createRef<HTMLElement>()
215218
paddlePreviousRef = React.createRef<HTMLElement>()
216-
217-
focusItemAtIndex = debounce((index: number) => {
219+
focusItemAtIndex = _.debounce((index: number) => {
218220
this.itemRefs[index].current.focus()
219221
}, 400)
220222

packages/react/src/components/Dropdown/Dropdown.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,11 @@ class Dropdown extends AutoControlledComponent<WithAsProp<DropdownProps>, Dropdo
344344
static SearchInput = DropdownSearchInput
345345
static SelectedItem = DropdownSelectedItem
346346

347+
componentWillUnmount() {
348+
this.clearStartingString.cancel()
349+
this.clearA11ySelectionMessage.cancel()
350+
}
351+
347352
getInitialAutoControlledState({ multiple, search }: DropdownProps): DropdownState {
348353
return {
349354
a11ySelectionStatus: '',
@@ -361,15 +366,8 @@ class Dropdown extends AutoControlledComponent<WithAsProp<DropdownProps>, Dropdo
361366
}
362367
}
363368

364-
a11yStatusTimeout: any
365-
charKeysPressedTimeout: any
366369
defaultTriggerButtonId = _.uniqueId('dropdown-trigger-button-')
367370

368-
componentWillUnmount() {
369-
clearTimeout(this.a11yStatusTimeout)
370-
clearTimeout(this.charKeysPressedTimeout)
371-
}
372-
373371
/**
374372
* Used to compute the filtered items (by value and search query) and, if needed,
375373
* their string equivalents, in order to be used throughout the component.
@@ -1348,20 +1346,22 @@ class Dropdown extends AutoControlledComponent<WithAsProp<DropdownProps>, Dropdo
13481346
* so it is not read anymore via virtual cursor.
13491347
*/
13501348
setA11ySelectionMessage = (a11ySelectionStatus: string): void => {
1351-
clearTimeout(this.a11yStatusTimeout)
13521349
this.setState({ a11ySelectionStatus })
1353-
this.a11yStatusTimeout = setTimeout(() => {
1354-
this.setState({ a11ySelectionStatus: '' })
1355-
}, Dropdown.a11yStatusCleanupTime)
1350+
this.clearA11ySelectionMessage()
13561351
}
13571352

13581353
setStartingString = (startingString: string): void => {
1359-
clearTimeout(this.charKeysPressedTimeout)
13601354
this.setState({ startingString })
1361-
this.charKeysPressedTimeout = setTimeout(() => {
1362-
this.setState({ startingString: '' })
1363-
}, Dropdown.charKeyPressedCleanupTime)
1355+
this.clearStartingString()
13641356
}
1357+
1358+
clearA11ySelectionMessage = _.debounce(() => {
1359+
this.setState({ a11ySelectionStatus: '' })
1360+
}, Dropdown.a11yStatusCleanupTime)
1361+
1362+
clearStartingString = _.debounce(() => {
1363+
this.setState({ startingString: '' })
1364+
}, Dropdown.charKeyPressedCleanupTime)
13651365
}
13661366

13671367
Dropdown.slotClassNames = {

packages/react/src/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,3 @@ export module commonPropTypes {
4949

5050
export { default as withDebugId } from './withDebugId'
5151
export { default as Telemetry } from './Telemetry'
52-
export { default as debounce } from './debounce'
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
const debounce = (fn: Function, time: number): Function => {
1+
// @ts-ignore
2+
export * from 'lodash'
3+
export const debounce = (fn: Function, time: number): Function => {
24
let timeoutId
3-
45
function cancel() {
56
if (timeoutId) {
67
clearTimeout(timeoutId)
78
}
89
}
9-
1010
function wrapper(...args) {
1111
cancel()
1212
timeoutId = setTimeout(() => {
1313
timeoutId = null
1414
fn(...args)
1515
}, time)
1616
}
17-
1817
wrapper.cancel = cancel
19-
2018
return wrapper
2119
}
22-
23-
export default debounce

packages/react/test/specs/components/Dropdown/Dropdown-test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ShorthandValue } from 'src/types'
1313

1414
jest.dontMock('keyboard-key')
1515
jest.useFakeTimers()
16+
// jest.mock('lodash')
1617

1718
const getTriggerButtonWrapper = (wrapper: ReactWrapper): CommonWrapper =>
1819
findIntrinsicElement(wrapper, `.${Dropdown.slotClassNames.triggerButton}`)

0 commit comments

Comments
 (0)