How to disable a Field in redux-form?

Edv Beq picture Edv Beq · Jun 16, 2018 · Viewed 11.6k times · Source

I am simply trying to disable a field in redux-form as shown below but it does not seem to have any effect. This is redux-form version 7.4.2.

  <Field
    name="mu"
    type="text"
    component={renderField}
    label="DRIFT FUNCTION [ μ(X(t),t) ]:"
    disabled={true} 
    validate={[required]}
  />

Also

  <Field
    name="mu"
    type="text"
    component={renderField}
    label="DRIFT FUNCTION [ μ(X(t),t) ]:"
    props={{ disabled: true }}
    validate={[required]}
  />

Any help please

Answer

Idan Dagan picture Idan Dagan · Aug 21, 2018

You can pass the props object:

props : object [optional]: Object with custom props to pass through the Field component into a component provided to component prop. This props will be merged to props provided by Field itself.

// outside your render() method
const renderField = field => (
  <div>
    <input
      {...field.input}
      disabled={field.disabled} // you'll use it here
      type="text"
    />
  </div>
);

// inside your render() method
<Field
  name="myField"
  props={{
    disabled: true, // like this
  }},
  component={renderField}
/>