@lark-apaas/coding-miaoda-sandbox-skills 0.1.57 → 0.1.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,8 +9,12 @@ Shadcn Form + React Hook Form + Zod 表单开发。
9
9
 
10
10
  ## Guidelines
11
11
 
12
- - 类型必须用 `z.infer` 推导,禁止手动定义
13
- - 所有字段必须有默认值
12
+ - 表单类型从 schema 推导:`z.input<typeof schema>` 用于 `defaultValues`、`reset`、控件和 `getValues`;`z.output<typeof schema>`(即 `z.infer`)用于默认 `raw: false` 的 resolver 解析后的 `handleSubmit` 回调。coerce、default、transform 不能假设输入输出相同。
13
+ - 输入输出相同时可用单泛型 `useForm<Input>`;不同时,单输入泛型可能通过类型检查,但提交回调的静态类型仍是较宽的输入类型,必须按输入安全处理。需要精确输出时用 `useForm<z.input<typeof schema>, unknown, z.output<typeof schema>>`,回调用 `SubmitHandler<z.output<typeof schema>>`。
14
+ - 初始化每个需要的控件值;schema 有 default 的可选输入可以省略,但 `reset` 仍接收输入类型。外部 `unknown` 不能直接 spread 到 DOM:先适配成控件接受的值。
15
+ - `z.coerce.number<number>()` 只适用于控件实际写入 number;泛型不改变运行时转换。下例将空值写成 NaN 以触发校验,而非让空串 coerce 为 0;只适用于无需保留编辑中间态的非负整数等输入。需要逐键保留小数、符号等中间态时,使用下方字符串兼容路径;旧 Zod 不支持该泛型时也用该路径,不强制升级。
16
+ - 需要 string→number transform 时,先确认实际 `FormField` 是否透传第三泛型;现有包装可能不支持,勿修改托管包装或把三库的支持等同于 UI 支持。可保持 schema、控件、reset 均为字符串,校验后在提交中逐字段转换(见下方兼容路径)。
17
+ - API 合同允许字段可选的查询、筛选、搜索表单,可直接提交与 API 合同匹配的解析输出。
14
18
  - 必须用 Shadcn 组件,禁止原生 input
15
19
  - 同行 FormField 必须等宽
16
20
  - 每个控件都要有能读出来的字段名(读屏和自动化验收都按它找控件):FormField 内用 `FormLabel` + `FormControl`,id 会自动关联;FormField 外的筛选、搜索控件用 `<Label htmlFor="x">` 配控件 `id="x"`,没有可见标签时加 `aria-label`。不要只靠 placeholder 或当前选中值表达字段含义
@@ -27,7 +31,7 @@ Shadcn Form + React Hook Form + Zod 表单开发。
27
31
 
