Skip to content

Commit c245c27

Browse files
author
acring
committed
fix: 修复 Form 无法传入 ref 的类型问题
Signed-off-by: acring <liu-zhen@xsky.com>
1 parent 3f12406 commit c245c27

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

apps/storybook/stories/Form.stories.tsx

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Button, Form, Input } from '@xsky/eris-ui';
1+
import { Button, Drawer, Form, Input } from '@xsky/eris-ui';
22
import { StoryObj } from '@storybook/react';
33
import * as yup from 'yup';
4+
import { useRef, useState } from 'react';
45

56
export default {
67
title: 'DATA ENTRY/Form/Form',
@@ -125,3 +126,65 @@ export const Default: FormStory = {
125126
export const WithWarningSchema: FormStory = {
126127
render: (args) => <Default.FormWithHook {...args} />,
127128
};
129+
130+
export const WithRef: FormStory = {
131+
render: (args) => {
132+
function WithRef() {
133+
const formRef = useRef<any>(null);
134+
const defaultValues = {
135+
name: '',
136+
};
137+
const schema = yup.object({
138+
name: yup.string().required('名称不能为空'),
139+
});
140+
const handleSubmit = (data: any) => {
141+
console.log('handleSubmit', data);
142+
alert(JSON.stringify(data));
143+
};
144+
const [open, setOpen] = useState(false);
145+
146+
return (
147+
<>
148+
<Button onClick={() => setOpen(true)}>打开表单</Button>
149+
<Drawer
150+
open={open}
151+
onCancel={() => setOpen(false)}
152+
onOk={() => {
153+
console.log('onOk');
154+
const result = formRef.current.submit();
155+
console.log('result', result);
156+
// setOpen(false);
157+
}}
158+
>
159+
<Form
160+
ref={formRef}
161+
defaultValues={defaultValues}
162+
schema={schema}
163+
onSubmit={handleSubmit}
164+
>
165+
<FormContent />
166+
</Form>
167+
</Drawer>
168+
</>
169+
);
170+
171+
function FormContent() {
172+
const {
173+
control,
174+
formState: { errors },
175+
} = Form.useFormContext();
176+
177+
return (
178+
<Form.Field label="名称" required errors={errors} name="name">
179+
<Form.Controller
180+
name="name"
181+
control={control}
182+
render={({ field }) => <Input {...field} />}
183+
/>
184+
</Form.Field>
185+
);
186+
}
187+
}
188+
return <WithRef />;
189+
},
190+
};

packages/ui/components/Form/Form.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export interface FormProps {
3434
id?: string;
3535
}
3636

37-
interface FormWithStatics extends React.ForwardRefExoticComponent<FormProps> {
37+
interface FormWithStatics
38+
extends React.ForwardRefExoticComponent<FormProps & React.RefAttributes<any>> {
3839
FieldGroup: typeof FieldGroup;
3940
Field: typeof Field;
4041
Errors: typeof Errors;
@@ -77,6 +78,7 @@ const Form = forwardRef((props: FormProps, ref) => {
7778

7879
useImperativeHandle(ref, () => ({
7980
submit: () => {
81+
debugger;
8082
if (Object.keys(errors).length === 0 && handleSubmit) {
8183
return handleSubmit();
8284
}

packages/ui/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@xsky/eris-ui",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"license": "MIT",
55
"scripts": {
66
"dev": "concurrently \"yarn dev:js:cjs\" \"yarn dev:js:esm\"",
@@ -19,7 +19,8 @@
1919
"ut:coverage": "FORCE_COLOR=1 vitest run --coverage",
2020
"json2exc:vi": "node ./scripts/json2exc.js vi",
2121
"json2exc:en": "node ./scripts/json2exc.js en",
22-
"i18n:scanner": "i18next-scanner --config ./i18next-scanner.config.js"
22+
"i18n:scanner": "i18next-scanner --config ./i18next-scanner.config.js",
23+
"release": "pnpm build && npm publish --access public"
2324
},
2425
"files": [
2526
"dist",

0 commit comments

Comments
 (0)