Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit 542e8c4

Browse files
committed
docs: Update sidebar structure and create/update guides
1 parent fb492b3 commit 542e8c4

9 files changed

Lines changed: 286 additions & 65 deletions

File tree

docs/config/sidebar.yml

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# Sidebar navigation
22

3-
- label: 'Getting started'
3+
- label: 'Overview'
44
link: '/'
5-
- label: 'Why Unform?'
6-
link: '/why-unform'
7-
- label: 'FAQ'
8-
link: '/faq'
9-
- label: Migration from 1.x
10-
link: '/migration-guide'
11-
- label: 'Github'
12-
link: 'https://github.com/Rocketseat/unform'
5+
- label: 'Installation'
6+
link: '/installation'
7+
- label: 'Quick start'
8+
link: '/quick-start'
9+
- label: 'API reference'
10+
items:
11+
- label: 'useField'
12+
link: '/api/use-field'
13+
- label: 'Scope'
14+
link: '/api/scope'
15+
1316
- label: Guides
1417
items:
1518
- label: 'Basic form'
@@ -31,13 +34,6 @@
3134
- label: 'Manual form submit'
3235
link: '/guides/manual-form-submit'
3336

34-
- label: Playgrounds
35-
items:
36-
- label: 'ReactJS Live Demo'
37-
link: 'https://codesandbox.io/embed/agitated-tdd-uf177?autoresize=1&expanddevtools=1&fontsize=14&hidenavigation=1&theme=dark'
38-
- label: 'React Native Live Demo'
39-
link: 'https://snack.expo.io/@diego3g/1e9fb3'
40-
4137
- label: Recipes
4238
items:
4339
- label: 'Simple Input'
@@ -73,3 +69,16 @@
7369
link: '/examples/react-input-mask'
7470
- label: 'React Native Input Mask'
7571
link: '/examples/react-native-input-mask'
72+
73+
- label: More
74+
items:
75+
- label: Roadmap
76+
link: '/roadmap'
77+
- label: 'Migration guide'
78+
link: '/migration-guide'
79+
- label: 'Github'
80+
link: 'https://github.com/Rocketseat/unform'
81+
- label: 'React Demo'
82+
link: 'https://codesandbox.io/embed/agitated-tdd-uf177?autoresize=1&expanddevtools=1&fontsize=14&hidenavigation=1&theme=dark'
83+
- label: 'React Native Demo'
84+
link: 'https://snack.expo.io/@diego3g/1e9fb3'

docs/src/@rocketseat/gatsby-theme-docs/text/index.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ validate your fields, and have your data out of the box.
99

1010
## Designed to have zero design
1111

12-
As a form API, Unform was designed to have zero design. Easily integrate with any
13-
UI library or CSS-in-JS solution to build form components and display errors,
12+
As a form API, Unform was designed to have zero design. Easily integrate with
13+
UI libraries or CSS-in-JS solutions to build form components and display errors,
1414
change colors, add icons, and use your style guide.
1515

1616
## Extensible
1717

1818
Unform exposes hooks to integrate with any form library, such as React Select
19-
and React Datepicker. Access form data, errors, refs, and more manually from
19+
and React Datepicker. Access form data, errors, refs, and more from
2020
within your component using the form reference.
2121

2222
## Complex data and relationships

docs/src/docs/api/scope.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: 'Scope'
3+
description: 'Unform API reference for Scope'
4+
---
5+
6+
Scope is a Unform component to build complex structures.
7+
If you want to see it's use, check this [example](/guides/complex-structures).
8+
9+
## Props
10+
11+
- `path: string`
12+
- Required

