Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable a Field in redux-form?

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

like image 752
Edv Beq Avatar asked Jun 16 '18 19:06

Edv Beq


4 Answers

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}
/>
like image 79
Idan Dagan Avatar answered Sep 19 '22 08:09

Idan Dagan


Apparently, it works if you provide primitive react components rather than functions:

      <Field
        name="firstName"
        component="input"
        type="text"
        disabled={true}
        placeholder="First Name"
      />

Edit Redux Form - Simple Example

So I think the problem in your case is inside the renderField function you have not shown.

like image 32
Vanuan Avatar answered Sep 18 '22 08:09

Vanuan


If you are working with redux-Form given code can work as @Vanun explained

<Field
    name="Name"
    component="fieldset"
    type="text"
    disabled={true}
  />
like image 44
DevCo Avatar answered Sep 21 '22 08:09

DevCo


input={{ disabled: true, }}

add this to your Field tag

like image 38
nishant Avatar answered Sep 19 '22 08:09

nishant