|
1 | 1 | --- |
2 | 2 | title: State |
3 | | -order: 1 |
| 3 | +order: 2 |
4 | 4 | toc: menu |
5 | 5 | --- |
6 | 6 |
|
7 | | -TODO |
| 7 | +### State |
| 8 | + |
| 9 | +A formstate-x state is a object which holds state (value, error, etc.) for a input. |
| 10 | + |
| 11 | +In formstate-x, there are more than one state types (such as `FieldState`, `FormState`, `TransformedState`, ...), while all of them implemented the same interface `IState`. Here is the interface definition: |
| 12 | + |
| 13 | +_For convenience of reading, some unimportant fields are omitted._ |
| 14 | + |
| 15 | +```ts |
| 16 | +/** interface for State */ |
| 17 | +export interface IState<V> { |
| 18 | + /** Value in the state. */ |
| 19 | + value: V |
| 20 | + /** Set `value` on change event. */ |
| 21 | + onChange(value: V): void |
| 22 | + /** Set `value` imperatively. */ |
| 23 | + set(value: V): void |
| 24 | + /** If activated (with auto-validation). */ |
| 25 | + activated: boolean |
| 26 | + /** Current validate status. */ |
| 27 | + validateStatus: ValidateStatus |
| 28 | + /** The error info of validation. */ |
| 29 | + error: ValidationError |
| 30 | + /** The state's own error info, regardless of child states. */ |
| 31 | + ownError: ValidationError |
| 32 | + /** Append validator(s). */ |
| 33 | + withValidator(...validators: Array<Validator<V>>): this |
| 34 | + /** Fire a validation behavior imperatively. */ |
| 35 | + validate(): Promise<ValidateResult<V>> |
| 36 | + /** Configure when state should be disabled. */ |
| 37 | + disableWhen(predictFn: () => boolean): this |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### Composability |
| 42 | + |
| 43 | +Like inputs, states are composable. |
| 44 | + |
| 45 | +A state may be composed of multiple child states. Its value is the composition of these child states' values and its validation result reflects all its child states' validation results. |
| 46 | + |
| 47 | +A state for a complex input can be composed of its child inputs' states. |
| 48 | + |
| 49 | +A state for a list input can be composed of its item inputs' states. |
| 50 | + |
| 51 | +A form, which includes multiple inputs, can be considered as a complex input with a submit button. We use a formstate-x state to hold state for a form. The state for a form can be composed of its inputs' states. |
| 52 | + |
| 53 | +### Value |
| 54 | + |
| 55 | +A state holds the current value, and provides methods to change it. |
| 56 | + |
| 57 | +A input component is expected to get state from its props. The component read the value for rendering and set the value when user interacts. |
| 58 | + |
| 59 | +The input component's consumer is expected to create the state and hold it. By passing it to the input component, the consumer get the ability to "control" the input component. |
| 60 | + |
| 61 | +The consumer may also read value from the state for other purposes. Typically, when a user clicks the submit button, from its state a form reads the whole value for submitting (maybe sending an HTTP request). |
| 62 | + |
| 63 | +### Validation |
| 64 | + |
| 65 | +Validation is the process of validating user input values. |
| 66 | + |
| 67 | +Validation is important for cases like: |
| 68 | + |
| 69 | +* When user inputs, we display error tips if validation not passed, so users see that and correct the input |
| 70 | +* Before form submiiting, we check if all value is valid, so invalid requests to the server can be avoided |
| 71 | + |
| 72 | +That's why validation should provide such features: |
| 73 | + |
| 74 | +* It should run automatically, when users changed the value, or when some other data change influcend the value validity |
| 75 | +* It should produce details such as a meaningful message, so users can get friendly hint |
| 76 | + |
| 77 | +With formstate-x, we define validators and append them to states with `withValidator`. formstate-x will do validation for us. Through `validateStatus` & `error`, we can access the validate status and result. |
| 78 | + |
| 79 | +For more examples of validation, check section [Validation](/guide/validation). |
| 80 | + |
| 81 | +For more details about validators, check section [Validator](/concepts/validator). |
| 82 | + |
| 83 | +### Activated |
| 84 | + |
| 85 | +It's not user-friendly to notify a user for inputs he hasn't interact with. |
| 86 | + |
| 87 | +Most forms start with all inputs empty—which values are obviously invalid. While it's not appropriate to show error tips at the beginning. |
| 88 | + |
| 89 | +That's why there's a boolean field `activated` for states. |
| 90 | + |
| 91 | +States will not be auto-validated until it becomes **activated**. And they will become (and stay) activated if one of these happens: |
| 92 | + |
| 93 | +1. Value changed by user interactions (method `onChange()` is called). |
| 94 | +2. State imperatively validated (method `validate()` is called). |
| 95 | + |
| 96 | +### Own Error |
| 97 | + |
| 98 | +`ownError` & `hasOwnError` are special fields especially for composed states. You can check details about them in issue [#71](https://github.com/qiniu/formstate-x/issues/71). |
| 99 | + |
| 100 | +### Disable State |
| 101 | + |
| 102 | +You may find that we defined method `disableWhen` to configure when a state should be disabled. It is useful in some specific cases. You can check details in section [Disable State](/guide/advanced#disable-state). |
0 commit comments