Skip to content

Commit 6630671

Browse files
committed
New docs (#57)
* more cases * fix test * use dumi * docs * typo * finish docs build * update CI config * restore ci config * adjust Node.js version for npm
1 parent b4c71a4 commit 6630671

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+16461
-9967
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717

1818
strategy:
1919
matrix:
20-
node-version: [12.x, 14.x, 16.x]
20+
node-version: [16.x]
2121

2222
steps:
2323
- uses: actions/checkout@v1

.github/workflows/doc.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ jobs:
1515
- name: Use Node.js
1616
uses: actions/setup-node@v1
1717
with:
18-
node-version: 12.x
18+
node-version: 16.x
1919
- name: Generate and deploy document
2020
uses: JamesIves/[email protected]
2121
env:
2222
BRANCH: gh-pages
23-
FOLDER: docs
23+
FOLDER: dumi/dist
2424
BUILD_SCRIPT: npm ci && npm run build:doc
2525
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
node_modules
2-
lib
3-
esm
4-
coverage
5-
docs
2+
/lib
3+
/esm
4+
/coverage
5+
.umi
66
.vscode

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
docs
1+
dumi
22
jest.config.js
33
tsconfig.json

dumi/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/src
2+
/dist

dumi/.umirc.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import path from 'path'
2+
import { defineConfig } from 'dumi'
3+
4+
const repo = 'formstate-x'
5+
6+
export default defineConfig({
7+
title: repo,
8+
favicon:
9+
'https://user-images.githubusercontent.com/9554297/83762004-a0761b00-a6a9-11ea-83b4-9c8ff721d4b8.png',
10+
logo:
11+
'https://user-images.githubusercontent.com/9554297/83762004-a0761b00-a6a9-11ea-83b4-9c8ff721d4b8.png',
12+
outputPath: `dist/${repo}`,
13+
mode: 'site',
14+
hash: true,
15+
// Because of using GitHub Pages
16+
base: `/${repo}/`,
17+
publicPath: `/${repo}/`,
18+
navs: [
19+
null,
20+
{
21+
title: 'GitHub',
22+
path: 'https://github.com/qiniu/formstate-x',
23+
},
24+
],
25+
alias: {
26+
'formstate-x': path.join(__dirname, 'src'),
27+
'formstate-x/bindings/react': path.join(__dirname, 'src/bindings/react')
28+
},
29+
mfsu: {}
30+
// more config: https://d.umijs.org/config
31+
})

dumi/docs/concepts/state/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: State
3+
order: 1
4+
toc: menu
5+
---
6+
7+
TODO
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: Validator
3+
order: 2
4+
toc: menu
5+
---
6+
7+
TODO
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react'
2+
import { observer } from 'mobx-react'
3+
import { FieldState, FormState, ProxyState } from 'formstate-x'
4+
import { TextField, Grid } from '@mui/material'
5+
import { bindTextField } from '../../../mui-binding'
6+
7+
export function createState() {
8+
const state = new FormState({
9+
first: new FieldState('').validators(required),
10+
last: new FieldState('').validators(required)
11+
})
12+
return new ProxyState(
13+
state,
14+
({ first, last }) => `${first} ${last}`,
15+
fullName => {
16+
const [first, last] = fullName.split(' ')
17+
return { first, last }
18+
}
19+
)
20+
}
21+
22+
interface Props {
23+
state: ReturnType<typeof createState>
24+
}
25+
26+
export default observer(function FullNameInput({ state }: Props) {
27+
return (
28+
<Grid container spacing={2}>
29+
<Grid item xs={12} sm={6}>
30+
<TextField label="First Name" margin="normal" {...bindTextField(state.$.$.first)} />
31+
</Grid>
32+
<Grid item xs={12} sm={6}>
33+
<TextField label="Last Name" margin="normal" {...bindTextField(state.$.$.last)} />
34+
</Grid>
35+
</Grid>
36+
)
37+
})
38+
39+
function required(v: string) {
40+
if (!v) return 'Please input!'
41+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React, { FormEvent } from 'react'
2+
import { observer } from 'mobx-react'
3+
import { FieldState, FormState } from 'formstate-x'
4+
import { Button, TextField, Container } from '@mui/material'
5+
6+
import { bindTextField } from '../../../mui-binding'
7+
import FullNameInput, { createState as createFullNameState } from './FullNameInput'
8+
9+
const form = new FormState({
10+
name: createFullNameState(),
11+
email: new FieldState('').validators(validateEmail)
12+
})
13+
14+
export default observer(function Demo() {
15+
16+
async function handleSubmit(e: FormEvent<HTMLFormElement>) {
17+
e.preventDefault()
18+
// trigger validation when user want to submit
19+
const result = await form.validate()
20+
// if error, do nothing (or maybe show a toast?)
21+
if (result.hasError) return
22+
// if no error, wo do next things, such as sending HTTP request
23+
console.log('Submit with:', result.value)
24+
}
25+
26+
return (
27+
<Container component="form" noValidate onSubmit={handleSubmit} maxWidth="xs">
28+
<FullNameInput state={form.$.name} />
29+
<TextField
30+
label="Email Address"
31+
margin="normal"
32+
fullWidth
33+
{...bindTextField(form.$.email)}
34+
/>
35+
<Button type="submit" fullWidth variant="contained" sx={{ mt: 3 }}>
36+
Submit
37+
</Button>
38+
</Container>
39+
)
40+
})
41+
42+
function validateEmail(v: string) {
43+
if (!v) return 'Please input your email!'
44+
}

0 commit comments

Comments
 (0)