Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add helper to highlight text and open search bar initial text c… #82

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.58-beta-8",
"version": "0.0.58-beta-21",
"description": "Supporting common component library",
"main": "dist/index.js",
"scripts": {
Expand Down
10 changes: 4 additions & 6 deletions src/Common/SearchBar/SearchBar.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ const SearchBar = ({
debounceTimeout,
])

// In most case would reset the state by doing history.push('/')
// assuming initialSearchText will change if we are changing history otherwise will be constant and will not change
// since on changing history we expect to make api call using useAsync so not applying handleEnter
useEffect(() => {
inputRef.current.value = initialSearchText
setShowClearButton(!!initialSearchText)
}, [initialSearchText])

const _applySearch = (value: string) => {
Expand All @@ -75,11 +77,6 @@ const SearchBar = ({
const { value } = e.target
setShowClearButton(!!value)

if (!value) {
clearSearch()
return
}

if (shouldDebounce) {
debouncedSearchChange(value)
} else {
Expand All @@ -91,6 +88,7 @@ const SearchBar = ({
const { key } = e

if (key === 'Enter') {
e.preventDefault()
const inputTarget = e.target as HTMLInputElement
const value = inputTarget.value.trim()
_applySearch(value)
Expand Down
2 changes: 1 addition & 1 deletion src/Pages/GlobalConfigurations/BuildInfra/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const BUILD_INFRA_TEXT = {
DESCRIPTION_LABEL: 'Description',
DESCRIPTION_PLACEHOLDER: 'Enter a description here',
PROFILE_LABEL: 'Profile name',
PROFILE_PLACEHOLDER: 'Eg. Java or Node',
PROFILE_PLACEHOLDER: 'Eg. java or node',
INHERITING_HEADING_DESCRIPTION: 'Inheriting from default',
SUBMIT_BUTTON_TIPPY: {
INVALID_INPUT: 'Valid input is required for all mandatory fields.',
Expand Down
29 changes: 29 additions & 0 deletions src/Shared/Helpers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
interface HighlightSearchTextProps {
/**
* The text to be highlighted
*/
searchText: string
/**
* The whole text string
*/
text: string
/**
* The classes to be applied to the highlighted text
*/
highlightClasses?: string
}

// Disabling default export since this is a helper function and we would have to export a lot of functions in future.
// eslint-disable-next-line import/prefer-default-export
export const highlightSearchText = ({ searchText, text, highlightClasses }: HighlightSearchTextProps): string => {
if (!searchText) {
return text
}

try {
const regex = new RegExp(searchText, 'gi')
return text.replace(regex, (match) => `<span class="${highlightClasses}">${match}</span>`)
} catch (error) {
return text
}
}
1 change: 1 addition & 0 deletions src/Shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './Components'
export * from './validations'
export * from './Helpers'
Loading