BooleanDimensionFormExtra.tsx 2.36 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
import * as React from 'react';
import {translate} from 'react-i18next';
import { Field } from 'redux-form';

import Radio from '@material-ui/core/Radio';
import RadioGroup from '@material-ui/core/RadioGroup';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import FormLabel from '@material-ui/core/FormLabel';
import FormControl from '@material-ui/core/FormControl';

interface IBooleanFilterInternal extends React.ClassAttributes<any> {
  input: any;
  label: string;
  t: any;
}

class BooleanDimensionFormExtraInternal extends React.Component<IBooleanFilterInternal, any> {

  private constructor(props, context) {
    super(props, context);

    this.state = {
      value: props.input.value,
    };
  }

  private handleChange = (event, value) => {
    const { input } = this.props;
    this.setState({ value });
    input.onChange(value === '3' ? 3 : value === '1' ? 1 : 2);
  }

  public componentWillReceiveProps(nextProps) {
    this.setState({value: nextProps.input.value});
  }

  public render() {
    const { label, t } = this.props;
    return (
      <div>
        <FormControl fullWidth>
          <FormLabel>{ label }</FormLabel>
          <RadioGroup
            value={ `${this.state.value}` }
            onChange={ this.handleChange }
            style={ {flexDirection: 'row', justifyContent: 'space-around'} }
          >
Oleksii Savran's avatar
Oleksii Savran committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
            <FormControlLabel
              value="1"
              control={ <Radio /> }
              label={ t('common:label.true') }
            />
            <FormControlLabel
              value="2"
              control={ <Radio /> }
              label={ t('common:label.false') }
            />
            <FormControlLabel
              value="3"
              control={ <Radio /> }
              label={ t('common:label.either') }
            />
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
          </RadioGroup>
        </FormControl>
      </div>
    );
  }
}

interface IBooleanDimensionFormExtra extends React.ClassAttributes<any> {
  label?: string;
  t: any;
}

class BooleanDimensionFormExtra extends React.Component<IBooleanDimensionFormExtra, any> {

  public render() {
    const { label, t } = this.props;
    return (
      <div>
        <Field
          name={ 'mode' }
          component={ BooleanDimensionFormExtraInternal }
          label={ t(label) }
          t={ t }
        />
      </div>
    );
  }
}

export default translate()(BooleanDimensionFormExtra);