28
32
  ```tsx
29
33
  import { zodResolver } from "@hookform/resolvers/zod";
30
- import { useForm } from "react-hook-form";
34
+ import { useForm, type SubmitHandler } from "react-hook-form";
31
35
  import { z } from "zod";
32
36
  import { Button } from "@/components/ui/button";
33
37
  import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
@@ -37,21 +41,36 @@ import { logger } from "@lark-apaas/client-toolkit/logger";
37
41
 
38
42
  const studentSchema = z.object({
39
43
  name: z.string().min(1, "姓名不能为空"),
40
- age: z.coerce.number().min(0, "年龄不能为负数"),
41
- grade: z.string().min(1, "请选择年级"),
44
+ age: z.coerce.number<number>().int().min(0, "年龄不能为负数").max(150, "年龄不能大于150"),
45
+ grade: z.string().optional().default("grade1"),
42
46
  });
43
47
 
44
- type StudentFormData = z.infer<typeof studentSchema>;
48
+ type StudentInput = z.input<typeof studentSchema>;
49
+ type StudentOutput = z.output<typeof studentSchema>;
50
+
51
+ interface CreateStudentRequest {
52
+ name: string;
53
+ age: number;
54
+ grade: string;
55
+ }
56
+
57
+ async function createStudent(payload: CreateStudentRequest) {
58
+ logger.info(payload);
59
+ }
45
60
 
46
61
  export function StudentForm() {
47
- const form = useForm<StudentFormData>({
62
+ const form = useForm<StudentInput, unknown, StudentOutput>({
48
63
  resolver: zodResolver(studentSchema),
49
- defaultValues: { name: "", age: 0, grade: "" },
64
+ defaultValues: { name: "", age: NaN },
50
65
  });
66
+ const onSubmit: SubmitHandler<StudentOutput> = async (data) => {
67
+ const payload: CreateStudentRequest = { name: data.name, age: data.age, grade: data.grade };
68
+ await createStudent(payload);
69
+ };
51
70
 
52
71
  return (
53
72
  <Form {...form}>
54
- <form onSubmit={form.handleSubmit((data) => logger.info(data))} className="space-y-6">
73
+ <form noValidate onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
55
74
  <div className="flex flex-wrap gap-4">
56
75
  <FormField control={form.control} name="name" render={({ field }) => (
57
76
  <FormItem className="flex-1">
@@ -63,16 +82,18 @@ export function StudentForm() {
63
82
  <FormField control={form.control} name="age" render={({ field }) => (
64
83
  <FormItem className="flex-1">
65
84
  <FormLabel>年龄 <span aria-hidden="true" className="text-destructive">*</span></FormLabel>
66
- <FormControl><Input type="number" {...field} /></FormControl>
85
+ <FormControl><Input {...field} type="number" min={0} max={150} step={1}
86
+ value={Number.isFinite(field.value) ? field.value : ""}
87
+ onChange={(event) => field.onChange(event.target.value === "" ? NaN : event.target.valueAsNumber)} /></FormControl>
67
88
  <FormMessage />
68
89
  </FormItem>
69
90
  )} />
70
91
  </div>
71
92
  <FormField control={form.control} name="grade" render={({ field }) => (
72
93
  <FormItem>
73
- <FormLabel>年级 <span aria-hidden="true" className="text-destructive">*</span></FormLabel>
74
- <Select onValueChange={field.onChange} defaultValue={field.value}>
75
- <FormControl><SelectTrigger><SelectValue placeholder="请选择" /></SelectTrigger></FormControl>
94
+ <FormLabel>年级</FormLabel>
95
+ <Select value={field.value ?? "grade1"} onValueChange={field.onChange}>
96
+ <FormControl><SelectTrigger onBlur={field.onBlur} ref={field.ref}><SelectValue placeholder="默认为一年级" /></SelectTrigger></FormControl>
76
97
  <SelectContent>
77
98
  <SelectItem value="grade1">一年级</SelectItem>
78
99
  <SelectItem value="grade2">二年级</SelectItem>
@@ -82,12 +103,41 @@ export function StudentForm() {
82
103
  </FormItem>
83
104
  )} />
84
105
  <Button type="submit">提交</Button>
106
+ <Button type="button" onClick={() => form.reset({ name: "", age: NaN })}>重置</Button>
85
107
  </form>
86
108
  </Form>
87
109
  );
88
110
  }
89
111
  ```
90
112
 
113
+ ### 字符串兼容路径
114
+
115
+ 按 Guidelines 的字符串兼容路径:用 string/refine 校验非空、有限值和业务范围。范围 0~24、步长 0.5 仅为示例,按业务调整;文本控件保留 `0.` 等中间态,步长在 schema 中校验。
116
+
117
+ ```tsx
118
+ const amountSchema = z.object({
119
+ amount: z.string().refine((text) => {
120
+ const n = Number(text);
121
+ return text.trim() !== "" && Number.isFinite(n) && n >= 0 && n <= 24 && Number.isInteger(n * 2);
122
+ }, "请输入 0~24 内步长为 0.5 的数值"),
123
+ });
124
+ type AmountValues = z.input<typeof amountSchema>;
125
+ const form = useForm<AmountValues>({ resolver: zodResolver(amountSchema), defaultValues: { amount: "" } });
126
+ const submit: SubmitHandler<z.output<typeof amountSchema>> = async (data) => {
127
+ const request: SaveRequest = { amount: Number(data.amount) };
128
+ await api.save(request);
129
+ };
130
+ // 在重置动作中调用 form.reset({ amount: "" });reset 使用输入字符串。
131
+ // 在 <Form {...form}><form onSubmit={form.handleSubmit(submit)}> 中:
132
+ <FormField control={form.control} name="amount" render={({ field }) => (
133
+ <FormItem>
134
+ <FormLabel>数值</FormLabel>
135
+ <FormControl><Input type="text" inputMode="decimal" {...field} /></FormControl>
136
+ <FormMessage />
137
+ </FormItem>
138
+ )} />
139
+ ```
140
+
91
141
  ### 推荐的多步骨架
