|
| 1 | +import { minLength, useTranslate } from "react-admin"; |
| 2 | + |
| 3 | +import { useForm, FormProvider } from "react-hook-form"; |
| 4 | +import { Box, Button, InputAdornment } from "@mui/material"; |
| 5 | +import SearchIcon from "@mui/icons-material/Search"; |
| 6 | +import { TextInput, useListContext } from "react-admin"; |
| 7 | +import { useEffect } from "react"; |
| 8 | +import JobTypeRaFilter from "./JobRaTypeFilter"; |
| 9 | + |
| 10 | +type JobRaListFilterValues = { |
| 11 | + job_type?: string[]; |
| 12 | + search_query?: string; |
| 13 | +}; |
| 14 | + |
| 15 | +const JobRaListFilters = () => { |
| 16 | + const translate = useTranslate(); |
| 17 | + const { filterValues, setFilters } = useListContext(); |
| 18 | + const form = useForm({ defaultValues: filterValues }); |
| 19 | + |
| 20 | + const submit = form.handleSubmit((formValues: JobRaListFilterValues) => { |
| 21 | + if (Object.keys(formValues).length > 0) { |
| 22 | + setFilters(formValues); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + // fill up filters just after opening the page |
| 27 | + useEffect(() => { |
| 28 | + submit(); |
| 29 | + }, []); |
| 30 | + |
| 31 | + return ( |
| 32 | + <FormProvider {...form}> |
| 33 | + <form onSubmit={submit}> |
| 34 | + <Box display="flex" alignItems="flex-end"> |
| 35 | + <Box component="span" mr={2}> |
| 36 | + <JobTypeRaFilter /> |
| 37 | + </Box> |
| 38 | + |
| 39 | + <Box component="span" mr={2} sx={{ flex: 0.3 }}> |
| 40 | + {/* Not using SearchInput here because it doesn't match styles with other filters */} |
| 41 | + <TextInput |
| 42 | + source="search_query" |
| 43 | + InputProps={{ |
| 44 | + endAdornment: ( |
| 45 | + <InputAdornment position="end"> |
| 46 | + <SearchIcon color="disabled" /> |
| 47 | + </InputAdornment> |
| 48 | + ), |
| 49 | + }} |
| 50 | + validate={minLength(3)} |
| 51 | + label="resources.jobs.filters.search_query.label" |
| 52 | + helperText="resources.jobs.filters.search_query.helperText" |
| 53 | + /> |
| 54 | + </Box> |
| 55 | + |
| 56 | + <Box component="span" mb={4}> |
| 57 | + <Button |
| 58 | + variant="outlined" |
| 59 | + color="primary" |
| 60 | + type="submit" |
| 61 | + > |
| 62 | + {translate("resources.jobs.filters.apply_button")} |
| 63 | + </Button> |
| 64 | + </Box> |
| 65 | + </Box> |
| 66 | + </form> |
| 67 | + </FormProvider> |
| 68 | + ); |
| 69 | +}; |
| 70 | + |
| 71 | +export default JobRaListFilters; |
0 commit comments