0

I want to set value for DisabledInput when ReferenceInput onChange event. I

export const RegistersCreate=(props) => (
  <Create {...props}>
    <SimpleForm>
      <ReferenceInput source='member_id' reference='members' allowEmpty validate={required}>
        <SelectInput source='name'/>
      </ReferenceInput>
      <ReferenceInput source='package_id' reference='packages' allowEmpty validate={required}>
        <SelectInput source='name'/>
      </ReferenceInput>
      <ReferenceInput source='promotion_id' reference='promotions' allowEmpty>
        <SelectInput source='name'/>
      </ReferenceInput>
      <DateInput source='date_from' validate={required}/>
      <DateInput source='date_to' validate={required}/>
      <DisabledInput source='amount'/>
    </SimpleForm>
  </Create>
); // RegistersCreate
qui ha
  • 39
  • 1
  • 5

2 Answers2

2

but above advice isn't perfect, and i tried to make it but with TextInputs (no ReferenceInput as you) and wrote simple class:

import React from 'react';
import { Field } from 'redux-form';
import { 
  TextInput,
  required,
} from 'admin-on-rest';

export class ValueField extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      val1: parseInt(this.props.record.money_amount, 10),
      val2: parseInt(this.props.record.credit_limit, 10),
    };
  }

  render(){
    return (
      <div>
        <Field name="money_amount" validate={required} component={TextInput} onChange={(e) => this.setState({ val1: parseInt(e.target.value, 10) }) }/>
        <Field name="credit_limit" component={TextInput} onChange={(e) => this.setState({ val2: parseInt(e.target.value, 10) }) } />
        <span>{this.state.val1 + this.state.val2}</span>
      </div>
    );
  };
}

export default ValueField;

and importing to resources.js :

import ValueField from './ValueField';

... and adding to edit view as:

<ValueField />

in my case there is money_amount and credit_limit fields. i needed to calculate sum of these fields.

Alexey
  • 601
  • 7
  • 17
  • Thanks! how do you make it so it has a label in a list view though? – Michail Michailidis Sep 01 '17 at 20:32
  • Maybe 'label' prop? You can open new question with full description and samples from your code and some people will try to help you. – Alexey Sep 03 '17 at 02:19
  • thanks! what i was trying to do is have a computed field.. but since this information is not coming from the database it won't be able to sort by it and have pager too - that's why I didnt post a new question.. – Michail Michailidis Sep 03 '17 at 09:43
0

aor command created specified component for fields which depends on specified fields. hope it helps you. please check link: https://github.com/marmelab/aor-dependent-input

Alexey
  • 601
  • 7
  • 17