2021-07-31 15:57:43 +00:00
|
|
|
import { forwardRef, PropsWithoutRef } from "react";
|
|
|
|
import { useFormContext } from "react-hook-form";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
export interface LabeledTextFieldProps extends PropsWithoutRef<JSX.IntrinsicElements["input"]> {
|
|
|
|
/** Field name. */
|
2021-07-31 15:57:43 +00:00
|
|
|
name: string;
|
2021-07-31 14:33:18 +00:00
|
|
|
/** Field label. */
|
2021-07-31 15:57:43 +00:00
|
|
|
label: string;
|
2021-07-31 14:33:18 +00:00
|
|
|
/** Field type. Doesn't include radio buttons and checkboxes */
|
2021-07-31 15:57:43 +00:00
|
|
|
type?: "text" | "password" | "email" | "number";
|
|
|
|
outerProps?: PropsWithoutRef<JSX.IntrinsicElements["div"]>;
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const LabeledTextField = forwardRef<HTMLInputElement, LabeledTextFieldProps>(
|
|
|
|
({ label, outerProps, name, ...props }, ref) => {
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
formState: { isSubmitting, errors },
|
2021-07-31 15:57:43 +00:00
|
|
|
} = useFormContext();
|
2021-07-31 14:33:18 +00:00
|
|
|
const error = Array.isArray(errors[name])
|
|
|
|
? errors[name].join(", ")
|
2021-07-31 15:57:43 +00:00
|
|
|
: errors[name]?.message || errors[name];
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div {...outerProps}>
|
|
|
|
<label>
|
|
|
|
{label}
|
|
|
|
<input disabled={isSubmitting} {...register(name)} {...props} />
|
|
|
|
</label>
|
|
|
|
|
|
|
|
{error && (
|
|
|
|
<div role="alert" style={{ color: "red" }}>
|
|
|
|
{error}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<style jsx>{`
|
|
|
|
label {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: start;
|
|
|
|
font-size: 1rem;
|
|
|
|
}
|
|
|
|
input {
|
|
|
|
font-size: 1rem;
|
|
|
|
padding: 0.25rem 0.5rem;
|
|
|
|
border-radius: 3px;
|
|
|
|
border: 1px solid purple;
|
|
|
|
appearance: none;
|
|
|
|
margin-top: 0.5rem;
|
|
|
|
}
|
|
|
|
`}</style>
|
|
|
|
</div>
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
export default LabeledTextField;
|