All files / app/features/search SearchBox.tsx

86.2% Statements 25/29
100% Branches 3/3
71.42% Functions 5/7
85.71% Lines 24/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169                3x 3x 3x 3x 3x 3x 3x   3x   3x   9x                                                     3x               31x 31x   31x       31x 1x 1x       31x     17x 17x           31x                           31x                                                         1x 1x                                                                                            
import {
  FormControl,
  FormControlLabel,
  IconButton,
  InputAdornment,
  Radio,
  RadioGroup,
  Typography,
} from "@material-ui/core";
import { CrossIcon } from "components/Icons";
import LocationAutocomplete from "components/LocationAutocomplete";
import TextField from "components/TextField";
import { useTranslation } from "i18n";
import { GLOBAL, SEARCH } from "i18n/namespaces";
import { LngLat } from "maplibre-gl";
import { Dispatch, SetStateAction } from "react";
import { useForm } from "react-hook-form";
import { GeocodeResult } from "utils/hooks";
import makeStyles from "utils/makeStyles";
 
const useStyles = makeStyles((theme) => ({
  filterDialogButtonDesktop: {
    marginInlineStart: "auto",
  },
  mobileHide: {
    [theme.breakpoints.down("sm")]: {
      display: "none",
    },
  },
  flexRow: {
    display: "flex",
    width: "100%",
  },
  justifyContent: {
    justifyContent: "space-around",
  },
}));
 
interface SearchBoxProps {
  searchType: "location" | "keyword";
  setSearchType: Dispatch<"location" | "keyword">;
  locationResult: GeocodeResult;
  setLocationResult: Dispatch<SetStateAction<GeocodeResult>>;
  setQueryName: Dispatch<SetStateAction<string>>;
  queryName: string;
}
 
export default function SearchBox({
  searchType,
  setSearchType,
  locationResult,
  setLocationResult,
  setQueryName,
  queryName,
}: SearchBoxProps) {
  const { t } = useTranslation([GLOBAL, SEARCH]);
  const classes = useStyles();
 
  const { control, errors } = useForm({
    mode: "onChange",
  });
 
  const handleOnChangeAutocomplete = (event: GeocodeResult) => {
    if (event) {
      setLocationResult(event);
    }
  };
 
  const handleOnChangeKeyword = (
    event: React.ChangeEvent<HTMLInputElement>
  ) => {
    setQueryName(event.target.value);
    setLocationResult({
      ...locationResult,
      location: new LngLat(0, 0),
    });
  };
 
  const handleOnChangeRadioButton = (
    event: React.ChangeEvent<HTMLInputElement>,
    value: "location" | "keyword"
  ) => {
    setSearchType(value);
    setLocationResult({
      ...locationResult,
      name: "",
      simplifiedName: "",
      location: new LngLat(0, 0),
    });
    setQueryName("");
  };
 
  return (
    <>
      {searchType === "location" ? (
        <LocationAutocomplete
          control={control}
          name="location"
          defaultValue={locationResult}
          label={t("search:form.location_field_label")}
          onChange={handleOnChangeAutocomplete}
          fieldError={errors.location?.message}
          disableRegions
        />
      ) : (
        <TextField
          fullWidth
          id="query"
          value={queryName}
          label={t("search:form.keywords.field_label")}
          variant="standard"
          helperText=" "
          onChange={handleOnChangeKeyword}
          InputProps={{
            endAdornment: (
              <InputAdornment position="end">
                <IconButton
                  aria-label={t(
                    "search:form.keywords.clear_field_action_a11y_label"
                  )}
                  onClick={() => {
                    setQueryName("");
                    setLocationResult({
                      ...locationResult,
                      location: new LngLat(0, 0),
                    });
                  }}
                  size="small"
                >
                  <CrossIcon />
                </IconButton>
              </InputAdornment>
            ),
          }}
        />
      )}
      <div className={classes.flexRow}>
        <FormControl className={classes.flexRow} component="fieldset">
          <RadioGroup
            className={classes.justifyContent}
            row
            onChange={(event) => handleOnChangeRadioButton(event, searchType)}
            value={searchType}
          >
            <FormControlLabel
              value="location"
              control={<Radio />}
              label={
                <Typography variant="body2">
                  {t("search:form.by_location_filter_label")}
                </Typography>
              }
            />
            <FormControlLabel
              value="keyword"
              control={<Radio />}
              label={
                <Typography variant="body2">
                  {t("search:form.by_keyword_filter_label")}
                </Typography>
              }
            />
          </RadioGroup>
        </FormControl>
      </div>
    </>
  );
}