teslamotors/informed

Best way to handle null values

andrewdesmondm opened this issue · 3 comments

I am currently having an issue with handling null values.

For instance I have a notes field which I used the informed text input. When I have a user edit their notes, if they delete all of the text, the value is not included in values passed to on submit. This results in me doing the following for all fields with onSubmit: notes: values.notes || "" . If I don't do this, my request does not contain the notes field and so the field is not updated.

However, if I pass in an initialValue of null, a value for it is present.

This is how my forms are currently structured.

<Form
          onSubmit={values => {
            onSubmit({ ...values, notes: values.notes || "" });
          }}
          autoComplete="off"
          validate={this.validate}>
          () => (
            <div>
              <div className={Classes.DIALOG_BODY}>
                <FormGroup label="Notes">
                  <Text
                    field="notes"
                    className={Classes.INPUT}
                    style={formStyles.text}
                  />

If I understand correctly, this is a good use case for allowEmptyString on the input, which will preserve the field's key in the form values if all of the text is deleted. https://joepuzzo.github.io/informed/?path=/story/inputs--intro

Thank you!

Yup allowEmptyString at field level or allowEmptyStrings at form level