Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit90294cb

Browse files
committed
Remove form.setDefaultValues, merged with setValues
1 parent5e961ce commit90294cb

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

‎Todo.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
- Combine array helpers into one object? This is usefull to pass to other components
44
- Require index for array fields
55
- Add React.forwardRef to input elements
6-
- Rename`setDefaultValues` to`setAllValues`
76
- Let`comparePrimitiveObject` compare deep objects too
87
- Field on blur

‎src/form.ts‎

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ export class FormState<T extends object, State = DefaultState, Error extends str
6262
publicvalidateOnMount:boolean;
6363

6464
/**
65-
* The values on this form. Use setValues() to set these.
65+
* The values on this form. Use`setValues()` to set these.
6666
*/
6767
publicreadonlyvalues:T;
6868

6969
/**
70-
* The default values on this form. UsesetDefaultValues(...) to set these.
70+
* The default values on this form. Use`setValues(?,?,true)` to set these.
7171
*/
7272
publicreadonlydefaultValues:T;
7373

@@ -236,17 +236,22 @@ export class FormState<T extends object, State = DefaultState, Error extends str
236236
* Set multiple values OR default values on this form.
237237
*@param values The new values to set on this form.
238238
*@param validate Validate? Overrides `validateOnChange`.
239-
*@param isDefaultAre these values the defaultvalues for this form? This functiononlyupdates values ordefaultValues, not both! Tosetboth, use `form.setDefaultValues()`.
239+
*@param isDefaultLeave undefined to set both `values` and `defaultValues`. Set to true toonlyset `defaultValues` and false to onlyset`values`.
240240
*@param notifyChild Should this form notify the child form about this change?
241241
*@param notifyParent Should this form notify the parent form about this change?
242242
*/
243243
publicsetValues(
244244
values:Partial<T>|undefined,
245245
validate?:boolean,
246-
isDefault:boolean=false,
246+
isDefault?:boolean,
247247
notifyChild:boolean=true,
248248
notifyParent:boolean=true
249249
){
250+
if(isDefault===undefined){
251+
this.setValues(values,false,true,notifyChild,notifyParent);
252+
isDefault=false;
253+
}
254+
250255
letkeys=Object.keys(isDefault ?this.defaultValues :this.values);
251256
letv:typeofvalues=values??{};
252257
addDistinct(keys,Object.keys(v));
@@ -287,18 +292,6 @@ export class FormState<T extends object, State = DefaultState, Error extends str
287292
if(validate??(this.validateOnChange&&this.validator))this.validate();
288293
}
289294

290-
/**
291-
* Set both values and default values for this form. If you only want to set values, use setValues(...). If you only want to set default values, use `setValues(...,...,true)`.
292-
*@param values The new default values to set on this form.
293-
*@param validate Validate? Overrides `validateOnChange`.
294-
*@param notifyChild Should this form notify the child form about this change?
295-
*@param notifyParent Should this form notify the parent form about this change?
296-
*/
297-
publicsetDefaultValues(values:Partial<T>,validate:boolean=true,notifyChild:boolean=true,notifyParent:boolean=true){
298-
this.setValues(values,false,true,notifyChild,notifyParent);
299-
this.setValues(values,validate,false,notifyChild,notifyParent);
300-
}
301-
302295
/**
303296
* Force validation on this form. Required when `validateOnChange` is disabled. **This function works with both asynchronous and synchronous validators.**
304297
*@returns true if the form is valid.

‎testing/src/Fieldform.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
importReactfrom"react";
2-
import{useForm,Field}from"typed-react-form";
2+
import{useForm,Field,AnyListener}from"typed-react-form";
33

44
constinputStyle:React.CSSProperties={
55
color:"gray",
@@ -11,7 +11,7 @@ function CustomInput(props: { value: string; onChange: (val: string) => void; my
1111
return<inputstyle={inputStyle}value={props.value}onChange={(ev)=>props.onChange(ev.target.value)}/>;
1212
}
1313

14-
exportdefaultfunctionExampleForm(){
14+
exportfunctionFieldForm(){
1515
constform=useForm({firstName:"",lastName:""});
1616

1717
functionsubmit(){
@@ -23,6 +23,15 @@ export default function ExampleForm() {
2323
<Fieldform={form}name="firstName"as={CustomInput}myCustomProp={true}/>
2424
<Fieldform={form}name="lastName"as={CustomInput}/>
2525
<buttontype="submit">Go</button>
26+
<button
27+
type="button"
28+
onClick={()=>{
29+
form.setValues({firstName:"test",lastName:"test2"},false,true);
30+
}}
31+
>
32+
Set values
33+
</button>
34+
<AnyListenerform={form}render={({ dirty})=><pre>{String(dirty)}</pre>}/>
2635
</form>
2736
);
2837
}

‎testing/src/OneOfObjectArrayForm.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default function OneOfObjectArrayForm() {
3636
awaitnewPromise((res)=>setTimeout(res,500));
3737
form.setState({isSubmitting:false});
3838
console.log(form.values);
39-
form.setDefaultValues(form.values);
39+
form.setValues(form.values);
4040
}}
4141
>
4242
<ahref="https://github.com/CodeStix/typed-react-form/blob/master/example/src/OneOfObjectArrayForm.tsx">View source code</a>

‎testing/src/OneOfObjectForm.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default function OneOfObjectArrayForm() {
3131
awaitnewPromise((res)=>setTimeout(res,500));
3232
form.setState({isSubmitting:false});
3333
console.log(form.values);
34-
form.setDefaultValues(form.values);
34+
form.setValues(form.values);
3535
}}
3636
>
3737
<ahref="https://github.com/CodeStix/typed-react-form/blob/master/example/src/OneOfObjectForm.tsx">View source code</a>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp