Skip to content

Commit 31c1dc7

Browse files
committed
docs: general improvements
1 parent 9a4ba2a commit 31c1dc7

File tree

7 files changed

+119
-92
lines changed

7 files changed

+119
-92
lines changed

docs/assets/schema.png

94.1 KB
Loading

docs/basic_usage.md

Lines changed: 36 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ title: Basic usage
55

66
A basic usage of ReForm looks like this:
77

8-
First, create a lenses module that will be passed as a param to the ReFormNext functor:
8+
First, create a lenses module that will be passed as a param to the ReForm functor:
99

10-
```reason
10+
```ocaml
1111
module StateLenses = [%lenses
1212
type state = {
1313
description: string,
@@ -18,112 +18,71 @@ module StateLenses = [%lenses
1818
```
1919

2020
Now you can generate the actual form container component:
21-
```reason
22-
module PostAddForm = ReFormNext.Make(StateLenses);
21+
```ocaml
22+
module PostAddForm = ReForm.Make(StateLenses);
2323
```
2424

2525
After creating the container module, you can use it as a React component:
2626

27-
```reason
27+
```ocaml
2828
[@react.component]
2929
let make = () => {
30-
let {state, submit, getFieldState, handleChange}: PostAddForm.api =
30+
let reform =
3131
PostAddForm.use(
32+
~validationStrategy=OnDemand,
3233
~schema={
3334
PostAddForm.Validation.Schema([|
34-
Custom(
35-
Title,
36-
values =>
37-
Js.String.length(values.title) > 20
38-
? Error("Keep it short!") : Valid,
39-
),
35+
StringMin(Title, 20),
4036
StringNonEmpty(Description),
4137
Custom(
4238
AcceptTerms,
4339
values =>
4440
values.acceptTerms == false
4541
? Error("You must accept all the terms") : Valid,
46-
),
42+
)
4743
|]);
4844
},
4945
~onSubmit=
5046
({state}) => {
51-
yourLogic()
52-
47+
Js.log2("title", state.values.description);
48+
Js.log2("description", state.values.description);
49+
Js.log2("acceptTerms", state.values.description);
5350
None;
5451
},
5552
~initialState={title: "", description: "", acceptTerms: false},
5653
(),
5754
);
5855
56+
<PostAddForm.Provider value=reform>
5957
<form
6058
onSubmit={event => {
6159
ReactEvent.Synthetic.preventDefault(event);
62-
submit();
60+
reform.submit();
6361
}}>
64-
<label>
65-
<span> {"Title:" |> React.string} </span>
66-
<input
67-
value={state.values.title}
68-
onChange={ReForm.Helpers.handleDomFormChange(handleChange(Title))}
69-
/>
70-
<p>
71-
{getFieldState(Field(Title))
72-
|> (
73-
fun
74-
| Error(error) => Some(error)
75-
| _ => None
76-
)
77-
|> Belt.Option.getWithDefault(_, "")
78-
|> React.string}
79-
</p>
80-
</label>
81-
<label>
82-
<span> {"Description:" |> React.string} </span>
83-
<textarea
84-
value={state.values.description}
85-
rows=4
86-
onChange={ReForm.Helpers.handleDomFormChange(
87-
handleChange(Description),
88-
)}
89-
/>
90-
<p>
91-
{getFieldState(Field(Description))
92-
|> (
93-
fun
94-
| Error(error) => Some(error)
95-
| _ => None
96-
)
97-
|> Belt.Option.getWithDefault(_, "")
98-
|> React.string}
99-
</p>
100-
</label>
101-
<label>
102-
<p>
103-
<span> {"Accept terms? " |> React.string} </span>
104-
<input
105-
type_="checkbox"
106-
value={string_of_bool(state.values.acceptTerms)}
107-
onChange={event =>
108-
ReactEvent.Form.target(event)##checked
109-
|> handleChange(AcceptTerms)
110-
}
111-
/>
112-
</p>
113-
<p>
114-
{getFieldState(Field(AcceptTerms))
115-
|> (
116-
fun
117-
| Error(error) => Some(error)
118-
| _ => None
119-
)
120-
|> Belt.Option.getWithDefault(_, "")
121-
|> React.string}
122-
</p>
123-
</label>
124-
{state.formState == Submitting
62+
<FieldString field=StateLenses.Title label="Title" />
63+
<FieldString field=StateLenses.Description label="Description" />
64+
<PostAddForm.Field
65+
field=StateLenses.AcceptTerms
66+
render={({handleChange, error, value}) =>
67+
<label>
68+
<p>
69+
<span> {"Accept terms? " |> React.string} </span>
70+
<input
71+
type_="checkbox"
72+
value={string_of_bool(value)}
73+
onChange={event =>
74+
ReactEvent.Form.target(event)##checked |> handleChange
75+
}
76+
/>
77+
</p>
78+
<p> {error->Belt.Option.getWithDefault("")->React.string} </p>
79+
</label>
80+
}
81+
/>
82+
{reform.state.formState == Submitting
12583
? <p> {React.string("Saving...")} </p>
12684
: <button type_="submit"> {"Submit" |> React.string} </button>}
12785
</form>
86+
</PostAddForm.Provider>;
12887
};
12988
```

docs/getting_started.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
id: gettingStarted
3+
title: Getting Started
4+
---
5+
6+
## What this is and why
7+
8+
Code that deals with strongly typed forms can quickly become walls of repeated text.
9+
We created ReForm to be both deadly simple and to make forms sound good leveraging ReasonML's powerful typesytem.
10+
Even the schemas we use are nothing more than constructors built-in in the language itself with a small size footprint.
11+
12+
## Features
13+
14+
- Hook API
15+
- Schema API
16+
- Type safe, `handleChange` properly infers the value of the field it is handling
17+
- Context Provider
18+
- Field component
19+
- Validation strategy, OnDemand and OnChange

docs/installation.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,49 @@ title: Installation
44
sidebar_label: Installation
55
---
66

7-
Add via yarn
7+
### Pre-requisites
8+
9+
#### BS-Platform
10+
In order to start using Reform you need the basic tooling for ReasonML using [Buckescript](https://bucklescript.github.io/en/).
11+
12+
#### React
13+
Reform is a library for React applications, so we assume that you are already familiar with [React](https://reactjs.org/).
14+
15+
### Instalation and Setup
16+
17+
You’ll need to have [Node 8.16.0 or Node 10.16.0 or later version](https://nodejs.org/en/) on your local development machine (but it’s not required on the server). You can use `nvm` (macOS/Linux) or `nvm-windows` to easily switch Node versions between different projects.
18+
19+
If you want to create a ReasonReact development envinroment you can use the create-react-app to bootstrap a initial project.
20+
`yarn create react-app my-app --scripts-version reason-scripts`
21+
`yarn create` is available in [Yarn 0.25+](https://yarnpkg.com/lang/en/)
22+
23+
or
24+
25+
`npx create-react-app <app-name> --scripts-version reason-scripts`
26+
`npx` comes with npm 5.2+ and higher
27+
28+
It will create a directory called my-app inside the current folder.
29+
Inside that directory, it will generate the initial project structure.
30+
31+
Now you can install the package Reform to your existing project using yarn or npm:
832
```
933
yarn add bs-reform reschema
1034
```
11-
Then add to your bs-dependencies in bsconfig.json
35+
36+
Then add to your `bs-dependencies` in bsconfig.json
1237
```
1338
"bs-dependencies": [
1439
"bs-reform",
1540
"reschema"
1641
]
1742
```
1843

19-
Now add lenses
44+
Now add lenses, a package to generate GADT lenses for ReasonML.
2045
```
2146
yarn add -D lenses-ppx
2247
```
2348

24-
And update your bs-dependencies
49+
And update your `bs-dependencies`
2550
```
2651
"ppx-flags": [
2752
"lenses-ppx/ppx"

docs/schema.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,79 +8,79 @@ Validators are type safe and only accept the field of the type they are meant to
88

99
### Email(Lenses.field(string))
1010
Validates email values
11-
```reason
11+
```ocaml
1212
let schema = ReSchema.Validation([|
1313
Email(Lenses.Email),
1414
|]);
1515
```
1616

1717
### StringNonEmpty(Lenses.field(string))
1818
Requires that the string within is not empty
19-
```reason
19+
```ocaml
2020
let schema = ReSchema.Validation([|
2121
StringNonEmpty(Lenses.Name),
2222
|]);
2323
```
2424

2525
### NoValidation(Lenses.field('a))
2626
Does not apply any validation in the field
27-
```reason
27+
```ocaml
2828
let schema = ReSchema.Validation([|
2929
NoValidation(Lenses.Age),
3030
|]);
3131
```
3232

3333
### StringRegExp(Lenses.field(string), string)
3434
Set a custom RegExp to match the value
35-
```reason
35+
```ocaml
3636
let schema = ReSchema.Validation([|
3737
StringRegExp(Lenses.Name, "^[a-zA-Z\s]*$"),
3838
|]);
3939
```
4040

4141
### StringMin(Lenses.field(string), int)
4242
Set a minimum length for the string
43-
```reason
43+
```ocaml
4444
let schema = ReSchema.Validation([|
4545
StringMin(Lenses.Name, 3),
4646
|]);
4747
```
4848

4949
### StringMax(Lenses.field(string), int)
5050
Set a maximum length for the string
51-
```reason
51+
```ocaml
5252
let schema = ReSchema.Validation([|
5353
StringMax(Lenses.Name, 20),
5454
|]);
5555
```
5656

5757
### IntMin(Lenses.field(int), int)
5858
Set a minumum value for the int
59-
```reason
59+
```ocaml
6060
let schema = ReSchema.Validation([|
6161
IntMin(Lenses.Age, 21),
6262
|]);
6363
```
6464

6565
### IntMax(Lenses.field(int), int)
6666
Set a maximum value for the int
67-
```reason
67+
```ocaml
6868
let schema = ReSchema.Validation([|
6969
IntMax(Leses.Age, 60),
7070
|]);
7171
```
7272

7373
### FloatMin(Lenses.field(float), float)
7474
Set a minimum value for the float
75-
```reason
75+
```ocaml
7676
let schema = ReSchema.Validation([|
7777
FloatMin(Lenses.Price, 1.0),
7878
|]);
7979
```
8080

8181
### FloatMax(Lenses.field(float), float)
8282
Set a maximum value for the float
83-
```reason
83+
```ocaml
8484
let schema = ReSchema.Validation([|
8585
FloatMax(Lenses.Price, 100.0),
8686
|]);
@@ -89,3 +89,24 @@ let schema = ReSchema.Validation([|
8989
### Custom(Lenses.field('a), Lenses.state => fieldState)
9090
Useful for when you need to build your own custom logic.
9191
You can validate with props your component received - this is why we let you pass the schema as prop and not in the functor - or whatever you need.
92+
93+
In this example the "Title" input must have a length smaller than 20, the "Description" input must not be empty and the "AcceptTerms" must be checked.
94+
```reason
95+
PostAddForm.Validation.Schema([|
96+
Custom(
97+
Title,
98+
values =>
99+
Js.String.length(values.title) > 20
100+
? Error("Keep it short!") : Valid,
101+
),
102+
StringNonEmpty(Description),
103+
Custom(
104+
AcceptTerms,
105+
values =>
106+
values.acceptTerms == false
107+
? Error("You must accept all the terms") : Valid,
108+
),
109+
|]);
110+
```
111+
112+
![Example of Schema Validation](https://github.com/Astrocoders/reform/tree/master/docs/assets/schema.png)

website/i18n/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"doc5": {
1515
"title": "Fifth Document"
1616
},
17+
"gettingStarted": {
18+
"title": "Getting Started"
19+
},
1720
"i18n": {
1821
"title": "i18n"
1922
},

website/sidebars.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"docs": {
3-
"Setting up": ["installation"],
3+
"Setting up": ["gettingStarted", "installation"],
44
"Usage": ["basicUsage"],
55
"API": ["schema", "i18n"]
66
},

0 commit comments

Comments
 (0)