Skip to content

Commit

Permalink
Change form.setValue to form.updateInputsWithValue (#375)
Browse files Browse the repository at this point in the history
* Change form.setValue to form.updateInputsWithValue

* Fix built
  • Loading branch information
rkuykendall committed Feb 3, 2020
1 parent a98c070 commit f382b52
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
20 changes: 20 additions & 0 deletions API.md
Expand Up @@ -12,6 +12,7 @@
- [preventExternalInvalidation](#preventExternalInvalidation)
- [reset()](#reset)
- [updateInputsWithError()](#updateInputsWithError)
- [updateInputsWithValue()](#setInputsWithValue)
- [validationErrors](#validationErrors)
- [withFormsy](#withFormsy)
- [errorMessage](#errorMessage)
Expand Down Expand Up @@ -211,6 +212,25 @@ argument and optionally invalidate the form by passing `true` as the second argu
validation. This is also passed as the third parameter to the [`onSubmit`](#onSubmit), [`onValidSubmit`](#onValid) or
[`onInvalidSubmit`](#onInvalid).

#### <a id="updateInputsWithValue">updateInputsWithValue(values)</a>

```jsx
class MyForm extends React.Component {
someFunction = () => {
this.refs.form.updateInputsWithValue({
email: 'value@example.com',
'field[10]': 'value!',
});
};
render() {
return <Formsy ref="form">...</Formsy>;
}
}
```

Manually set the form fields values by taking an object that maps field name to value as the first argument and
optionally validate the inputs by passing `true` as the second argument.

#### <a id="preventExternalInvalidation">preventExternalInvalidation</a>

```jsx
Expand Down
4 changes: 2 additions & 2 deletions __tests__/Formsy-spec.tsx
Expand Up @@ -543,7 +543,7 @@ describe('value === false', () => {
expect(input.instance().getValue()).toEqual(true);
});

it('should be able to set a value to one component with setValue', () => {
it('should be able to set a value to components with updateInputsWithValue', () => {
class TestForm extends React.Component {
state = {
valueFoo: true,
Expand Down Expand Up @@ -576,7 +576,7 @@ describe('value === false', () => {
.instance()
.getValue(),
).toEqual(true);
formsyForm.instance().setValue('foo', false);
formsyForm.instance().updateInputsWithValue({ foo: false });
expect(
inputs
.at(0)
Expand Down
25 changes: 14 additions & 11 deletions src/index.ts
Expand Up @@ -11,7 +11,7 @@ import {
IModel,
InputComponent,
IResetModel,
ISetInputValue,
IUpdateInputsWithValue,
IUpdateInputsWithError,
ValidationFunction,
} from './interfaces';
Expand Down Expand Up @@ -297,14 +297,6 @@ class Formsy extends React.Component<FormsyProps, FormsyState> {
this.validateForm();
};

// Set the value of one component
public setValue: ISetInputValue<any> = (name, value, validate) => {
const input = this.inputs.find(component => component.props.name === name);
if (input) {
input.setValue(value, validate);
}
};

// Checks validation on current value or a passed value
public runValidation = <V>(component: InputComponent<V>, value = component.state.value) => {
const { validationErrors } = this.props;
Expand Down Expand Up @@ -406,7 +398,7 @@ class Formsy extends React.Component<FormsyProps, FormsyState> {
const { preventExternalInvalidation } = this.props;
const { isValid } = this.state;

Object.keys(errors).forEach(name => {
Object.entries(errors).forEach(([name, error]) => {
const component = this.inputs.find(input => input.props.name === name);
if (!component) {
throw new Error(
Expand All @@ -418,7 +410,7 @@ class Formsy extends React.Component<FormsyProps, FormsyState> {
const args = [
{
isValid: preventExternalInvalidation,
externalError: typeof errors[name] === 'string' ? [errors[name]] : errors[name],
externalError: utils.isString(error) ? [error] : error,
},
];
component.setState(...args);
Expand All @@ -428,6 +420,17 @@ class Formsy extends React.Component<FormsyProps, FormsyState> {
}
};

// Set the value of components
public updateInputsWithValue: IUpdateInputsWithValue<any> = (values, validate) => {
Object.entries(values).forEach(([name, value]) => {
const input = this.inputs.find(component => component.props.name === name);

if (input) {
input.setValue(value, validate);
}
});
};

// Use the binded values and the actual input value to
// validate the input and set its state. Then check the
// state of the form itself
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.ts
Expand Up @@ -8,7 +8,7 @@ export interface Values {
export type IModel = any;
export type IData = any;
export type IResetModel = (model?: IModel) => void;
export type ISetInputValue<V> = (name: string, value: V, validate?: boolean) => void;
export type IUpdateInputsWithValue<V> = (values: any, validate?: boolean) => void;
export type IUpdateInputsWithError = (errors: any, invalidate?: boolean) => void;

export type ValidationFunction<V> = (values: Values, value: V, extra?: any) => boolean | string;
Expand Down

0 comments on commit f382b52

Please sign in to comment.