Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
fix(Formbot): Set field value before validating in onChange handler (#16
Browse files Browse the repository at this point in the history
)

The previous logic would invalidate the field, losing the scope of the synthetic onChange event handler in the valdiation promise. After validation, Formbot would then set the new values, causing Input cursor to jump to the end.
  • Loading branch information
kylealwyn authored Jan 28, 2019
1 parent 23ea486 commit 60a64d1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/Form/Formbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,20 @@ export default class Formbot extends React.Component {
};

onChange = (field, value) => {
this.updateField(field, { validated: false }).then(() => {
this.setState(
{
values: {
...this.state.values,
[field]: value,
},
this.setState(
{
values: {
...this.state.values,
[field]: value,
},
() => {
},
() => {
this.props.onChange(field, value, this.state.values);
this.updateField(field, { validated: false }).then(() => {
this.validateField(field);
this.props.onChange(field, value, this.state.values);
}
);
});
});
}
);
};

onBlur = field => {
Expand Down
30 changes: 29 additions & 1 deletion src/Form/Input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ name: Input
import { Playground, PropsTable } from 'docz'
import Input from './Input'
import Button from '../Button'
import Formbot from './Formbot';

# Input

Expand All @@ -16,10 +17,37 @@ Form input component. Different sizes are available.
<PropsTable of={Input} />

## Examples
<Playground>
{() => {
class Controlled extends React.Component {
render() {
return (
<Formbot
onSubmit={console.log}
initialValues={{ value: '' }}
validations={{ value: (value) => {
if (value !== 'secretpassword') {
throw new Error('Try typing "secretpassword"')
}
}}}>
{({ values, errors, onChange, onSubmit }) => (
<form onSubmit={onSubmit}>
<Input label="Input with Formbot" name="value" value={values.value} error={errors.value} onChange={onChange}/>
<Button mt={2} type="submit">Submit</Button>
</form>
)}
</Formbot>
);
}
}

return <Controlled />
}}
</Playground>

### Default Input
<Playground>
<Input placeholder="Example" label="Example" autofocus />
<Input placeholder="Example" label="Example" />
</Playground>

### Multiline Input
Expand Down

0 comments on commit 60a64d1

Please sign in to comment.