Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update one Field based on another

As a part of my form I have a field for selecting items and a button which the client clicks in order to add the items to their list of units. I wish to show the list of units selected by the user in the units text field. How do I implement the onclick function for the button so that it takes the currently selected value for the select component before appending it to the list as presented by the textbox? I have a separate save button which will take the form data and send it to the backend when clicked.

let EditUserForm = (props) => {
    const { handleSubmit, units } = props;
    return (
        <form onSubmit={handleSubmit}>

            <Field name="units" component="input" type="text" readOnly/> 

            <Field name="unit" component="select" >
            {
                units.map(u => <option value={u} key={u}>{u}</option>)
            }
            </Field>

            <button type="button" onClick={props.updateUserUnits}>Add unit</button>

        </form>
    );
};
like image 390
Baz Avatar asked Apr 04 '17 11:04

Baz


People also ask

How do you UPDATE values based on conditions in SQL?

Update with condition WHERE clause can be used with SQL UPDATE to add conditions while modifying records. Without using any WHERE clause, the SQL UPDATE command can change all the records for the specific columns of the table.

How do you UPDATE a column based on a filter of another column?

UPDATE table SET col = new_value WHERE col = old_value AND other_col = some_other_value; UPDATE table SET col = new_value WHERE col = old_value OR other_col = some_other_value; As you can see, you can expand the WHERE clause as much as you'd like in order to filter down the rows for updating to what you need.

How do you UPDATE the same column with different values in SQL?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.


1 Answers

You could do something like in the code below. This combines several redux-form concepts.

Basically you want to intercept the onChange event from the select component, so you can attach some logic to it.

In this case we'll use the change prop that is passed down by redux-form. This will allow you to change the value of another field in the form.

Combine this with the formValueSelector which allows you to get a value from a specific form field.

import React from 'react'
import { connect } from 'react-redux';
import { Field, FieldArray, formValueSelector, reduxForm } from 'redux-form'

const EditUser = (props) => {
    const { change, handleSubmit, selectedUnits = [], units } = props;

    const handleUnitChange = (event, value) => {
      const newUnits = [ ...selectedUnits, value ];

      change('units', newUnits);
    }

    return (
        <form onSubmit={handleSubmit}>
            <Field name="units" component="input" type="text" readOnly/> 

            <Field name="unit" component="select" onChange={handleUnitChange}>
              {units.map(u => <option value={u} key={u}>{u}</option>)}
            </Field>

            <button type="button" onClick={props.sendData}>Add unit</button>
        </form>
    );
};

const form = 'editUserForm';

const EditUserForm = reduxForm({
  form
})(EditUser);

const selector = formValueSelector(form);

export default connect(state => ({
  selectedUnits: selector(state, 'units')
}))(EditUserForm);
like image 149
Dennis Avatar answered Oct 21 '22 02:10

Dennis