Skip to content

Instantly share code, notes, and snippets.

@chenyong
Created June 25, 2019 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenyong/9d96e459acd9bd8a185d396d8ee4bbb1 to your computer and use it in GitHub Desktop.
Save chenyong/9d96e459acd9bd8a185d396d8ee4bbb1 to your computer and use it in GitHub Desktop.
Immer Form solution
<LabeledField label={lingual.event} showRequired={false}>
<EventDropdown
placeholder={lingual.pleaseSelect}
notFoundContent={lingual.pleaseSelect}
onDeselect={() => {
this.changeField("event", null);
}}
value={form.event}
onChange={(value) => {
this.changeField("event", value);
}}
/>
</LabeledField>
<LabeledFieldInput
label={lingual.description}
value={form.description}
onChange={(value) => {
this.changeField("description", value);
}}
styleInput={{ width: "auto", minWidth: 200 }}
multiline
/>
<div className={rowParted}>
<span />
<div className={row}>
<Button onClick={this.props.onCancelModal}>{lingual.cancel}</Button>
<Space width={8} />
<Button type={"primary"} onClick={this.onConfirm}>
{lingual.confirm}
</Button>
</div>
</div>
changeField<K extends keyof IProcess>(k: K, v: IProcess[K]) {
this.immerState((state) => {
state.form[k] = v;
if (validationRules[k] != null) {
state.failures[k] = validationRules[k](state.form as IProcess);
}
});
}
onConfirm = () => {
let form = this.state.form as IProcess;
let failures = getValidationFailures<IProcess>(validationRules, form);
this.mergeState({ failures });
if (isFailuresEmpty(failures)) {
this.props.onSubmitForm(form);
}
};
interface IState {
form: Partial<IProcess>;
failures: IShapedFailures<IProcess>;
}
const validationRules: IShapedRules<IProcess> = {
name: (form: IProcess) => formValueRequired(form.name),
kind: (form: IProcess) => formValueRequired(form.kind),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment