0

I am using react-final-form library .so in which I have one checkbox , on click of checkbox it shows textfield . User can enter text in text field .but if you uncheck the checkbox text retain in textfield .

Steps to reproduce this bug

Checked the check box

Enter “abc” text in input field

Unchecked the checkbox

Again checked the checkbox.Textfied retain abc text.

<FieldPrefix prefix="app1">
  <div>
    <label>
      app1
      <PrefixedField name="appStatus" component="input" type="checkbox" />
    </label>
  </div>
  {values.app1 && values.app1.appStatus ? (
    <div>
      <label>City</label>
      <PrefixedField
        name="city"
        component="input"
        type="text"
        placeholder="City"
      />
    </div>
  ) : null}
</FieldPrefix>;

https://codesandbox.io/s/optimistic-field-56zpm enter image description here

https://github.com/final-form/react-final-form

1
  • Firstly pick value for textbox from a state variable and hook onChange event for textbox and change state accordingly. When you reset it just update the state variable to an empty string and you will see it clean. Commented Aug 2, 2019 at 5:31

2 Answers 2

2

use value props in the input field where put a state like

<div>
    <label>City</label>
    <PrefixedField
    name="city"
    component="input"
    type="text"
    placeholder="City"
    value = {this.state.valueForInput}
    />
</div>

and clear the state valueForInput when user uncheck the check box

Sign up to request clarification or add additional context in comments.

6 Comments

please use react final form api
well why ? can you elaborate it !
yeah but they do have a value props where you use it
I think there is no need to add create state unnecessary ..react final form do for us
actually they will also do the same process using a state , as react render will never update unless you update a state
|
0

Add this (values.app1 && values.app1.city?values.app1.city="":null) instead of the null in the ternary expression.

{values.app1 && values.app1.appStatus ? (
                <div>
                  <label>City</label>
                  <PrefixedField
                    name="city"
                    component="input"
                    type="text"
                    placeholder="City"
                  />
                </div>
              ) : (values.app1 && values.app1.city?values.app1.city="":null)}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.