feat: 完善模板字段编辑与分类治理
This commit is contained in:
1 parent
91322fa9a0
commit
26bcb2b66e
23 files changed
+631
-250
No files matched your search
@@ -1,16 +1,18 @@
|
||||
import { Image, Input, Space, Typography } from "antd";
|
||||
|
||||
interface ImageUrlFieldProps {
|
||||
id?: string;
|
||||
value?: string;
|
||||
onChange?: (value: string) => void;
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
export function ImageUrlField({ value, onChange, placeholder }: ImageUrlFieldProps) {
|
||||
export function ImageUrlField({ id, value, onChange, placeholder }: ImageUrlFieldProps) {
|
||||
const imageUrl = typeof value === "string" ? value.trim() : "";
|
||||
return (
|
||||
<Space direction="vertical" size={8} style={{ width: "100%" }}>
|
||||
<Input
|
||||
{...(id ? { id } : {})}
|
||||
value={value}
|
||||
onChange={(event) => onChange?.(event.target.value)}
|
||||
placeholder={placeholder ?? "粘贴图片地址"}
|
||||
|
||||
@@ -1,29 +1,11 @@
|
||||
import {
|
||||
Alert,
|
||||
Button,
|
||||
Col,
|
||||
Form,
|
||||
Input,
|
||||
InputNumber,
|
||||
Row,
|
||||
Select,
|
||||
Space,
|
||||
Switch,
|
||||
Typography,
|
||||
message,
|
||||
} from "antd";
|
||||
import { Alert, Button, Col, Form, Row, Space, Typography, message } from "antd";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import type {
|
||||
Offering,
|
||||
TemplateAdminField,
|
||||
TemplateAdminSection,
|
||||
TrustContent,
|
||||
} from "@personal-brand/api-client";
|
||||
import type { Offering, TemplateAdminSection, TrustContent } from "@personal-brand/api-client";
|
||||
import { createApiClient } from "../lib/api.js";
|
||||
import { ImageUrlField } from "./ImageUrlField.js";
|
||||
import { TemplateListField } from "./TemplateListField.js";
|
||||
import { TemplateFieldRenderer } from "./TemplateFieldRenderer.js";
|
||||
import { MutationError } from "./QueryState.js";
|
||||
import {
|
||||
applyPresentationValues,
|
||||
@@ -45,51 +27,33 @@ export interface RecordContentEditorProps {
|
||||
visualSelection?: { targetKey: string; page: string };
|
||||
}
|
||||
|
||||
function renderField(field: TemplateAdminField) {
|
||||
if (field.input === "textarea")
|
||||
return (
|
||||
<Input.TextArea
|
||||
rows={3}
|
||||
maxLength={field.maxLength}
|
||||
showCount={!!field.maxLength}
|
||||
placeholder={field.placeholder}
|
||||
/>
|
||||
);
|
||||
if (field.input === "image")
|
||||
return <ImageUrlField {...(field.placeholder ? { placeholder: field.placeholder } : {})} />;
|
||||
if (field.input === "phone") return <Input type="tel" placeholder={field.placeholder} />;
|
||||
if (field.input === "number") return <InputNumber min={0} placeholder={field.placeholder} />;
|
||||
if (field.input === "boolean") return <Switch />;
|
||||
if (field.input === "select")
|
||||
return <Select options={(field.options ?? []).map((item) => ({ ...item }))} />;
|
||||
if (field.input === "multi-select")
|
||||
return (
|
||||
<Select
|
||||
mode="multiple"
|
||||
options={(field.options ?? []).map((item) => ({ ...item }))}
|
||||
placeholder={field.placeholder}
|
||||
/>
|
||||
);
|
||||
return <Input placeholder={field.placeholder} />;
|
||||
}
|
||||
|
||||
export function RecordContentFields({ section }: { section: TemplateAdminSection }) {
|
||||
export function RecordContentFields({
|
||||
section,
|
||||
sectionKey = section.key,
|
||||
}: {
|
||||
section: TemplateAdminSection;
|
||||
sectionKey?: string;
|
||||
}) {
|
||||
return (
|
||||
<Row gutter={16}>
|
||||
{section.fields.map((field) => (
|
||||
<Col key={field.key} xs={24} md={24}>
|
||||
{field.input === "list" ? (
|
||||
<TemplateListField field={field} name={presentationFieldName(section.key, field.key)} />
|
||||
<TemplateListField field={field} name={presentationFieldName(sectionKey, field.key)} />
|
||||
) : (
|
||||
<Form.Item
|
||||
name={presentationFieldName(section.key, field.key)}
|
||||
name={presentationFieldName(sectionKey, field.key)}
|
||||
label={field.label}
|
||||
htmlFor={presentationFieldName(sectionKey, field.key)}
|
||||
{...(field.help ? { help: field.help } : {})}
|
||||
{...(field.required
|
||||
? { rules: [{ required: true, message: `请填写${field.label}` }] }
|
||||
: {})}
|
||||
>
|
||||
{renderField(field)}
|
||||
<TemplateFieldRenderer
|
||||
field={field}
|
||||
id={presentationFieldName(sectionKey, field.key)}
|
||||
/>
|
||||
</Form.Item>
|
||||
)}
|
||||
</Col>
|
||||
@@ -111,6 +75,19 @@ export function RecordContentEditor({
|
||||
const [form] = Form.useForm<PresentationValues>();
|
||||
const queryClient = useQueryClient();
|
||||
const recordName = "name" in record ? record.name : record.title;
|
||||
const [draftValues, setDraftValues] = useState<PresentationValues>(() =>
|
||||
Object.fromEntries(
|
||||
section.fields.map((field) => [
|
||||
presentationFieldName(section.key, field.key),
|
||||
readPresentationValue(
|
||||
record.config,
|
||||
field.binding,
|
||||
field,
|
||||
record as unknown as Record<string, unknown>,
|
||||
),
|
||||
]),
|
||||
),
|
||||
);
|
||||
const initialValues = useMemo(
|
||||
() =>
|
||||
Object.fromEntries(
|
||||
@@ -139,7 +116,9 @@ export function RecordContentEditor({
|
||||
const changed = Object.fromEntries(
|
||||
Object.entries(standard).filter(
|
||||
([key, value]) =>
|
||||
key !== "config" && value !== (record as unknown as Record<string, unknown>)[key],
|
||||
key !== "config" &&
|
||||
(key in (record as unknown as Record<string, unknown>) ||
|
||||
value !== (record as unknown as Record<string, unknown>)[key]),
|
||||
),
|
||||
);
|
||||
const client = createApiClient();
|
||||
@@ -164,8 +143,24 @@ export function RecordContentEditor({
|
||||
form={form}
|
||||
layout="vertical"
|
||||
initialValues={initialValues}
|
||||
onValuesChange={(_, values) => onDraft?.(values)}
|
||||
onFinish={(values) => save.mutate(values)}
|
||||
onValuesChange={(_, values) => {
|
||||
const currentValues = form.getFieldsValue(true) as PresentationValues;
|
||||
const next = {
|
||||
...draftValues,
|
||||
...values,
|
||||
...currentValues,
|
||||
};
|
||||
setDraftValues(next);
|
||||
onDraft?.(next);
|
||||
}}
|
||||
onFinish={(values) => {
|
||||
const currentValues = form.getFieldsValue(true) as PresentationValues;
|
||||
save.mutate({
|
||||
...draftValues,
|
||||
...values,
|
||||
...currentValues,
|
||||
});
|
||||
}}
|
||||
style={{ minHeight: 0, display: "flex", flexDirection: "column" }}
|
||||
>
|
||||
<Alert
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
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}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,10 @@
|
||||
import { Button, Form, Input, Space } from "antd";
|
||||
import { Button, Card, Collapse, Empty, Form, Input, Space, Typography } from "antd";
|
||||
import {
|
||||
CaretDownOutlined,
|
||||
CaretUpOutlined,
|
||||
DeleteOutlined,
|
||||
PlusOutlined,
|
||||
} from "@ant-design/icons";
|
||||
import type { TemplateAdminField } from "@personal-brand/api-client";
|
||||
|
||||
export function TemplateListField({
|
||||
@@ -22,42 +28,113 @@ export function TemplateListField({
|
||||
]}
|
||||
>
|
||||
{(rows, { add, remove, move }, { errors }) => (
|
||||
<Space direction="vertical" style={{ width: "100%" }}>
|
||||
{rows.map((row, index) => (
|
||||
<Space key={row.key} align="start" wrap>
|
||||
{field.itemFields?.map((itemField) => (
|
||||
<Form.Item
|
||||
key={itemField.key}
|
||||
name={[row.name, itemField.key]}
|
||||
label={`${index + 1}. ${itemField.label}`}
|
||||
rules={
|
||||
itemField.required === false
|
||||
? []
|
||||
: [{ required: true, message: `请填写${itemField.label}` }]
|
||||
}
|
||||
>
|
||||
{itemField.input === "textarea" ? <Input.TextArea rows={2} /> : <Input />}
|
||||
</Form.Item>
|
||||
))}
|
||||
<Button onClick={() => remove(row.name)}>删除</Button>
|
||||
<Button
|
||||
aria-label={`上移第 ${index + 1} 项`}
|
||||
disabled={index === 0}
|
||||
onClick={() => move(index, index - 1)}
|
||||
<Space direction="vertical" size={12} style={{ width: "100%" }}>
|
||||
{rows.length === 0 ? (
|
||||
<Empty
|
||||
image={Empty.PRESENTED_IMAGE_SIMPLE}
|
||||
description={`还没有${field.label}内容`}
|
||||
/>
|
||||
) : null}
|
||||
{rows.map((row, index) => {
|
||||
const titleField = field.itemFields?.[0];
|
||||
const primaryTitle = titleField ? (
|
||||
<Form.Item
|
||||
noStyle
|
||||
shouldUpdate={(prev: unknown, next: unknown) => {
|
||||
const previous = Array.isArray(prev)
|
||||
? (prev[row.name] as Record<string, unknown> | undefined)
|
||||
: undefined;
|
||||
const nextRow = Array.isArray(next)
|
||||
? (next[row.name] as Record<string, unknown> | undefined)
|
||||
: undefined;
|
||||
return previous?.[titleField.key] !== nextRow?.[titleField.key];
|
||||
}}
|
||||
>
|
||||
上移
|
||||
</Button>
|
||||
<Button
|
||||
aria-label={`下移第 ${index + 1} 项`}
|
||||
disabled={index === rows.length - 1}
|
||||
onClick={() => move(index, index + 1)}
|
||||
{({ getFieldValue }) => {
|
||||
const value = String(
|
||||
getFieldValue([...name, row.name, titleField.key] as const) ?? "",
|
||||
);
|
||||
return (
|
||||
<Typography.Text
|
||||
strong
|
||||
>{`${index + 1}. ${value || titleField.label}`}</Typography.Text>
|
||||
);
|
||||
}}
|
||||
</Form.Item>
|
||||
) : (
|
||||
<Typography.Text strong>{`${index + 1}. ${field.label}`}</Typography.Text>
|
||||
);
|
||||
return (
|
||||
<Card
|
||||
key={row.key}
|
||||
size="small"
|
||||
title={primaryTitle}
|
||||
extra={
|
||||
<Space size={0}>
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
aria-label={`上移第 ${index + 1} 项`}
|
||||
icon={<CaretUpOutlined />}
|
||||
disabled={index === 0}
|
||||
onClick={() => move(index, index - 1)}
|
||||
/>
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
aria-label={`下移第 ${index + 1} 项`}
|
||||
icon={<CaretDownOutlined />}
|
||||
disabled={index === rows.length - 1}
|
||||
onClick={() => move(index, index + 1)}
|
||||
/>
|
||||
<Button
|
||||
type="text"
|
||||
danger
|
||||
size="small"
|
||||
aria-label={`删除第 ${index + 1} 项`}
|
||||
icon={<DeleteOutlined />}
|
||||
onClick={() => remove(row.name)}
|
||||
/>
|
||||
</Space>
|
||||
}
|
||||
>
|
||||
下移
|
||||
</Button>
|
||||
</Space>
|
||||
))}
|
||||
{field.itemFields?.map((itemField) => (
|
||||
<Form.Item
|
||||
key={itemField.key}
|
||||
name={[row.name, itemField.key]}
|
||||
label={itemField.label}
|
||||
rules={
|
||||
itemField.required === false
|
||||
? []
|
||||
: [{ required: true, message: `请填写${itemField.label}` }]
|
||||
}
|
||||
>
|
||||
{itemField.input === "textarea" ? <Input.TextArea rows={2} /> : <Input />}
|
||||
</Form.Item>
|
||||
))}
|
||||
</Card>
|
||||
);
|
||||
})}
|
||||
{rows.length > 1 ? (
|
||||
<Collapse
|
||||
ghost
|
||||
items={[
|
||||
{
|
||||
key: "sort-help",
|
||||
label: "排序说明",
|
||||
children: (
|
||||
<Typography.Text type="secondary">
|
||||
使用每张卡片右上角的箭头调整顺序,保存后预览也会按当前顺序展示。
|
||||
</Typography.Text>
|
||||
),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
) : null}
|
||||
<Form.ErrorList errors={errors} />
|
||||
<Button onClick={() => add()}>新增{field.label}</Button>
|
||||
<Button block icon={<PlusOutlined />} onClick={() => add()}>
|
||||
新增{field.label}
|
||||
</Button>
|
||||
</Space>
|
||||
)}
|
||||
</Form.List>
|
||||
|
||||
Reference in new issue
Block a user