UNPKG

@nish1896/rhf-mui-components

Version:

A suite of 25+ production-ready react-hook-form components built with material-ui. Fully typed, tree-shakable, and optimized for enterprise-grade forms.

138 lines (137 loc) 5.53 kB
"use client"; import { RHFMuiConfigContext } from "../../config/ConfigProvider.js"; import { keepLabelAboveFormField, mergeRefs } from "../../utils/control.js"; import FormControl from "../../common/FormControl.js"; import FormHelperText from "../../common/FormHelperText.js"; import FormLabel from "../../common/FormLabel.js"; import FormLabelText from "../../common/FormLabelText.js"; import { fieldNameToLabel } from "../../utils/text-transform.js"; import { useFieldIds } from "../../utils/useFieldIds.js"; import { forwardRef, useContext, useState } from "react"; import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime"; import { Controller } from "react-hook-form"; import TextField from "@mui/material/TextField"; import IconButton from "@mui/material/IconButton"; import InputAdornment from "@mui/material/InputAdornment"; import VisibilityIcon from "@mui/icons-material/Visibility"; import VisibilityOffIcon from "@mui/icons-material/VisibilityOff"; //#region src/mui/password-input/index.tsx const RHFPasswordInput = forwardRef(function RHFPasswordInput({ fieldName, control, registerOptions, customOnChange, onValueChange, disabled: muiDisabled, label, showLabelAboveFormField, formLabelProps, hideLabel, showPasswordIcon, hidePasswordIcon, required, helperText, errorMessage, hideErrorMessage, formHelperTextProps, slotProps: muiSlotProps, onBlur: muiOnBlur, autoComplete = "off", customIds, ...otherPasswordInputProps }, ref) { const { fieldId, labelId, helperTextId, errorId } = useFieldIds(fieldName, customIds); const { allLabelsAboveFields } = useContext(RHFMuiConfigContext); const isLabelAboveFormField = keepLabelAboveFormField(showLabelAboveFormField, allLabelsAboveFields); const defaultFieldLabel = fieldNameToLabel(fieldName); const fieldLabel = label ?? defaultFieldLabel; const accessibleFieldLabel = typeof fieldLabel === "string" ? fieldLabel : defaultFieldLabel; const [showPassword, setShowPassword] = useState(false); const ShowPasswordIcon = showPasswordIcon ?? /* @__PURE__ */ jsx(VisibilityOffIcon, {}); const HidePasswordIcon = hidePasswordIcon ?? /* @__PURE__ */ jsx(VisibilityIcon, {}); const handleClickShowPassword = () => setShowPassword((show) => !show); const handleMouseDownPassword = (event) => { event.preventDefault(); }; const endAdornment = /* @__PURE__ */ jsx(InputAdornment, { position: "end", children: /* @__PURE__ */ jsx(IconButton, { type: "button", "aria-label": showPassword ? "Hide password" : "Show password", onClick: handleClickShowPassword, onMouseDown: handleMouseDownPassword, edge: "end", children: showPassword ? HidePasswordIcon : ShowPasswordIcon }) }); return /* @__PURE__ */ jsx(Controller, { name: fieldName, control, rules: registerOptions, render: ({ field: { name: rhfFieldName, value: rhfValue, onChange: rhfOnChange, onBlur: rhfOnBlur, ref: rhfRef, disabled: rhfDisabled }, fieldState: { error: fieldStateError } }) => { const isDisabled = muiDisabled || rhfDisabled; const fieldErrorMessage = fieldStateError?.message?.toString() ?? errorMessage; const isError = !!fieldErrorMessage; const showHelperTextElement = !!(helperText || isError && !hideErrorMessage); return /* @__PURE__ */ jsxs(FormControl, { error: isError, disabled: isDisabled, children: [ !hideLabel && /* @__PURE__ */ jsx(FormLabel, { label: fieldLabel, isVisible: isLabelAboveFormField, required, error: isError, disabled: isDisabled, formLabelProps: { ...formLabelProps, id: labelId, htmlFor: fieldId } }), /* @__PURE__ */ jsx(TextField, { ...otherPasswordInputProps, id: fieldId, name: rhfFieldName, inputRef: mergeRefs(rhfRef, ref), autoComplete, type: showPassword ? "text" : "password", label: !hideLabel && !isLabelAboveFormField ? /* @__PURE__ */ jsx(FormLabelText, { label: fieldLabel, required }) : void 0, value: rhfValue ?? "", disabled: isDisabled, onChange: (event) => { const changeEvent = event; const newValue = changeEvent.target.value; if (customOnChange) { customOnChange({ rhfOnChange, newValue, event: changeEvent }); return; } rhfOnChange(newValue); onValueChange?.({ newValue, event: changeEvent }); }, onBlur: (blurEvent) => { rhfOnBlur(); muiOnBlur?.(blurEvent); }, error: isError, slotProps: { ...muiSlotProps, htmlInput: { ...muiSlotProps?.htmlInput, "aria-labelledby": !hideLabel && isLabelAboveFormField ? labelId : void 0, "aria-label": hideLabel ? accessibleFieldLabel : void 0, "aria-describedby": showHelperTextElement ? isError ? errorId : helperTextId : void 0, "aria-required": required }, input: { ...muiSlotProps?.input, endAdornment: /* @__PURE__ */ jsxs(Fragment$1, { children: [(muiSlotProps?.input)?.endAdornment, endAdornment] }) } }, multiline: false }), /* @__PURE__ */ jsx(FormHelperText, { error: isError, errorMessage: fieldErrorMessage, hideErrorMessage, helperText, showHelperTextElement, formHelperTextProps: { ...formHelperTextProps, id: isError ? errorId : helperTextId } }) ] }); } }); }); //#endregion export { RHFPasswordInput as default };