Skip to content

Commit

Permalink
fix autocomplete field and styling
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jul 25, 2024
1 parent 474059e commit 143bce3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/components/form/AutocompleteField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ import {
type TextFieldProps,
} from "@mui/material"
import { Field, type FieldConfig, type FieldProps } from "formik"
import { string as YupString, type ValidateOptions } from "yup"
import {
number as YupNumber,
string as YupString,
type ValidateOptions,
} from "yup"

import { schemaToFieldValidator } from "../../utils/form"
import { getNestedProperty } from "../../utils/general"

export interface AutocompleteFieldProps<
Value extends string | number,
Multiple extends boolean | undefined = false,
DisableClearable extends boolean | undefined = false,
FreeSolo extends boolean | undefined = false,
ChipComponent extends React.ElementType = ChipTypeMap["defaultComponent"],
> extends Omit<
AutocompleteProps<
string, // NOTE: force type to be string, not generic
Value,
Multiple,
DisableClearable,
FreeSolo,
Expand All @@ -44,6 +49,7 @@ export interface AutocompleteFieldProps<
}

const AutocompleteField = <
Value extends string | number,
Multiple extends boolean | undefined = false,
DisableClearable extends boolean | undefined = false,
FreeSolo extends boolean | undefined = false,
Expand All @@ -54,6 +60,7 @@ const AutocompleteField = <
validateOptions,
...otherAutocompleteProps
}: AutocompleteFieldProps<
Value,
Multiple,
DisableClearable,
FreeSolo,
Expand All @@ -63,12 +70,16 @@ const AutocompleteField = <

const dotPath = name.split(".")

let schema = YupString().oneOf(options, "not a valid option")
const message = "not a valid option"
let schema =
typeof options[0] === "string"
? YupString().oneOf(options as readonly string[], message)
: YupNumber().oneOf(options as readonly number[], message)
if (required) schema = schema.required()

const fieldConfig: FieldConfig = {
name,
type: "text",
type: typeof options[0] === "string" ? "text" : "number",
validate: schemaToFieldValidator(schema, validateOptions),
}

Expand All @@ -90,7 +101,7 @@ const AutocompleteField = <
id={name}
name={name}
required={required}
type="text"
type="text" // Force to be string to avoid number incrementor/decrementor
value={value}
error={touched && Boolean(error)}
helperText={(touched && error) as false | string}
Expand Down
11 changes: 11 additions & 0 deletions src/theme/components/MuiAutocomplete.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type Components from "./_components"

const MuiAutocomplete: Components["MuiAutocomplete"] = {
styleOverrides: {
root: {
width: "100%",
},
},
}

export default MuiAutocomplete
2 changes: 2 additions & 0 deletions src/theme/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type ThemeOptions } from "@mui/material"

import MuiAccordion from "./MuiAccordion"
import MuiAutocomplete from "./MuiAutocomplete"
import MuiButton from "./MuiButton"
import MuiCardActions from "./MuiCardActions"
import MuiCheckbox from "./MuiCheckbox"
Expand All @@ -27,6 +28,7 @@ import MuiTypography from "./MuiTypography"

const components: ThemeOptions["components"] = {
MuiAccordion,
MuiAutocomplete,
MuiButton,
MuiCardActions,
MuiCheckbox,
Expand Down

0 comments on commit 143bce3

Please sign in to comment.