92
142
 
93
143
  ```tsx
@@ -160,7 +210,7 @@ const form = useForm<FormData>({ resolver: zodResolver(schema), defaultValues: {
160
210
  )} />
161
211
 
162
212
  // 必填 Request/DTO 桥接:zodResolver 负责运行时校验,DTO 边界负责静态类型匹配。
163
- // 用 handleSubmit 回调的 data,不要用 getValues(拿到的是 coerce 前的原始值)。
213
+ // 用 handleSubmit 回调的解析输出 data;getValues 返回输入值。
164
214
  import type { SaveRequest } from "@/shared/api";
165
215
  form.handleSubmit(async (data) => {
166
216
  const request: SaveRequest = { field: data.field };
@@ -9,10 +9,15 @@ Shadcn Form + React Hook Form + Zod 表单开发。
9
9
 
10
10
  ## Guidelines
11
11
 
12
- - 表单类型由 zod schema 推导;所有字段必须有 `defaultValues`;运行时校验用 `zodResolver`
12
+ - 表单类型从 schema 推导:`z.input<typeof schema>` 用于 `defaultValues`、`reset`、控件和 `getValues`;`z.output<typeof schema>`(即 `z.infer`)用于默认 `raw: false` 的 resolver 解析后的 `handleSubmit` 回调。coerce、default、transform 不能假设输入输出相同。
13
+ - 输入输出相同时可用单泛型 `useForm<Input>`;不同时,单输入泛型可能通过类型检查,但提交回调的静态类型仍是较宽的输入类型,必须按输入安全处理。需要精确输出时用 `useForm<z.input<typeof schema>, unknown, z.output<typeof schema>>`,回调用 `SubmitHandler<z.output<typeof schema>>`。
14
+ - 初始化每个需要的控件值;schema 有 default 的可选输入可以省略,但 `reset` 仍接收输入类型。外部 `unknown` 不能直接 spread 到 DOM:先适配成控件接受的值。
15
+ - `z.coerce.number<number>()` 只适用于控件实际写入 number;泛型不改变运行时转换。下例将空值写成 NaN 以触发校验,而非让空串 coerce 为 0;只适用于无需保留编辑中间态的非负整数等输入。需要逐键保留小数、符号等中间态时,使用下方字符串兼容路径;旧 Zod 不支持该泛型时也用该路径,不强制升级。
16
+ - 需要 string→number transform 时,先确认实际 `FormField` 是否透传第三泛型;现有包装可能不支持,勿修改托管包装或把三库的支持等同于 UI 支持。可保持 schema、控件、reset 均为字符串,校验后在提交中逐字段转换(见下方兼容路径)。
17
+ - 运行时校验使用 `zodResolver`。
13
18
  - 提交到必填 API/service Request 时,submit 的类型边界用业务 Request:Request 类型可读就 `const payload: XxxRequest = { ... }` 逐字段补齐;不可读但函数签名可读用 `Parameters<typeof submit>[0]` 派生
14
- - 补 `useForm<TFieldValues, TContext, TTransformedValues>` 的泛型参数解决不了这个问题
15
- - API 合同允许字段可选的查询、筛选、搜索表单,继续提交 `z.infer` 值
19
+ - 三泛型可表达 resolver 的输入输出,但不会自动补齐必填 Request/DTO 字段。
20
+ - API 合同允许字段可选的查询、筛选、搜索表单,可直接提交与 API 合同匹配的解析输出。
16
21
  - 必须用 Shadcn 组件,禁止原生 input
17
22
  - 同行 FormField 必须等宽
18
23
  - 每个控件都要有能读出来的字段名(读屏和自动化验收都按它找控件):FormField 内用 `FormLabel` + `FormControl`,id 会自动关联;FormField 外的筛选、搜索控件用 `<Label htmlFor="x">` 配控件 `id="x"`,没有可见标签时加 `aria-label`。不要只靠 placeholder 或当前选中值表达字段含义
@@ -28,7 +33,7 @@ Shadcn Form + React Hook Form + Zod 表单开发。
28
33
 
29
34
  ```tsx
