50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { PasswordField } from "@govoplan/core-webui";
|
|
import { inputValueToFieldValue, normalizeFieldType, valueToInputText } from "../utils/fieldDefinitions";
|
|
|
|
export type FieldValueInputProps = {
|
|
fieldType?: string;
|
|
value: unknown;
|
|
disabled?: boolean;
|
|
placeholder?: string;
|
|
className?: string;
|
|
onChange: (value: unknown) => void;
|
|
};
|
|
|
|
export default function FieldValueInput({ fieldType = "string", value, disabled = false, placeholder, className, onChange }: FieldValueInputProps) {
|
|
const normalizedType = normalizeFieldType(fieldType);
|
|
if (normalizedType === "password") {
|
|
return (
|
|
<PasswordField
|
|
inputClassName={className}
|
|
value={valueToInputText(value, normalizedType)}
|
|
disabled={disabled}
|
|
generator
|
|
placeholder={placeholder}
|
|
autoComplete="new-password"
|
|
onValueChange={(nextValue) => onChange(inputValueToFieldValue(normalizedType, nextValue))}
|
|
/>
|
|
);
|
|
}
|
|
|
|
const inputType = inputTypeForField(normalizedType);
|
|
const step = normalizedType === "integer" ? "1" : normalizedType === "double" ? "any" : undefined;
|
|
|
|
return (
|
|
<input
|
|
className={className}
|
|
type={inputType}
|
|
step={step}
|
|
value={valueToInputText(value, normalizedType)}
|
|
disabled={disabled}
|
|
placeholder={placeholder}
|
|
onChange={(event) => onChange(inputValueToFieldValue(normalizedType, event.target.value))}
|
|
/>
|
|
);
|
|
}
|
|
|
|
function inputTypeForField(fieldType: string): string {
|
|
if (fieldType === "integer" || fieldType === "double") return "number";
|
|
if (fieldType === "date") return "date";
|
|
return "text";
|
|
}
|