Skip to content

Commit 9f86e1f

Browse files
committed
Details (#69)
* remove isFormState * Simplify validation logic * typo * specify exports in src/index.ts * rename bindInput in docs react-bindings * remove bindInput * type details optimization * remove useless path mapping * Optimize test cases * remove bindInput in docs * field -> child state * clear validation info when ending validation
1 parent d8470b9 commit 9f86e1f

29 files changed

+227
-540
lines changed

dumi/.umirc.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ const repo = 'formstate-x'
55

66
export default defineConfig({
77
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',
8+
favicon: 'TODO',
9+
logo: 'TODO',
1210
outputPath: `dist/${repo}`,
1311
mode: 'site',
1412
hash: true,
@@ -23,8 +21,7 @@ export default defineConfig({
2321
},
2422
],
2523
alias: {
26-
'formstate-x': path.join(__dirname, 'src'),
27-
'formstate-x/bindings/react': path.join(__dirname, 'src/bindings/react')
24+
'formstate-x': path.join(__dirname, 'src')
2825
},
2926
mfsu: {}
3027
// more config: https://d.umijs.org/config

dumi/docs/guide/advanced/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ As we learnt from section [Reactive Validation](/guide/validation#reactive-valid
6363

6464
### Transformed State
6565

66-
When we create input components based on UI inputs, these input components always provide their own business semantics which differs from the semantics of the original UI inputs. Which means, sometimes there's a gap between the business-value and the view-value for one component.
66+
When we create input components based on UI inputs, these input components always provide their own business semantics which differ from the semantics of the original UI inputs. Which means, sometimes there's a gap between the business-value and the view-value for one component.
6767

6868
Let's take a component named `NumberInput` as example.
6969

@@ -77,7 +77,7 @@ A typical number input is made with a normal text input to collect user input. S
7777
const state = new FieldState('')
7878
```
7979

80-
However, as implementation details, the `string`-type value, is not the value we would like the consumer of `NumberInput` to access. The component should behave as its name (`NumberInput`) suggests, collecting `number`-type value from user.
80+
However, as implementation details, the `string`-type value, is not the value we would like the consumer of `NumberInput` to access. The component should behave as its name (`NumberInput`) suggests, to collect `number`-type value from user.
8181

8282
We need to transform the `string`-type value to number before we export it out. That's when `TransformedState` rescues:
8383

@@ -101,7 +101,7 @@ In section [Conditional Validation](/guide/validation#conditional-validation), w
101101

102102
However, sometimes we may add more than one validators for one state. It will not be convenient to add the `if` logic in each validator.
103103

104-
Sometimes we face complex states, which composed with many child states. It's not possible to change child states' validators' logic for the parent state. `formstate-x` provides a method `disableWhen` to help with such cases.
104+
Sometimes we face complex states, which are composed with child states. It's not proper to change validators' logic of child states when writing logic of parent states. `formstate-x` provides a method `disableWhen` to help with such cases.
105105

106106
Here is a demo:
107107

dumi/docs/guide/basic/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ As formstate-x is based on MobX, components who read state from formstate-x need
2828

2929
For convenience, we defined the field state (variable `state`) outside the component body. Actually, like any other MobX observable state, it's ok to be hold anywhere you like.
3030

31-
Now let's try some really cool thing. We will see how formstate-x provides powerful validating ability in section [Validation](/guide/validation).
31+
Now let's try some real rocking thing. We will see how formstate-x provides powerful validating ability in section [Validation](/guide/validation).

dumi/docs/guide/validation/async-validation.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import { observer } from 'mobx-react'
33
import { FieldState } from 'formstate-x'
4-
import { bindInputWithChangeEvent } from '../../react-bindings'
4+
import { bindInput } from '../../react-bindings'
55

66
function validateName(v: string) {
77
return new Promise<string | false>(resolve => {
@@ -19,7 +19,7 @@ export default observer(function Demo() {
1919
<>
2020
<label>
2121
Name:
22-
<input type="text" {...bindInputWithChangeEvent(state)} />
22+
<input type="text" {...bindInput(state)} />
2323
</label>
2424
{state.validating && <span style={tipStyle}>validating...</span>}
2525
{state.validated && state.hasError && <span style={errorTipStyle}>{state.error}</span>}

dumi/docs/guide/validation/conditional-validation.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import { observable } from 'mobx'
33
import { observer } from 'mobx-react'
44
import { FieldState } from 'formstate-x'
5-
import { bindInputWithChangeEvent } from '../../react-bindings'
5+
import { bindInput } from '../../react-bindings'
66

77
const validationCtrl = observable({
88
enabled: true,
@@ -27,7 +27,7 @@ export default observer(function Demo() {
2727
<p>
2828
<label>
2929
Name (5 characters at most):
30-
<input type="text" {...bindInputWithChangeEvent(state)} />
30+
<input type="text" {...bindInput(state)} />
3131
</label>
3232
{state.hasError && <span style={errorTipStyle}>{state.error}</span>}
3333
</p>

dumi/docs/guide/validation/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async function validateName(v: string) {
4848
return data.valid ? undefined : data.message
4949
} catch (e) {
5050
// If some exception (such as network error) occurs, this `catch` prevents formstate-x to throw.
51-
// The return value, instead, tells formstate-x to treat the value as invalid (with message "Validate failed")
51+
// The return value, instead, tells formstate-x to treat the value invalid (with message "Validate failed")
5252
return 'Validate failed'
5353
}
5454
}

dumi/docs/guide/validation/multiple-validator.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import { observer } from 'mobx-react'
33
import { FieldState } from 'formstate-x'
4-
import { bindInputWithChangeEvent } from '../../react-bindings'
4+
import { bindInput } from '../../react-bindings'
55

66
const validateNotEmpty = (v: string) => v.trim() === '' && 'Empty'
77
const validateLength = (v: string) => v.length > 5 && 'Too long'
@@ -16,7 +16,7 @@ export default observer(function Demo() {
1616
<>
1717
<label>
1818
Name:
19-
<input type="text" {...bindInputWithChangeEvent(state)} />
19+
<input type="text" {...bindInput(state)} />
2020
</label>
2121
{state.hasError && <span style={errorTipStyle}>{state.error}</span>}
2222
</>

dumi/docs/guide/validation/reactive-validation.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import { observable } from 'mobx'
33
import { observer } from 'mobx-react'
44
import { FieldState } from 'formstate-x'
5-
import { bindInputWithChangeEvent } from '../../react-bindings'
5+
import { bindInput } from '../../react-bindings'
66

77
const someObservableStore = observable({
88
strict: true,
@@ -34,7 +34,7 @@ export default observer(function Demo() {
3434
<p>
3535
<label>
3636
Name:
37-
<input type="text" {...bindInputWithChangeEvent(state)} />
37+
<input type="text" {...bindInput(state)} />
3838
</label>
3939
{state.hasError && <span style={errorTipStyle}>{state.error}</span>}
4040
</p>

dumi/docs/guide/validation/validation.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import { observer } from 'mobx-react'
33
import { FieldState } from 'formstate-x'
4-
import { bindInputWithChangeEvent } from '../../react-bindings'
4+
import { bindInput } from '../../react-bindings'
55

66
function validateName(v: string) {
77
return v.length > 5 && 'Too long'
@@ -14,7 +14,7 @@ export default observer(function Demo() {
1414
<>
1515
<label>
1616
Name (5 characters at most):
17-
<input type="text" {...bindInputWithChangeEvent(state)} />
17+
<input type="text" {...bindInput(state)} />
1818
</label>
1919
{state.hasError && <span style={errorTipStyle}>{state.error}</span>}
2020
</>

dumi/docs/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ yarn add formstate-x
2929
### Usage
3030

3131
```javascript
32-
import { FieldState, FormState, bindInput } from 'formstate-x'
32+
import { FieldState, FormState } from 'formstate-x'
3333

3434
const form = new FormState({
3535
foo: new FieldState(''),
@@ -50,16 +50,16 @@ async function handleSubmit(e) {
5050

5151
// when render (with react)
5252
<form onSubmit={handleSubmit}>
53-
<FooInput {...bindInput(form.$.foo)}>
54-
<BarInput {...bindInput(form.$.bar)}>
53+
<FooInput value={form.$.foo.value} onChange={v => form.$.foo.onChange(v)}>
54+
<BarInput value={form.$.bar.value} onChange={v => form.$.bar.onChange(v)}>
5555
</form>
5656
```
5757

5858
### Comparison with [formstate](https://github.com/formstate/formstate)
5959

6060
formstate-x provides similar APIs with [formstate](https://github.com/formstate/formstate) because we like their simplicity. formstate has a very helpful document which we will recommend you to read. But there are some points of formstate that brought inconvenience to our development, and we got disagreement with formstate in related problems. That's why we build formstate-x:
6161

62-
1. formstate uses MobX but not embracing it, which constrained its ability (related issue: [#11](https://github.com/formstate/formstate/issues/11)). formstate-x leverages MobX's power substantially, so we can easily track all dependencies when do validation, including dynamic values or dynamic valdiators. That's also why realizing cross-field validation is extremly easy with formstat-x.
62+
1. formstate uses MobX but not embracing it, which constrained its ability (related issue: [#11](https://github.com/formstate/formstate/issues/11)). formstate-x leverages MobX's power substantially, so we can easily track all dependencies when do validation, including dynamic values or dynamic valdiators. That's also why realizing cross-state validation is extremly easy with formstat-x.
6363
2. formstate mixes validated, safe value with readable value (`$`), in some cases it's not suitable to use either `$` or `value`. formstate-x provides `value` as readable value, `$` as validated and safe value and `_value` for UI binding only.
6464
3. formstate doesn't provide a good way to extract value from `FormState` ( related issues: [#25](https://github.com/formstate/formstate/issues/25) [#28](https://github.com/formstate/formstate/issues/28)). formstate-x provides `value` as `FormState`'s serialized value, with full type info.
6565
4. formstate dosen't provide a good way to disable part of a `FormState`. `FormStateLazy` is like a hack and very different concept from `FieldState` & `FormState`. formstate-x provides `disableWhen` to let you realize conditional validation with a single method call.

0 commit comments

Comments
 (0)