Created
June 25, 2019 11:21
-
-
Save chenyong/9d96e459acd9bd8a185d396d8ee4bbb1 to your computer and use it in GitHub Desktop.
Immer Form solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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