BooleanCheckbox.tsx 2.72 KB
Newer Older
Oleksii Savran's avatar
Oleksii Savran committed
1
import * as React from 'react';
2
import {WithTranslation, withTranslation} from 'react-i18next';
Oleksii Savran's avatar
Oleksii Savran committed
3 4 5 6 7

import FormControlLabel from '@material-ui/core/FormControlLabel';
import FormLabel from '@material-ui/core/FormLabel';
import FormControl from '@material-ui/core/FormControl';
import RadioGroup from '@material-ui/core/RadioGroup';
8
import { withStyles, WithStyles } from '@material-ui/core/styles';
Oleksii Savran's avatar
Oleksii Savran committed
9 10 11 12 13 14 15 16 17 18 19
import Checkbox from '@material-ui/core/Checkbox';
import { cleanFilters } from 'utilities';

/* tslint:disable */
const styles = (theme) =>({
  label: {
    // just mock for overriding
  },
});
/* tslint:enable */

20
interface IBooleanArrCheckbox extends React.ClassAttributes<any>, WithTranslation, WithStyles {
Oleksii Savran's avatar
Oleksii Savran committed
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  formLabel: string;
  options: any[];
  renderOptionLabel: any;
  input: any;
  customHandleChange: any;
  isNotNull: boolean;
  isNull: boolean;
  notNull: boolean;
}

class BooleanArrCheckbox extends React.Component<IBooleanArrCheckbox> {

  public componentWillReceiveProps(nextProps) {
    const { input, notNull } = this.props;
    const { input: nextInput } = nextProps;

    // notNull case, set initial value for filter in case when filter === null
    if (notNull && input && input.value === '' && nextInput && nextInput.value === '') {
      input.onChange(cleanFilters.DEFINED_NULL);
    }
  }

  private onChangeHandler = (option) => () => {
    const { customHandleChange, input } = this.props;
    return customHandleChange(input, option);
  }

  private isChecked = (option) => {
    const { input, isNull, isNotNull, notNull } = this.props;

    if (notNull) {
      // notNull case

      return input.value === option || typeof input.value === 'string';
    } else {
      // notNull === false case

      if (isNotNull) {
        return typeof option === 'boolean';
      }

      if (isNull) {
        return option === 'NULL';
      }

66
      return input.value === option || typeof input.value === 'string';
Oleksii Savran's avatar
Oleksii Savran committed
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 93 94 95
    }
  }

  public render() {
    const { classes, formLabel, options, renderOptionLabel } = this.props;
    return (
      <FormControl className={ classes.root } fullWidth>
        <FormLabel>{ formLabel }</FormLabel>
        <RadioGroup>
          { options.map((option, i) => (
            <FormControlLabel
              key={ `radio_option_${i}` }
              value={ option }
              classes={ {label: classes.label} }
              label={ renderOptionLabel(option) }
              control={
                <Checkbox
                  checked={ this.isChecked(option) }
                  onChange={ this.onChangeHandler(option) }
                />
              }
            />
          )) }
        </RadioGroup>
      </FormControl>
    );
  }
}

96
export default withStyles(styles)(withTranslation()(BooleanArrCheckbox));