-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from Enterwell/feature/search-header
feat: Added SearchHeader component to ui
- Loading branch information
Showing
7 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { SearchHeader } from '@enterwell/react-ui'; | ||
|
||
export function ExampleSearchHeader() { | ||
return ( | ||
<SearchHeader | ||
variant='h4' | ||
onSubmit={(searchTerm) => console.log(searchTerm)} | ||
placeholder='Search by term'> | ||
Search | ||
</SearchHeader> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
title: SearchHeader | ||
--- | ||
|
||
import { SearchHeader } from '@enterwell/react-ui'; | ||
import { ComponentWithSource } from '../../../components/docs/ComponentWithSource.tsx'; | ||
import { ExampleSearchHeader } from '../../../components/ExampleSearchHeader.tsx'; | ||
import { ComponentDescription, ComponentParameters, ComponentSource } from '../../../components/docs/ComponentDocs'; | ||
|
||
# SearchHeader | ||
|
||
## Description | ||
|
||
<ComponentDescription name="SearchHeader" /> | ||
|
||
### Parameters | ||
|
||
<ComponentParameters name="SearchHeader" /> | ||
|
||
## Example | ||
|
||
<ComponentWithSource component={ ExampleSearchHeader } centered /> | ||
|
||
## Inspect | ||
|
||
<details> | ||
<summary>Source code</summary> | ||
<ComponentSource | ||
package="@enterwell/react-ui" | ||
directory="SearchHeader" | ||
name="SearchHeader" | ||
/> | ||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React, { ReactNode, type MouseEvent } from 'react'; | ||
import { | ||
Typography, Stack, IconButton, InputBase, useTheme | ||
} from '@mui/material'; | ||
import { Variant } from '@mui/material/styles/createTypography'; | ||
import { Search, Clear } from '@mui/icons-material'; | ||
|
||
/** | ||
* The SearchHeader component props. | ||
* @public | ||
*/ | ||
export type SearchHeaderProps = { | ||
onSubmit?: (searchTerm: string) => void, | ||
placeholder?: string | undefined, | ||
/** | ||
* @defaultValue 'h1' | ||
*/ | ||
variant?: Variant | undefined, | ||
children?: ReactNode | undefined | ||
} | ||
|
||
/** | ||
* Search header. | ||
* @param props - The props of the component. | ||
* @returns The search header component. | ||
* @public | ||
*/ | ||
export function SearchHeader({ | ||
onSubmit, | ||
placeholder, | ||
children, | ||
variant = 'h1' | ||
}: SearchHeaderProps) { | ||
const [isSearching, setIsSearching] = React.useState(false); | ||
const [searchTerm, setSearchTerm] = React.useState(''); | ||
|
||
const theme = useTheme(); | ||
|
||
const handleSearchClick = (e: MouseEvent<HTMLButtonElement>) => { | ||
e.stopPropagation(); | ||
setIsSearching(true); | ||
}; | ||
|
||
const handleClearClick = (e: MouseEvent) => { | ||
e.stopPropagation(); | ||
setSearchTerm(''); | ||
setIsSearching(false); | ||
if (onSubmit) | ||
onSubmit(''); | ||
}; | ||
|
||
const handleInputBlur = () => { | ||
if (searchTerm === '') { | ||
setIsSearching(false); | ||
if (onSubmit) | ||
onSubmit(''); | ||
} | ||
}; | ||
|
||
const handleSubmit = (e: any) => { | ||
if (e.key === 'Enter' && onSubmit) { | ||
onSubmit(searchTerm); | ||
} else if (e.key === 'Escape') { | ||
handleClearClick(e); | ||
} | ||
}; | ||
|
||
const fontSize = variant ? theme.typography[variant as Variant].fontSize : undefined; | ||
|
||
return isSearching ? ( | ||
<InputBase | ||
startAdornment={( | ||
<IconButton onClick={handleClearClick}> | ||
<Clear color="primary" /> | ||
</IconButton> | ||
)} | ||
autoFocus | ||
placeholder={placeholder} | ||
onClick={(e) => e.stopPropagation()} | ||
value={searchTerm} | ||
onChange={(e) => setSearchTerm(e.target.value)} | ||
onKeyDown={handleSubmit} | ||
onBlur={handleInputBlur} | ||
/> | ||
) : ( | ||
<Stack spacing={1} direction="row" alignItems="center"> | ||
<Typography variant={variant}>{children}</Typography> | ||
{onSubmit && ( | ||
<IconButton onClick={handleSearchClick}> | ||
<Search sx={{ fontSize: fontSize }} /> | ||
</IconButton> | ||
)} | ||
</Stack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./SearchHeader"; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters