update InfoInputLine to work with use-react-form

This commit is contained in:
Brandon Egger 2023-06-06 00:40:50 -05:00
parent cabddd777e
commit 0a42d34bb4
2 changed files with 20 additions and 7 deletions

View File

@ -127,12 +127,14 @@ function ResourceSummarySubForm({
</h2> </h2>
<span className="text-md"> <span className="text-md">
<InfoInputLine <InfoInputLine
details={register("manufacturer.name", { required: true })}
placeholder="manufacturer" placeholder="manufacturer"
value={resource?.manufacturer?.name ?? ""} value={resource?.manufacturer?.name ?? ""}
hint="manufacturer" hint="manufacturer"
/> />
</span> </span>
<InfoInputLine <InfoInputLine
details={register("name", { required: true })}
placeholder="name" placeholder="name"
value={resource?.name ?? ""} value={resource?.name ?? ""}
hint="name" hint="name"

View File

@ -1,38 +1,49 @@
import { useState } from "react"; import { useState } from "react";
import {
type UseFormRegisterReturn,
type InternalFieldName,
} from "react-hook-form";
/** /**
* Single line input for the fields found to the right of the * Single line input for the fields found to the right of the
* resource logo. * resource logo.
*/ */
const InfoInputLine = ({ function InfoInputLine<
TFieldName extends InternalFieldName = InternalFieldName
>({
value, value,
placeholder, placeholder,
hint, hint,
details,
}: { }: {
value: string; value: string;
placeholder: string; placeholder: string;
hint?: string; hint?: string;
}) => { details: UseFormRegisterReturn<TFieldName>;
const [currentValue, setCurrentValue] = useState<string>(value); }) {
const [curValue, setCurValue] = useState(value);
return ( return (
<div className="relative"> <div className="relative">
<input <input
{...details}
onChange={(event) => { onChange={(event) => {
setCurrentValue(event.target.value); setCurValue(event.target.value);
details.onChange(event).catch((e) => {
console.error(e);
});
}} }}
placeholder={placeholder} placeholder={placeholder}
value={currentValue}
type="text" type="text"
className="w-full border-b border-neutral-300 px-2" className="w-full border-b border-neutral-300 px-2"
/> />
<label className="absolute bottom-0 right-2 top-0 text-right text-sm"> <label className="absolute bottom-0 right-2 top-0 text-right text-sm">
{currentValue !== "" ? ( {curValue !== "" ? (
<span className="italic text-neutral-400">{hint}</span> <span className="italic text-neutral-400">{hint}</span>
) : undefined} ) : undefined}
</label> </label>
</div> </div>
); );
}; }
export { InfoInputLine }; export { InfoInputLine };