fix: 修复组件预览底部按钮溢出与咨询表单缺失

This commit is contained in:
que01 committed 2026-10-09 15:12:40 +08:00
1 parent 5b85fe769f
commit fd2e970d8b
8 files changed
+151 -8

No files matched your search

@@ -35,6 +35,7 @@
}
.component-template-preview__canvas {
position: relative;
container-type: inline-size;
width: min(100%, 390px);
min-height: calc(100% - 48px);
@@ -1,19 +1,29 @@
import { TemplatePage as FoodTemplatePage } from "../../../../../packages/template-acheng-campus-food/taro/index.tsx";
import { TemplatePage as EditorialTemplatePage } from "../../../../../packages/template-linjing-editorial/taro/index.tsx";
import React from "react";
import { Inquiry } from "../../../../taro/src/components/Inquiry.tsx";
import { Records } from "../../../../taro/src/components/Records.tsx";
import { Success } from "../../../../taro/src/components/Success.tsx";
import { TemplateAssetContext } from "./taro-web-components.jsx";
function pageContent({ workspace, page }) {
if (page === "inquiry") return <Inquiry workspace={workspace} preview />;
if (page === "records") return <Records workspace={workspace} />;
if (page === "success") return <Success workspace={workspace} />;
return null;
}
function FoodPreview(props) {
return (
<TemplateAssetContext.Provider value="acheng-campus-food">
<FoodTemplatePage {...props} />
<FoodTemplatePage {...props}>{pageContent(props)}</FoodTemplatePage>
</TemplateAssetContext.Provider>
);
}
function EditorialPreview(props) {
return (
<TemplateAssetContext.Provider value="linjing-editorial">
<EditorialTemplatePage {...props} />
<EditorialTemplatePage {...props}>{pageContent(props)}</EditorialTemplatePage>
</TemplateAssetContext.Provider>
);
}
@@ -27,9 +27,76 @@ const element = (tag) =>
export const View = element("div");
export const Text = element("span");
export const Button = element("button");
export const Input = React.forwardRef((props, ref) =>
React.createElement("input", { ...props, ref }),
export const Input = React.forwardRef(({ onInput, ...props }, ref) =>
React.createElement("input", {
...props,
ref,
onInput: (event) => onInput?.({ detail: { value: event.currentTarget.value } }),
}),
);
export const Textarea = React.forwardRef(({ onInput, maxlength, ...props }, ref) =>
React.createElement("textarea", {
...props,
ref,
maxLength: maxlength,
onInput: (event) => onInput?.({ detail: { value: event.currentTarget.value } }),
}),
);
export function Picker({ children, mode, range = [], value, onChange }) {
return (
<div style={{ position: "relative" }}>
{children}
{mode === "date" ? (
<input
aria-label="选择日期"
type="date"
value={value || ""}
onChange={(event) => onChange?.({ detail: { value: event.currentTarget.value } })}
style={{ position: "absolute", inset: 0, width: "100%", opacity: 0, cursor: "pointer" }}
/>
) : (
<select
aria-label="选择选项"
value={value ?? 0}
onChange={(event) => onChange?.({ detail: { value: event.currentTarget.value } })}
style={{ position: "absolute", inset: 0, width: "100%", opacity: 0, cursor: "pointer" }}
>
{range.map((label, index) => (
<option key={index} value={index}>
{label}
</option>
))}
</select>
)}
</div>
);
}
const CheckboxContext = React.createContext(null);
export function CheckboxGroup({ children, onChange }) {
const [selected, setSelected] = React.useState([]);
const update = (value, checked) => {
const next = checked ? [...selected, value] : selected.filter((item) => item !== value);
setSelected(next);
onChange?.({ detail: { value: next } });
};
return (
<CheckboxContext.Provider value={update}>
<div>{children}</div>
</CheckboxContext.Provider>
);
}
export function Checkbox({ value, checked }) {
const update = React.useContext(CheckboxContext);
return (
<input
type="checkbox"
value={value}
checked={Boolean(checked)}
onChange={(event) => update?.(value, event.currentTarget.checked)}
/>
);
}
export const Label = element("label");
export const Image = React.forwardRef(({ mode, src, style, ...props }, ref) => {
const key = React.useContext(TemplateAssetContext);
return React.createElement("img", {
@@ -1,4 +1,7 @@
import React from "react";
const noop = () => Promise.resolve();
export const useRouter = () => ({ params: {} });
export const useDidShow = (callback) => React.useEffect(callback, []);
const Taro = {
setStorageSync() {},
getStorageSync() {
+9 -1
View File
@@ -4,7 +4,10 @@ export function templatePreviewCss() {
postcssPlugin: "template-preview-css",
Once(root) {
const file = root.source?.input.file || "";
if (!file.includes("/packages/template-") || !file.endsWith("/taro/style.css")) return;
const templateStyle =
file.includes("/packages/template-") && file.endsWith("/taro/style.css");
const sharedFormStyle = file.endsWith("/apps/taro/src/components/forms.css");
if (!templateStyle && !sharedFormStyle) return;
root.walkRules((rule) => {
rule.selector = rule.selector
.split(",")
@@ -21,6 +24,11 @@ export function templatePreviewCss() {
.join(", ");
});
root.walkDecls((declaration) => {
// 组件预览中的固定按钮必须以页面画布为定位基准。
if (declaration.prop === "position" && declaration.value === "fixed")
declaration.value = "absolute";
// 模板页面的视口高度按手机画布宽度计算,避免引用 Admin 浏览器高度。
declaration.value = declaration.value.replace(/100vh/g, "178cqw");
declaration.value = declaration.value.replace(
/(-?(?:[0-9]*[.])?[0-9]+)px/g,
(_, value) => Number((Number(value) / 7.5).toFixed(8)) + "cqw",
+1
View File
@@ -14,6 +14,7 @@ export default defineConfig({
},
define: {
"process.env.TARO_PLATFORM": JSON.stringify("web"),
"process.env.TARO_ENV": JSON.stringify("h5"),
PREVIEW_ALL_TEMPLATES: JSON.stringify(true),
TEMPLATE_MANIFEST: JSON.stringify({ navigation: [] }),
},
+9 -2
View File
@@ -17,7 +17,13 @@ import type { Values } from "../runtime/content";
import { ensureSession, request, brandSlug } from "../runtime/api";
import { takeQuery, navigate } from "../runtime/navigation";
import "./forms.css";
export function Inquiry({ workspace }: { workspace: RuntimeWorkspace }) {
export function Inquiry({
workspace,
preview = false,
}: {
workspace: RuntimeWorkspace;
preview?: boolean;
}) {
const router = useRouter();
const [offeringId, setOfferingId] = useState(router.params.offeringId || "");
const form: RuntimeInquiryForm | undefined =
@@ -28,7 +34,7 @@ export function Inquiry({ workspace }: { workspace: RuntimeWorkspace }) {
const [sending, setSending] = useState(false);
const busy = useRef(false);
const p = presentation(workspace, "inquiry");
const food = TEMPLATE_KEY === "acheng-campus-food";
const food = workspace.brand.visualTemplateKey === "acheng-campus-food";
useDidShow(() => {
const query = takeQuery("inquiry");
if (query.offeringId) setOfferingId(query.offeringId);
@@ -40,6 +46,7 @@ export function Inquiry({ workspace }: { workspace: RuntimeWorkspace }) {
const setValue = (key: string, value: string | string[]) =>
setValues((current) => ({ ...current, [key]: value }));
async function submit() {
if (preview) return;
if (busy.current) return;
if (!form) {
setError("咨询表单暂不可用,请稍后重试");
+47 -1
View File
@@ -76,7 +76,19 @@ const runtimeWorkspace = {
},
},
],
inquiryForms: [],
inquiryForms: [
{
key: "brand-inquiry",
version: "1",
purpose: "inquiry",
target: "brand",
submissionAction: "create-inquiry",
fields: [
{ key: "name", label: "称呼", type: "text", required: true },
{ key: "message", label: "需求说明", type: "textarea", required: true },
],
},
],
trustItems: [{ ...adminTrust }],
trustPresentation: {},
capabilities: [],
@@ -89,6 +101,7 @@ const templateContent = {
{ key: "offerings", page: "offerings", label: "套餐餐品" },
{ key: "service", page: "service", label: "出摊服务" },
{ key: "detail", page: "detail", label: "详情页" },
{ key: "inquiry", page: "inquiry", label: "定制/咨询" },
],
assetSlots: [],
trustContentTypes: [
@@ -362,12 +375,45 @@ test("Admin 按通用预览目标筛选字段并同步品牌草稿", async ({ pa
await page.getByText("出摊服务", { exact: true }).click();
await expect(preview.locator(".schedule-card")).toContainText("周一到周五营业");
await page.getByText("详情页", { exact: true }).click();
const detailBounds = await preview.evaluate((element) => {
const canvas = element.querySelector(".component-template-preview__canvas")!;
const bar = element.querySelector(".food-detail .bar")!;
const canvasBox = canvas.getBoundingClientRect();
const barBox = bar.getBoundingClientRect();
return {
position: getComputedStyle(bar).position,
left: barBox.left - canvasBox.left,
right: canvasBox.right - barBox.right,
bottom: canvasBox.bottom - barBox.bottom,
};
});
expect(detailBounds.position).toBe("absolute");
expect(detailBounds.left).toBeGreaterThanOrEqual(-1);
expect(detailBounds.right).toBeGreaterThanOrEqual(-1);
expect(detailBounds.bottom).toBeGreaterThanOrEqual(-1);
await preview.locator(".food-detail .bar").scrollIntoViewIfNeeded();
await expect(preview.locator(".food-detail .bar button")).toBeVisible();
await preview.screenshot({ path: "../../.codex-tasks/preview-proportion/detail.png" });
await preview.locator('[data-editor-target="acheng-campus-food.detail.detailCopy"]').click();
await expect(preview.locator(".component-template-preview__outline.is-selected")).toHaveAttribute(
"data-selected-target",
"acheng-campus-food.detail.detailCopy",
);
await expect(page.getByLabel("价格标题")).toBeVisible();
await page.getByText("定制/咨询", { exact: true }).click();
await expect(preview.locator(".inquiry-form .form-field")).toHaveCount(2);
await expect(preview.locator(".inquiry-form")).toContainText("称呼");
await expect(preview.locator(".inquiry-form")).toContainText("需求说明");
await preview.locator(".inquiry-form").scrollIntoViewIfNeeded();
await preview.screenshot({ path: "../../.codex-tasks/preview-proportion/inquiry.png" });
const formOverflow = await preview
.locator(".inquiry-form")
.evaluate((form) => form.scrollWidth - form.clientWidth);
expect(formOverflow).toBeLessThanOrEqual(1);
await page.getByRole("button", { name: "浏览页面" }).click();
await preview.locator(".inquiry-form input").first().fill("预览访客");
await expect(preview.locator(".inquiry-form input").first()).toHaveValue("预览访客");
await page.getByRole("button", { name: "点选编辑" }).click();
await page.getByText("套餐餐品", { exact: true }).click();
await expect
.poll(() =>