Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: adding active and hover states to search input #2741

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

Merged
merged 4 commits into from
Jun 30, 2022
Merged
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
69 changes: 38 additions & 31 deletions site/src/components/SearchBarWithFilter/SearchBarWithFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import Fade from "@material-ui/core/Fade"
import InputAdornment from "@material-ui/core/InputAdornment"
import Menu from "@material-ui/core/Menu"
import MenuItem from "@material-ui/core/MenuItem"
import OutlinedInput from "@material-ui/core/OutlinedInput"
import { makeStyles } from "@material-ui/core/styles"
import TextField from "@material-ui/core/TextField"
import { Theme } from "@material-ui/core/styles/createMuiTheme"
import SearchIcon from "@material-ui/icons/Search"
import { FormikErrors, useFormik } from "formik"
import debounce from "just-debounce-it"
import { useCallback, useEffect, useState } from "react"
import { getValidationErrorMessage } from "../../api/errors"
import { getFormHelpers } from "../../util/formUtils"
import { CloseDropdown, OpenDropdown } from "../DropdownArrows/DropdownArrows"
import { Stack } from "../Stack/Stack"

Expand Down Expand Up @@ -42,7 +42,7 @@ export const SearchBarWithFilter: React.FC<SearchBarWithFilterProps> = ({
presetFilters,
error,
}) => {
const styles = useStyles()
const styles = useStyles({ error })

const form = useFormik<FilterFormValues>({
enableReinitialize: true,
Expand Down Expand Up @@ -71,8 +71,6 @@ export const SearchBarWithFilter: React.FC<SearchBarWithFilterProps> = ({
return () => debouncedOnFilter.cancel()
}, [debouncedOnFilter, form.values.query])

const getFieldHelpers = getFormHelpers<FilterFormValues>(form)

const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null)

const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
Expand All @@ -93,7 +91,7 @@ export const SearchBarWithFilter: React.FC<SearchBarWithFilterProps> = ({

return (
<Stack spacing={1} className={styles.root}>
<Stack direction="row" spacing={0} className={styles.filterContainer}>
<Stack direction="row" spacing={0}>
{presetFilters && presetFilters.length > 0 && (
<Button
aria-controls="filter-menu"
Expand All @@ -106,19 +104,18 @@ export const SearchBarWithFilter: React.FC<SearchBarWithFilterProps> = ({
)}

<form onSubmit={form.handleSubmit} className={styles.filterForm}>
<TextField
{...getFieldHelpers("query")}
className={styles.textFieldRoot}
<OutlinedInput
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this because TextField is a nightmare to style.

id="query"
name="query"
value={form.values.query}
error={!!error}
className={styles.inputStyles}
onChange={form.handleChange}
fullWidth
variant="outlined"
InputProps={{
startAdornment: (
<InputAdornment position="start">
<SearchIcon fontSize="small" />
</InputAdornment>
),
}}
startAdornment={
<InputAdornment position="start">
<SearchIcon fontSize="small" />
</InputAdornment>
}
/>
</form>

Expand Down Expand Up @@ -152,29 +149,39 @@ export const SearchBarWithFilter: React.FC<SearchBarWithFilterProps> = ({
)
}

const useStyles = makeStyles((theme) => ({
interface StyleProps {
error?: unknown
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit This could just be a boolean.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I'm putting up another PR right after this one with more style improvements and I promise I'll fix it there. 🀞

}

const useStyles = makeStyles<Theme, StyleProps>((theme) => ({
root: {
marginBottom: theme.spacing(2),
},
filterContainer: {
border: `1px solid ${theme.palette.divider}`,
borderRadius: theme.shape.borderRadius,
},
// necessary to expand the textField
// the length of the page (within the bordered filterContainer)
filterForm: {
width: "100%",
},
buttonRoot: {
border: "none",
borderRight: `1px solid ${theme.palette.divider}`,
border: `1px solid ${theme.palette.divider}`,
borderRight: "0px",
borderRadius: `${theme.shape.borderRadius}px 0px 0px ${theme.shape.borderRadius}px`,
},
textFieldRoot: {
margin: "0px",
"& fieldset": {
border: "none",
},
},
errorRoot: {
color: theme.palette.error.dark,
},
inputStyles: {
height: "100%",
width: "100%",
borderRadius: `0px ${theme.shape.borderRadius}px ${theme.shape.borderRadius}px 0px`,
color: theme.palette.primary.contrastText,
backgroundColor: theme.palette.background.paper,

"& fieldset": {
borderColor: theme.palette.divider,
"&MuiOutlinedInput-root:hover, &MuiOutlinedInput-notchedOutline": {
borderColor: (props) => props.error && theme.palette.error.contrastText,
},
},
},
}))