StringArrFilter.tsx 6.7 KB
Newer Older
Maxym Borodenko's avatar
Maxym Borodenko committed
1 2
import * as React from 'react';
import { Field } from 'redux-form';
3
import { translate } from 'react-i18next';
Maxym Borodenko's avatar
Maxym Borodenko committed
4

Matija Obreza's avatar
Matija Obreza committed
5 6 7 8
import FormGroup from '@material-ui/core/FormGroup';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import FormLabel from '@material-ui/core/FormLabel';
import FormControl from '@material-ui/core/FormControl';
Maxym Borodenko's avatar
Maxym Borodenko committed
9

Matija Obreza's avatar
Matija Obreza committed
10 11 12 13 14 15
import Checkbox from '@material-ui/core/Checkbox';
import IconButton from '@material-ui/core/IconButton';
import Input from '@material-ui/core/Input';
import InputLabel from '@material-ui/core/InputLabel';
import InputAdornment from '@material-ui/core/InputAdornment';
import PlusOne from '@material-ui/icons/PlusOne';
Maxym Borodenko's avatar
Maxym Borodenko committed
16 17 18 19 20 21 22

interface IStringArrFilterInternal extends React.ClassAttributes<any> {
    name: string;
    input: any;
    placeholder?: string;
    label?: string;
    options?: { [key: string]: any; };
23
    t: any;
Maxym Borodenko's avatar
Maxym Borodenko committed
24 25 26 27 28 29 30 31 32 33 34 35 36
}

class InternalStringArrField extends React.Component<IStringArrFilterInternal & any, any> {

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

        this.state = {
            values: [ ...props.input.value ],
            text: '',
        };
    }

37 38 39 40 41 42 43 44
    public componentWillMount() {
      const value = typeof this.props.input.value[0] === 'number' ? this.props.input.value.map((key) => `${key}`) : this.props.input.value;
      this.setState({
        values: [ ...value ],
        text: '',
      });
    }

Maxym Borodenko's avatar
Maxym Borodenko committed
45
    public componentWillReceiveProps(nextProps) {
46
        const value = typeof nextProps.input.value[0] === 'number' ? nextProps.input.value.map((key) => `${key}`) : nextProps.input.value;
Maxym Borodenko's avatar
Maxym Borodenko committed
47
        this.setState({
48
            values: [ ...value ],
Maxym Borodenko's avatar
Maxym Borodenko committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
            text: '',
        });
    }

    private maybeAdd = (text: string) => {
      const values = [ ...this.state.values ];

      if (text && text.length > 0) {
        if (values.indexOf(text) < 0) {
          values.push(text);
        }
        this.setState({
          text: '',
          values,
        });
      }
      return values;
    }

    private maybeRemove = (text: string) => {
      const values = [ ...this.state.values ];

      if (text && text.length > 0) {
        const index: number = values.indexOf(text);
        if (index >= 0) {
          values.splice(index, 1);
        }
        this.setState({
          text: '',
          values,
        });
      }
      return values;
    }

    private handleKeyPres = (event) => {
        const { input } = this.props;
        const { text } = this.state;

        if (event.key === 'Enter') {
          if (text && text.length > 0) {
            event.preventDefault();
            const values = this.maybeAdd(text);
            input.onChange(values);
          } else {
            // on blank, submit it
            // input.onChange(values);
          }
        }
    }

    private handleCheckbox = (event) => {
        const { input } = this.props;

        const values = event.target.checked ? this.maybeAdd(event.target.value) : this.maybeRemove(event.target.value);
        input.onChange(values);
    }

    private removeByIndex = (index) => (event) => {
        const { input } = this.props;

        const newValues = this.maybeRemove(this.state.values[index]);
        input.onChange(newValues);
    }

    // only for the textbox
    private handleChange = (event) => {
        this.setState({ ...this.state, text: event.target.value });
    }

    private handleAddCurrent = (event) => {
        const { input } = this.props;
        const { text } = this.state;

        event.preventDefault();
        const values = this.maybeAdd(text);
        input.onChange(values);
    }

    public render() {
129
        const { placeholder, label, options, t } = this.props;
Maxym Borodenko's avatar
Maxym Borodenko committed
130 131 132 133 134 135 136 137
        const { text, values } = this.state;

        const withOptions: boolean = options !== undefined && typeof options === 'object';

        return (
            <div>
                { withOptions ?
                  <FormControl fullWidth component="fieldset">
138
                    { label && <FormLabel component="label">{ t(`${label}`) }</FormLabel> }
Maxym Borodenko's avatar
Maxym Borodenko committed
139 140
                    <FormGroup>
                    { Object.keys(options).map((key) => (
141
                      <FormControlLabel key={ `${key}` } label={ t(`${options[key]}`) }
Maxym Borodenko's avatar
Maxym Borodenko committed
142 143 144 145 146 147 148 149 150 151
                        control={
                          <Checkbox value={ key }
                            checked={ values.indexOf(key) >= 0 }
                            onChange={ this.handleCheckbox } />
                        } />
                    )) }
                    </FormGroup>
                  </FormControl>
                  :
                  <FormControl fullWidth className="full-width">
152
                    <InputLabel>{ t(`${label}`) }</InputLabel>
Maxym Borodenko's avatar
Maxym Borodenko committed
153 154 155 156
                    <Input
                      className="full-width"
                      value={ text }
                      onChange={ this.handleChange }
157
                      placeholder={ placeholder ? t(`${placeholder}`) : '' }
Maxym Borodenko's avatar
Maxym Borodenko committed
158 159 160 161 162
                      onKeyPress={ this.handleKeyPres }
                      onBlur={ this.handleAddCurrent }
                      endAdornment={
                        <InputAdornment position="end">
                          <IconButton type="button" onClick={ this.handleAddCurrent }>
Maxym Borodenko's avatar
Maxym Borodenko committed
163
                            <PlusOne style={ { fontSize: '1.5rem' } } />
Maxym Borodenko's avatar
Maxym Borodenko committed
164 165 166 167 168 169 170 171 172 173
                          </IconButton>
                        </InputAdornment>
                      }
                    />
                  </FormControl>
                }
                { (! withOptions && values && values.length > 0) &&
                  values.map((val, index) => (
                      <div style={ { margin: '.2rem 0', padding: '.2rem 1rem', backgroundColor: '#e8e5e1', color: '#202222' } } key={ val }>
                        { withOptions ? options[val] || val : val }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
174
                        <div className="font-bold float-right" onClick={ this.removeByIndex(index) }>X</div>
Maxym Borodenko's avatar
Maxym Borodenko committed
175 176 177 178 179 180 181 182 183 184 185 186
                      </div>
                  )) }
            </div>
        );
    }
}

interface IStringArrFilter extends React.ClassAttributes<any> {
    name: string;
    placeholder?: string;
    label?: string;
    options?: { [key: string]: any; };
187
    t: any;
Maxym Borodenko's avatar
Maxym Borodenko committed
188 189 190 191 192 193 194 195 196
}

class StringArrFilter extends React.Component<IStringArrFilter, any> {

    public constructor(props: any) {
        super(props);
    }

    public render() {
197
        const { name, label, placeholder, options, t } = this.props;
Maxym Borodenko's avatar
Maxym Borodenko committed
198 199 200 201 202 203 204 205
        return (
            <div>
                <Field
                    name={ name }
                    component={ InternalStringArrField }
                    label={ label }
                    placeholder={ placeholder }
                    options={ options }
206
                    t={ t }
Maxym Borodenko's avatar
Maxym Borodenko committed
207 208 209 210 211 212
                />
            </div>
        );
    }
}

213
export default translate()(StringArrFilter);