Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change default value in input tag

I am using react-form to create an input form. I am using the tag. I want to pre-populate the input field, but I want the user to still be able to change the default value that is entered. React is the front end language that I am using. I tried setting the readOnly field inside the input tag to false. It allows me to click on the field but I still cant backspace the default day and change it to something else.

When I use a placeholder, instead of having the input tag at all, there is an overlap problem (check the image attached). once you enter in a value, there is no problem as the "Due Date" moves up higher with the existing functionality.

I would need the placeholder to only show up when I click on the "Due Date" field to enter a value, or be able to alter the default value when using the input tag. I am using react version 16.2.0enter image description here

              <Field
                name="dueDayOfFilingPeriod"
                component={TextField}
                floatingLabelText="Due Date"
                floatingLabelStyle={(props.hasFilingFormName || props.hasPreference) ? {} : styles.floatingLabelStyle}
                floatingLabelFocusStyle={styles.floatingLabelFocusStyle}
                disabled={props.hasFilingFormName}
                style={styles.inline}
              >
                <input value= {props.filingInfo.dueDate}/>
              </Field>
like image 902
Vangjel Avatar asked Jun 03 '26 22:06

Vangjel


1 Answers

You can set a defaultValue prop instead of a value prop on the <input />. This pattern is usually called an uncontrolled component since you only set the starting value, you can read more here: https://reactjs.org/docs/uncontrolled-components.html

To make it a controlled component, where the value is always being set relative to the state of your component, you need to add an onChange handler to the <input /> component and update the state with the new value. You can read more on controlled components here: https://reactjs.org/docs/forms.html

like image 69
Alexei Darmin Avatar answered Jun 07 '26 22:06

Alexei Darmin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!