Skip to content

Commit 01b5872

Browse files
committed
Rename component -> as
1 parent 925124d commit 01b5872

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/elements/Field.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export function Field<T extends object, K extends keyof T, C extends React.Funct
1212
props: {
1313
form: FormState<T>;
1414
name: K;
15-
component?: C;
16-
} & Omit<ElementProps<C>, "form" | "name" | "component" | "value" | "onChange">
15+
as?: C;
16+
} & Omit<ElementProps<C>, "form" | "name" | "as" | "value" | "onChange">
1717
) {
18-
const { form, component = "input", ...rest } = props;
18+
const { form, as = "input", ...rest } = props;
1919
const { value, setValue } = useListener(form, props.name);
2020
const onChange = useCallback((ev: any) => setValue(ev.target?.value ?? ev), [setValue]);
21-
return React.createElement(component, { ...rest, value, onChange });
21+
return React.createElement(as, { ...rest, value, onChange });
2222
}

src/elements/FieldError.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ export function FieldError<
1313
props: {
1414
form: FormState<T, State, Error>;
1515
name: K;
16-
component?: C;
16+
as?: C;
1717
transformer?: (error: Error) => React.ReactNode;
18-
} & Omit<ElementProps<C>, "transformer" | "component" | "name" | "form" | "children">
18+
} & Omit<ElementProps<C>, "transformer" | "as" | "name" | "form" | "children">
1919
) {
20-
const { form, component = React.Fragment, transformer, ...rest } = props;
20+
const { form, as = React.Fragment, transformer, ...rest } = props;
2121
const { error } = useListener(form, props.name);
2222
if (!error || typeof error === "object") return null;
23-
return React.createElement(component, { ...rest, children: String(transformer ? transformer(error as Error) : error) });
23+
return React.createElement(as, { ...rest, children: String(transformer ? transformer(error as Error) : error) });
2424
}

testing/src/Fieldform.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ export function FieldForm() {
2929

3030
return (
3131
<form onSubmit={form.handleSubmit(submit)}>
32-
<Field form={form} name="email" component="input" />
33-
<Field form={form} name="email" component={Input} style={{ margin: "2em" }} />
34-
<FieldError form={form} name="email" component={Error} />
35-
<Field form={form} name="gender" component="select">
32+
<Field form={form} name="email" as="input" />
33+
<Field form={form} name="email" style={{ margin: "2em" }} />
34+
<Field form={form} name="email" as={Input} style={{ margin: "2em" }} />
35+
<FieldError form={form} name="email" as={Error} />
36+
<Field form={form} name="gender" as="select">
3637
<option value="male">male</option>
3738
<option value="female">female</option>
3839
</Field>

0 commit comments

Comments
 (0)