Skip to content

Commit 90294cb

Browse files
committed
Remove form.setDefaultValues, merged with setValues
1 parent 5e961ce commit 90294cb

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
public validateOnMount: 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
public readonly values: T;
6868

6969
/**
70-
* The default values on this form. Use setDefaultValues(...) to set these.
70+
* The default values on this form. Use `setValues(?,?,true)` to set these.
7171
*/
7272
public readonly defaultValues: 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 isDefault Are these values the default values for this form? This function only updates values or defaultValues, not both! To set both, use `form.setDefaultValues()`.
239+
* @param isDefault Leave undefined to set both `values` and `defaultValues`. Set to true to only set `defaultValues` and false to only set `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
public setValues(
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
let keys = Object.keys(isDefault ? this.defaultValues : this.values);
251256
let v: typeof values = 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-
public setDefaultValues(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
import React from "react";
2-
import { useForm, Field } from "typed-react-form";
2+
import { useForm, Field, AnyListener } from "typed-react-form";
33

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

14-
export default function ExampleForm() {
14+
export function FieldForm() {
1515
const form = useForm({ firstName: "", lastName: "" });
1616

1717
function submit() {
@@ -23,6 +23,15 @@ export default function ExampleForm() {
2323
<Field form={form} name="firstName" as={CustomInput} myCustomProp={true} />
2424
<Field form={form} name="lastName" as={CustomInput} />
2525
<button type="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+
<AnyListener form={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
await new Promise((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
<a href="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
await new Promise((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
<a href="https://github.com/CodeStix/typed-react-form/blob/master/example/src/OneOfObjectForm.tsx">View source code</a>

0 commit comments

Comments
 (0)