30
35
  import { zodResolver } from "@hookform/resolvers/zod";
31
- import { useForm } from "react-hook-form";
36
+ import { useForm, type SubmitHandler } from "react-hook-form";
32
37
  import { z } from "zod";
33
38
  import { Button } from "@/components/ui/button";
34
39
  import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
@@ -38,11 +43,12 @@ import { logger } from "@lark-apaas/client-toolkit-lite";
38
43
 
39
44
  const studentSchema = z.object({
40
45
  name: z.string().min(1, "姓名不能为空"),
41
- age: z.coerce.number().min(0, "年龄不能为负数"),
42
- grade: z.string().min(1, "请选择年级"),
46
+ age: z.coerce.number<number>().int().min(0, "年龄不能为负数").max(150, "年龄不能大于150"),
47
+ grade: z.string().optional().default("grade1"),
43
48
  });
44
49
 
45
- type StudentFormData = z.infer<typeof studentSchema>;
50
+ type StudentInput = z.input<typeof studentSchema>;
51
+ type StudentOutput = z.output<typeof studentSchema>;
46
52
 
47
53
  interface CreateStudentRequest {
48
54
  name: string;
@@ -55,23 +61,18 @@ async function createStudent(payload: CreateStudentRequest) {
55
61
  }
56
62
 
57
63
  export function StudentForm() {
58
- const form = useForm<StudentFormData>({
64
+ const form = useForm<StudentInput, unknown, StudentOutput>({
59
65
  resolver: zodResolver(studentSchema),
60
- defaultValues: { name: "", age: 0, grade: "" },
66
+ defaultValues: { name: "", age: NaN },
61
67
  });
62
-
63
- const handleSubmit = async (values: StudentFormData) => {
64
- const payload: CreateStudentRequest = {
65
- name: values.name,
66
- age: values.age,
67
- grade: values.grade,
68
- };
68
+ const onSubmit: SubmitHandler<StudentOutput> = async (data) => {
69
+ const payload: CreateStudentRequest = { name: data.name, age: data.age, grade: data.grade };
69
70
  await createStudent(payload);
70
71
  };
71
72
 
72
73
  return (
73
74
  <Form {...form}>
74
- <form onSubmit={form.handleSubmit(handleSubmit)} className="space-y-6">
75
+ <form noValidate onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
75
76
  <div className="flex flex-wrap gap-4">
76
77
  <FormField control={form.control} name="name" render={({ field }) => (
77
78
  <FormItem className="flex-1">
@@ -83,16 +84,18 @@ export function StudentForm() {
83
84
  <FormField control={form.control} name="age" render={({ field }) => (
84
85
  <FormItem className="flex-1">
85
86
  <FormLabel>年龄 <span aria-hidden="true" className="text-destructive">*</span></FormLabel>
86
- <FormControl><Input type="number" {...field} /></FormControl>
87
+ <FormControl><Input {...field} type="number" min={0} max={150} step={1}
88
+ value={Number.isFinite(field.value) ? field.value : ""}
89
+ onChange={(event) => field.onChange(event.target.value === "" ? NaN : event.target.valueAsNumber)} /></FormControl>
87
90
  <FormMessage />
88
91
  </FormItem>
89
92
  )} />
90
93
  </div>
91
94
  <FormField control={form.control} name="grade" render={({ field }) => (
92
95
  <FormItem>
93
- <FormLabel>年级 <span aria-hidden="true" className="text-destructive">*</span></FormLabel>
94
- <Select onValueChange={field.onChange} defaultValue={field.value}>
95
- <FormControl><SelectTrigger><SelectValue placeholder="请选择" /></SelectTrigger></FormControl>
96
+ <FormLabel>年级</FormLabel>
97
+ <Select value={field.value ?? "grade1"} onValueChange={field.onChange}>
98
+ <FormControl><SelectTrigger onBlur={field.onBlur} ref={field.ref}><SelectValue placeholder="默认为一年级" /></SelectTrigger></FormControl>
96
99
  <SelectContent>
97
100
  <SelectItem value="grade1">一年级</SelectItem>
98
101
  <SelectItem value="grade2">二年级</SelectItem>
@@ -102,12 +105,41 @@ export function StudentForm() {
102
105
  </FormItem>
103
106
  )} />
104
107
  <Button type="submit">提交</Button>
108
+ <Button type="button" onClick={() => form.reset({ name: "", age: NaN })}>重置</Button>
105
109
  </form>
106
110
  </Form>
107
111
  );
108
112
  }
109
113
  ```
110
114
 
115
+ ### 字符串兼容路径
116
+
117
+ 按 Guidelines 的字符串兼容路径:用 string/refine 校验非空、有限值和业务范围。范围 0~24、步长 0.5 仅为示例,按业务调整;文本控件保留 `0.` 等中间态,步长在 schema 中校验。
118
+
119
+ ```tsx
120
+ const amountSchema = z.object({
121
+ amount: z.string().refine((text) => {
122
+ const n = Number(text);
123
+ return text.trim() !== "" && Number.isFinite(n) && n >= 0 && n <= 24 && Number.isInteger(n * 2);
124
+ }, "请输入 0~24 内步长为 0.5 的数值"),
125
+ });
126
+ type AmountValues = z.input<typeof amountSchema>;
127
+ const form = useForm<AmountValues>({ resolver: zodResolver(amountSchema), defaultValues: { amount: "" } });
128
+ const submit: SubmitHandler<z.output<typeof amountSchema>> = async (data) => {
129
+ const request: SaveRequest = { amount: Number(data.amount) };
130
+ await api.save(request);
131
+ };
132
+ // 在重置动作中调用 form.reset({ amount: "" });reset 使用输入字符串。
133
+ // 在 <Form {...form}><form onSubmit={form.handleSubmit(submit)}> 中:
134
+ <FormField control={form.control} name="amount" render={({ field }) => (
135
+ <FormItem>
136
+ <FormLabel>数值</FormLabel>
137
+ <FormControl><Input type="text" inputMode="decimal" {...field} /></FormControl>
138
+ <FormMessage />
139
+ </FormItem>
140
+ )} />
141
+ ```
142
+
111
143
  ---
112
144
 
113
145
  ## Patterns
@@ -156,7 +188,7 @@ const form = useForm<FormData>({ resolver: zodResolver(schema), defaultValues: {
156
188
  )} />
157
189
 
158
190
  // 必填 Request/DTO 桥接:zodResolver 负责运行时校验,DTO 边界负责静态类型匹配。
159
- // 用 handleSubmit 回调的 data,不要用 getValues(拿到的是 coerce 前的原始值)。
191
+ // 用 handleSubmit 回调的解析输出 data;getValues 返回输入值。
160
192
  import type { SaveRequest } from "@/shared/api";
161
193
  form.handleSubmit(async (data) => {
162
194
  const request: SaveRequest = { field: data.field };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/coding-miaoda-sandbox-skills",
3
- "version": "0.1.57",
3
+ "version": "0.1.58",
4
4
  "description": "Miaoda 合并沙箱 skills 包(包含原 miaoda-skills 的 miaoda / miaoda-modern / miaoda-design / shared 四条业务线);发布公网 npm,随沙箱运行时经 update-skills 同步到 .agent/skills/",
5
5
  "type": "module",
6
6
  "files": [