fix: clear search on view change (#1856)

This commit is contained in:
Nishant Mittal 2021-12-10 18:38:02 +05:30 committed by GitHub
parent a931289e02
commit 87daacceca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 0 deletions

View file

@ -15,6 +15,16 @@ import {Constants} from '../constants'
import CenterPanel from './centerPanel'
Object.defineProperty(Constants, 'versionString', {value: '1.0.0'})
jest.mock('react-router-dom', () => {
const originalModule = jest.requireActual('react-router-dom')
return {
...originalModule,
useRouteMatch: jest.fn(() => {
return {url: '/board/view'}
}),
}
})
jest.mock('../utils')
jest.mock('../mutator')
jest.mock('../telemetry/telemetryClient')

View file

@ -17,6 +17,17 @@ const board = TestBlockFactory.createBoard()
const activeView = TestBlockFactory.createBoardView(board)
const card = TestBlockFactory.createCard(board)
jest.mock('react-router-dom', () => {
const originalModule = jest.requireActual('react-router-dom')
return {
...originalModule,
useRouteMatch: jest.fn(() => {
return {url: '/board/view'}
}),
}
})
describe('components/viewHeader/viewHeader', () => {
const state = {
users: {

View file

@ -11,6 +11,17 @@ import {mockStateStore, wrapIntl} from '../../testUtils'
import ViewHeaderSearch from './viewHeaderSearch'
jest.mock('react-router-dom', () => {
const originalModule = jest.requireActual('react-router-dom')
return {
...originalModule,
useRouteMatch: jest.fn(() => {
return {url: '/board/view'}
}),
}
})
describe('components/viewHeader/ViewHeaderSearch', () => {
const state = {
users: {

View file

@ -1,6 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useState, useRef, useEffect, useMemo} from 'react'
import {useRouteMatch} from 'react-router-dom'
import {FormattedMessage, useIntl} from 'react-intl'
import {useHotkeys} from 'react-hotkeys-hook'
import {debounce} from 'lodash'
@ -15,10 +16,12 @@ const ViewHeaderSearch = (): JSX.Element => {
const searchText = useAppSelector<string>(getSearchText)
const dispatch = useAppDispatch()
const intl = useIntl()
const match = useRouteMatch<{viewId?: string}>()
const searchFieldRef = useRef<{focus(selectAll?: boolean): void}>(null)
const [isSearching, setIsSearching] = useState(Boolean(searchText))
const [searchValue, setSearchValue] = useState(searchText)
const [currentView, setCurrentView] = useState(match.params?.viewId)
const dispatchSearchText = (value: string) => {
dispatch(setSearchText(value))
@ -27,6 +30,20 @@ const ViewHeaderSearch = (): JSX.Element => {
const debouncedDispatchSearchText = useMemo(
() => debounce(dispatchSearchText, 200), [])
useEffect(() => {
const viewId = match.params?.viewId
if (viewId !== currentView) {
setCurrentView(viewId)
setSearchValue('')
setIsSearching(false)
// Previously debounced calls to change the search text should be cancelled
// to avoid resetting the search text.
debouncedDispatchSearchText.cancel()
dispatchSearchText('')
}
}, [match.url])
useEffect(() => {
return () => {
debouncedDispatchSearchText.cancel()