usefield
Although you only need to specify data-field-name
the attribute can achieve two -way binding. But in actual development, we usually need more control and functions. At this time, we need to use it useField
create a two -way field binding object.
useField
the following functions can be implemented:
- Support multiple state values to be bound to one
input
superior. - Support to split one state value to multiple
input
superior. - Support
input
by -directional conversion with status values.
about
useField
it is used to create a form control binding object to establish status data and input
two -way binding between controls.
useField
The signature is as follows:
export interface UseFieldType<State extends Dict> {
(): UseFieldBindings<ComputedState<State>>
<Value>(
selector: ObjectKeyPaths<ComputedState<State>>,
options?:UseFieldOptions<Value>
): UseFieldBindings<Value>
<Value>(
getter: UseFieldGetter<Value,State>,
setter:UseFieldSetter<Value,State>,
options?:UseFieldOptions<Value>
):UseFieldBindings<Value>
<Value>(
getters: (ObjectKeyPaths<ComputedState<State>> | string[] | UseFieldGetter<Value,State>)[],
setter:UseFieldSetter<Value,State>,
options?:UseFieldOptions<Value>
):UseFieldBindings<Value>[]
}
useField
support the following call signature:
- Usefield (Selector, Options?)
pass selector
specify a state path and create a two -way field binding object.
- Usefield (Getter, Setter, Options?)
pass getter
and setter
methods to create a dynamic field binding object.
When you want to achieve multiple state values merged to one input
use.
- Usefield (Getters, Setter, Options?)
When it is realized to bind multiple state values to multiple input
use or perform a single state data when splitting.
Basic usage
Can directly pass Usefield (<State path>)
come to create a two -way field binding object, and then deconil input
the control.
State path
it can be arbitrarily depth string or string array, such asuseField("order.user.name")
.input[type='radio']
the field needs to be specified{Type: 'Radio', Values: ['Male', 'Female']})
options, and then in each oneradio
specifyvalue
binded.
Combined state binding
You can realize the combination of multiple state values and bind it to one input
- In the previous example, we pass
getter
andsetter
methods, willfirstName & lastName
binded after merging1
indivualinput
. getter
methods used to merge the status value into a value.setter
the method is responsible for analysisinput
value and update it into the state.
Disassembling state binding
It also supports binding multiple status values to multiple input
superior.
useField
support the following call signature:
useField<Value>(
getters: (string | string[] | UseFieldGetter<Value,State>)[],
setter:UseFieldSetter<Value,State>,
options?:UseFieldOptions
):UseFieldBindings<Value>[]
getters
: Enter onegetter
array, eachgetter
mayString
orString array
the state path can also be onegetter
function.setter
: onesetter
function for analysisinput
the value is updated to the state.
The following is a simple example:
Conversion state value
Be specified options.toState
parameter, you can input
write the state after the value conversion.
The characters entered in the following example will be converted to uppercase and then updated to the state
Configuration field
useField
support the following configuration options:
type UseFieldOptions<Value=any>={
name? : string // 可选的字段名称
type? : 'radio' | 'checkbox' | 'select' | 'textarea' | 'input'
// 仅当type = radio或checkbox时有效时有效
values? : any[]
toState? : (value:string,options?:{path:string[] | undefined,part:number})=>Value // 将数据更新到状态中时调用进行转换
}
name
: Optional field names for identifying fields.type
: Control type, supportradio
,checkbox
,select
,textarea
,input
.values
:type = radio
orcheckbox
effective time is effective, used to specifyradio
orcheckbox
value.toState
: For theinput
the value is converted to a function of the state value.