Skip to content

Commit

Permalink
feat: update the functionality for search clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
eshankvaish committed Jan 16, 2024
1 parent 4f03e5b commit dd333ed
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtron-labs/devtron-fe-common-lib",
"version": "0.0.53-beta-3",
"version": "0.0.53-beta-4",
"description": "Supporting common component library",
"main": "dist/index.js",
"scripts": {
Expand Down
24 changes: 14 additions & 10 deletions src/Common/SearchBar/SearchBar.component.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { ChangeEvent, FunctionComponent, useCallback, useRef, useState, KeyboardEvent } from 'react'
import { ChangeEvent, useCallback, useRef, useState, KeyboardEvent } from 'react'
import { ReactComponent as Search } from '../../Assets/Icon/ic-search.svg'
import { ReactComponent as Clear } from '../../Assets/Icon/ic-error-cross.svg'
import { SearchBarProps } from './types'
import './searchBar.scss'
import { debounce, noop } from '../Helper'
import { debounce } from '../Helper'

/**
* Generic search input component with support for enter based and debounced search
*/
const SearchBar: FunctionComponent<SearchBarProps> = ({
const SearchBar = ({
initialSearchText = '',
handleSearchChange = noop,
handleEnter = noop,
handleSearchChange = () => {},
handleEnter = () => {},
inputProps = {},
containerClassName,
shouldDebounce = false,
debounceTimeout = 300,
}) => {
}: SearchBarProps) => {
const [showClearButton, setShowClearButton] = useState(!!initialSearchText)
const inputRef = useRef<HTMLInputElement>()
const debouncedSearchChange = useCallback(debounce(handleSearchChange, debounceTimeout), [
Expand All @@ -34,20 +34,24 @@ const SearchBar: FunctionComponent<SearchBarProps> = ({
}
}

const _applySearch = (value: string) => {
handleSearchChange(value)
handleEnter(value)
}

const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
const { key } = e

if (key === 'Enter') {
const inputTarget = e.target as HTMLInputElement
const event = { ...e, target: { ...inputTarget, value: inputTarget.value.trim() } }
handleSearchChange(event.target.value)
handleEnter(event)
const value = inputTarget.value.trim()
_applySearch(value)
}
}

const clearSearch = () => {
inputRef.current.value = ''
handleSearchChange('')
_applySearch('')
}

return (
Expand Down
2 changes: 2 additions & 0 deletions src/Common/SearchBar/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as SearchBar } from './SearchBar.component'
export type { SearchBarProps } from './types'
4 changes: 2 additions & 2 deletions src/Common/SearchBar/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InputHTMLAttributes, SyntheticEvent } from 'react'
import { InputHTMLAttributes } from 'react'

export interface SearchBarProps {
/**
Expand All @@ -14,7 +14,7 @@ export interface SearchBarProps {
/**
* Enter event handler for the search input
*/
handleEnter?: (event: SyntheticEvent) => void
handleEnter?: (searchText: string) => void
/**
* Input props for the search input
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export * from './Common.service'
export * from './Checkbox'
export { default as EmptyState } from './EmptyState/EmptyState'
export { default as GenericEmptyState } from './EmptyState/GenericEmptyState'
export { default as SearchBar } from './SearchBar/SearchBar.component'
export * from './SearchBar'
export { default as Toggle } from './Toggle/Toggle'
export { default as ScanVulnerabilitiesTable } from './Security/ScanVulnerabilitiesTable'
export { default as StyledRadioGroup } from './RadioGroup/RadioGroup'
Expand Down

0 comments on commit dd333ed

Please sign in to comment.