Skip to content

Commit 2199337

Browse files
committed
Update docs
1 parent 92ba2a3 commit 2199337

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

docs/_config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# theme: "just-the-docs"
2-
# baseurl: "/typed-react-form"
1+
theme: "just-the-docs"
2+
baseurl: "/typed-react-form"
33
remote_theme: pmarsceill/just-the-docs
44

55
aux_links:

docs/reference/Field.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,31 @@ const form = useForm({
127127

128128
### Styling/custom component
129129

130-
TODO
130+
You can pass a custom component to the `as` prop of `Field`. The props required by your component will be placed on the `Field` component (will be type-checked).
131+
132+
Some props of your custom component will be filled in automatically by the `Field` component:
133+
134+
- `onChange`: a change handler, this can be a `React.ChangeEventHandler` or a `(value: T) => void`, ready to be passed to your underlying input.
135+
- `value`: the current value in string format, ready to be passed to the underlying input element
136+
- `checked`: the current checked state as boolean, ready to be passed to the underlying input element
137+
- `disabled`: a boolean that will be true when submitting
138+
- `field`: a [`FormField`](/typed-react-form/reference/useListener.html#return-value) instance, which contains information about the field like the value, its error, the form it is part of and whether is has been modified.
139+
140+
```tsx
141+
function CustomInput(props: { value: any; onChange: React.ChangeEventHandler }) {
142+
return <input value={props.value} onChange={props.onChange} style={{ padding: "0.3em" }} />;
143+
}
144+
145+
// The value and onChange props are handled by Field
146+
<Field as={CustomInput} />;
147+
148+
function StyledCustomInput(props: { value: any; onChange: React.ChangeEventHandler; style: React.CSSProperties }) {
149+
return <input value={props.value} onChange={props.onChange} style={{ ...props.style, padding: "0.3em" }} />;
150+
}
151+
152+
// Style prop must be given (because it is required on the CustomInput component), typescript error otherwise
153+
<Field as={StyledCustomInput} style={{ color: "gray" }} />;
154+
```
131155

132156
### Submit
133157

docs/reference/useListener.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function BreadForm() {
8888

8989
## Return value
9090

91-
Returns a object containing the following fields, which you can destruct:
91+
Returns a `FormField` instance containing the following fields, which you can destruct:
9292

9393
```tsx
9494
return {
@@ -98,8 +98,8 @@ return {
9898
dirty, // True if the field is modified (if not default value anymore)
9999
error, // The error on this field
100100
state, // The state of the form (contains isSubmitting)
101-
form; // The form this field belongs to
102-
}
101+
form // The form this field belongs to (FormState instance)
102+
};
103103

104104
// Example usage
105105
const { value, setValue, error } = useListener(form, "fieldname");

testing/src/Fieldform.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,16 @@ export function FieldForm() {
4949
</form>
5050
);
5151
}
52+
53+
function CustomInput(props: { value: any; onChange: React.ChangeEventHandler; style: React.CSSProperties }) {
54+
return <input value={props.value} onChange={props.onChange} style={{ ...props.style, padding: "0.3em" }} />;
55+
}
56+
57+
function Testform() {
58+
const form = useForm({ nice: "" });
59+
return (
60+
<form>
61+
<Field form={form} name="nice" as={CustomInput} style={{ color: "gray" }} />;
62+
</form>
63+
);
64+
}

0 commit comments

Comments
 (0)