docs/src/docs/api/use-field.mdx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
title: 'useField'
3+
description: 'Unform API reference for useField'
4+
---
5+
6+
`useField` is the main hook from Unform. To use it, you need to provide a name,
7+
passed as a parameter when declaring it on your functional component.
8+
9+
```jsx
10+
import { useField } from '@unform/core';
11+
12+
const { ... } = useField(name);
13+
```
14+
15+
## Properties
16+
17+
The following properties are returned from `useField`:
18+
19+
```jsx
20+
const { fieldName, registerField, defaultValue, clearError, error } = useField(
21+
name,
22+
);
23+
```
24+
25+
- `fieldName: string`
26+
- The field identifier;
27+
- `registerField: <T>(field: UnformField<T>): void`
28+
- The method used to register a field on Unform
29+
- `defaultValue: string`
30+
- The default value of the field
31+
- `clearError: () => void`
32+
- Clean error message
33+
- `error: string | undefined`
34+
- Get the error message
35+
36+
---
37+
38+
### registerField
39+
40+
When registering a field, you can pass some properties to the `registerField` method.
41+
42+
- `name: string`
43+
- Required
44+
- Field identifier
45+
- Always provide the `fieldName` returned from `useField`
46+
- `ref`
47+
- Required
48+
- The field ref
49+
- `setValue: (ref: any, value: T) => void`
50+
- Optional
51+
- Used to set the field value
52+
- `clearValue`
53+
- Optional
54+
- Cleans the field value
55+
- `getValue: (ref: any, newValue: T) => void`
56+
- Required (if you don't provide a `path`)
57+
- Can be used to transform a value before it is saved
58+
- `path` [deprecated]
59+
- Required (if you don't provide a `getValue`)
60+
- Deprecated in favour of `getValue`

docs/src/docs/faq.mdx

Lines changed: 0 additions & 21 deletions
This file was deleted.

docs/src/docs/installation.mdx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: 'Installation'
3+
description: 'How to install Unform for React and React Native'
4+
disableTableOfContents: true
5+
---
6+
7+
To get started, you need to install ou core package (`@unform/core`) and one
8+
package based on the client type:
9+
10+
> Unform is compatible with React >= v16.8.0
11+
12+
### React
13+
14+
```bash
15+
yarn add @unform/web @unform/core
16+
# or
17+
npm i @unform/web @unform/core
18+
```
19+
20+
### React Native
21+
22+
```bash
23+
yarn add @unform/mobile @unform/core
24+
# or
25+
npm i @unform/mobile @unform/core
26+
```

docs/src/docs/quick-start.mdx

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
title: 'Quick start'
3+
description: 'How to get started with Unform. You will learn how to build your first input and integrate with React.'
4+
---
5+
6+
This page explains hot to get started with Unform. You will learn
7+
how to build your first input and **integrate with React**.
8+
Remember that with Unform, **you are responsible for creating the UI** (markup and styles)
9+
of your form, but don't be intimidated!
10+
11+
Let's get started by creating a base input component.
12+
13+
```jsx lineNumbers
14+
import React from 'react';
15+
16+
const Input = () => {
17+
return <input type="text" placeholder="Type your username" />;
18+
};
19+
20+
export default Input;
21+
```
22+
23+
At the heart of every form component there are a `useField` hook. You will use it
24+
to register fields on Unform. It will also be used to integrate with form libraries,
25+
such as React Select, React Datepicker and others.
26+
27+
```jsx highlight={2,4-5,7,9-17} lineNumbers
28+
import React, { useEffect, useRef } from 'react';
29+
import { useField } from '@unform/core';
30+
31+
const Input = ({ name, ...rest }) => {
32+
const inputRef = useRef();
33+
34+
useEffect(() => {}, []);
35+
36+
return (
37+
<input
38+
name={name}
39+
ref={inputRef}
40+
type="text"
41+
placeholder="Type your username"
42+
{...rest}
43+
/>
44+
);
45+
};
46+
47+
export default Input;
48+
```
49+
50+
In the above code we are importing `useField` from `@unform/core`. We also
51+
created an empty `useEffect` and passed a prop `name` to our input.
52+
53+
> **Remember: when creating a form component, like this input, you always need to provide a ref.**
54+
55+
Now let's declare our `useField` hook passing to it as a parameter our `name`,
56+
it will return some methods and variables.
57+
58+
```jsx
59+
const { fieldName, defaultValue, registerField } = useField(name);
60+
```
61+
62+
For this example, we have used:
63+
64+
- `fieldName`: the input name;
65+
- `defaultValue`: the default value of our input (in case you provide a `initialData`);
66+
- `registerField`: the method that will be used to register a form field.
67+
68+
Here's how we can use the `registerField` method inside our `useEffect`:
69+
70+
```jsx
71+
useEffect(() => {
72+
registerField({
73+
name: fieldName,
74+
getValue: ref => {
75+
return ref.value;
76+
},
77+
ref: inputRef.current,
78+
});
79+
}, [fieldName, registerField]);
80+
```
81+
82+
It is a simple method that receives a:
83+
84+
- `name`: the `fieldName` returned from `registerField` (forms that use `Scope` have a different value from the name you provide);
85+
- `getValue`: The method used to return the input value. In a simple input, we just need to return `ref.value`;
86+
- `ref`: Input ref.
87+
88+
As mentioned, you are responsible for creating the UI. Here, we are just creating
89+
a simple input, but you could also add a label, a place to show a error message or anything
90+
you want. So let's continue the tutorial to use our recently created input. Start
91+
importing the `Form` component.
92+
93+
```jsx
94+
import { Form } from '@unform/web';
95+
```
96+
97+
Now you need to create a method to handle the form submit and also use our
98+
`Form` component, passing to it the submit method and a ref.
99+
100+
```jsx lineNumbers
101+
import React, { useRef } from 'react';
102+
import { Form } from '@unform/web';
103+
104+
const App = () => {
105+
const formRef = useRef();
106+
107+
const handleFormSubmit = data => {
108+
console.log(data);
109+
};
110+
111+
return <Form ref={formRef} onSubmit={handleFormSubmit} />;
112+
};
113+
114+
export default App;
115+
```
116+
117+
Inside your form, import the `Input` and use it like:
118+
119+
```jsx highlight={4,13-19} lineNumbers
120+
import React, { useRef } from 'react';
121+
import { Form } from '@unform/web';
122+
123+
import Input from '...';
124+
125+
const App = () => {
126+
const formRef = useRef();
127+
128+
const handleFormSubmit = data => {
129+
console.log(data);
130+
};
131+
132+
return (
133+
<Form ref={formRef} onSubmit={handleFormSubmit}>
134+
<Input name="username" placeholder="Choose a username" />
135+
136+
<button type="submit">Save</button>
137+
</Form>
138+
);
139+
};
140+
141+
export default App;
142+
```
143+
144+
If you save the form, something like this will show on console:
145+
146+
```js
147+
{
148+
username: 'hey';
149+
}
150+
```

docs/src/docs/roadmap.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: 'Roadmap'
3+
description: 'Our roadmap helps us share our priorities on the project.'
4+
---
5+
6+
Our roadmap helps us share our priorities. If Unform currently doesn't have a
7+
certain feature you think it's awesome, be sure to [check
8+
it](https://www.notion.so/Unform-public-roadmap-e4dff9e2053c4475b162cd19914eab02)
9+
to see if this is already planned for the future. Otherwise, we recommend you
10+
[create an issue](https://github.com/unform/unform/issues) describing your enhancement request.

docs/src/docs/why-unform.mdx

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)