Skip to content

Instantly share code, notes, and snippets.

@dralletje
Created February 13, 2019 11:45
Show Gist options
  • Select an option

  • Save dralletje/ac65207f43970b38d79e622fccf9753b to your computer and use it in GitHub Desktop.

Select an option

Save dralletje/ac65207f43970b38d79e622fccf9753b to your computer and use it in GitHub Desktop.
import React from 'react';
export class Form extends React.Component {
state = {
has_submitted: false,
submit_error: null,
};
render() {
let { children, onSubmit, conditions } = this.props;
let { submit_error } = this.state;
let errors = conditions.filter((x) => !x.condition);
let disabled = errors.length !== 0;
let handleSubmit = async () => {
try {
if (disabled) {
return;
}
await onSubmit();
this.setState({ has_submitted: true });
} catch (err) {
this.setState({ submit_error: err });
}
};
return children({
// errors: errors.map(x => x.message),
errors: errors.map((x) => x.message),
disabled: disabled,
onSubmit: handleSubmit,
submit_error,
getInputProps: () => {
return {
onKeyDown: (e) => {
if (e.which === 13) {
handleSubmit();
}
},
};
},
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment