97 lines
2.8 KiB
TypeScript
97 lines
2.8 KiB
TypeScript
import { Input, InputNumber, Select, Switch } from "antd";
|
|
import type { TemplateAdminField } from "@personal-brand/api-client";
|
|
import { ImageUrlField } from "./ImageUrlField.js";
|
|
|
|
export function TemplateFieldRenderer({
|
|
field,
|
|
id,
|
|
value,
|
|
onChange,
|
|
}: {
|
|
field: TemplateAdminField;
|
|
id?: string;
|
|
value?: unknown;
|
|
onChange?: (value: unknown) => void;
|
|
}) {
|
|
if (field.input === "textarea")
|
|
return (
|
|
<Input.TextArea
|
|
{...(id ? { id } : {})}
|
|
value={typeof value === "string" ? value : ""}
|
|
onChange={(event) => onChange?.(event.target.value)}
|
|
rows={3}
|
|
maxLength={field.maxLength}
|
|
showCount={!!field.maxLength}
|
|
placeholder={field.placeholder}
|
|
/>
|
|
);
|
|
if (field.input === "image")
|
|
return (
|
|
<ImageUrlField
|
|
{...(id ? { id } : {})}
|
|
{...(field.placeholder ? { placeholder: field.placeholder } : {})}
|
|
{...(typeof value === "string" ? { value } : {})}
|
|
{...(onChange ? { onChange: (next: string) => onChange(next) } : {})}
|
|
/>
|
|
);
|
|
if (field.input === "phone")
|
|
return (
|
|
<Input
|
|
{...(id ? { id } : {})}
|
|
type="tel"
|
|
placeholder={field.placeholder}
|
|
value={typeof value === "string" ? value : ""}
|
|
onChange={(event) => onChange?.(event.target.value)}
|
|
/>
|
|
);
|
|
if (field.input === "number")
|
|
return (
|
|
<InputNumber
|
|
{...(id ? { id } : {})}
|
|
value={typeof value === "number" ? value : null}
|
|
onChange={(input) => onChange?.(input ?? undefined)}
|
|
min={0}
|
|
style={{ width: "100%" }}
|
|
placeholder={field.placeholder}
|
|
/>
|
|
);
|
|
if (field.input === "boolean")
|
|
return (
|
|
<Switch
|
|
{...(id ? { id } : {})}
|
|
checked={Boolean(value)}
|
|
onChange={(checked) => onChange?.(checked)}
|
|
/>
|
|
);
|
|
if (field.input === "select")
|
|
return (
|
|
<Select
|
|
{...(id ? { id } : {})}
|
|
options={(field.options ?? []).map((item) => ({ ...item }))}
|
|
value={typeof value === "string" || typeof value === "number" ? value : undefined}
|
|
onChange={(selected) => onChange?.(selected)}
|
|
{...(field.placeholder ? { placeholder: field.placeholder } : {})}
|
|
/>
|
|
);
|
|
if (field.input === "multi-select")
|
|
return (
|
|
<Select
|
|
{...(id ? { id } : {})}
|
|
mode="multiple"
|
|
options={(field.options ?? []).map((item) => ({ ...item }))}
|
|
value={Array.isArray(value) ? value.map(String) : []}
|
|
onChange={(selected) => onChange?.(selected)}
|
|
placeholder={field.placeholder}
|
|
/>
|
|
);
|
|
return (
|
|
<Input
|
|
{...(id ? { id } : {})}
|
|
value={typeof value === "string" ? value : ""}
|
|
onChange={(event) => onChange?.(event.target.value)}
|
|
maxLength={field.maxLength}
|
|
placeholder={field.placeholder}
|
|
/>
|
|
);
|
|
}
|