StringArrFilter.tsx 5.91 KB
Newer Older
Maxym Borodenko's avatar
Maxym Borodenko 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 48 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
import * as React from 'react';
import { Field } from 'redux-form';

import { FormControl, FormLabel, FormGroup, FormControlLabel } from 'material-ui/Form';
import Checkbox from 'material-ui/Checkbox';

import IconButton from 'material-ui/IconButton';
import Input, { InputLabel, InputAdornment } from 'material-ui/Input';
import PlusOne from 'material-ui-icons/PlusOne';

interface IStringArrFilterInternal extends React.ClassAttributes<any> {
    name: string;
    input: any;
    placeholder?: string;
    label?: string;
    options?: { [key: string]: any; };
}

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

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

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

    public componentWillReceiveProps(nextProps) {
        this.setState({
            values: [ ...nextProps.input.value ],
            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() {
        const { placeholder, label, options } = this.props;
        const { text, values } = this.state;

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

        return (
            <div>
                { withOptions ?
                  <FormControl fullWidth component="fieldset">
                    { label && <FormLabel component="label">{ label }</FormLabel> }
                    <FormGroup>
                    { Object.keys(options).map((key) => (
                      <FormControlLabel key={ `${key}` } label={ options[key] }
                        control={
                          <Checkbox value={ key }
                            checked={ values.indexOf(key) >= 0 }
                            onChange={ this.handleCheckbox } />
                        } />
                    )) }
                    </FormGroup>
                  </FormControl>
                  :
                  <FormControl fullWidth className="full-width">
                    <InputLabel>{ label }</InputLabel>
                    <Input
                      className="full-width"
                      value={ text }
                      onChange={ this.handleChange }
                      placeholder={ placeholder || '' }
                      onKeyPress={ this.handleKeyPres }
                      onBlur={ this.handleAddCurrent }
                      endAdornment={
                        <InputAdornment position="end">
                          <IconButton type="button" onClick={ this.handleAddCurrent }>
                            <PlusOne />
                          </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 }
                        <div style={ { float: 'right' } } className="font-bold" onClick={ this.removeByIndex(index) }>X</div>
                      </div>
                  )) }
            </div>
        );
    }
}

interface IStringArrFilter extends React.ClassAttributes<any> {
    name: string;
    placeholder?: string;
    label?: string;
    options?: { [key: string]: any; };
}

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

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

    public render() {
        const { name, label, placeholder, options } = this.props;
        return (
            <div>
                <Field
                    name={ name }
                    component={ InternalStringArrField }
                    label={ label }
                    placeholder={ placeholder }
                    options={ options }
                />
            </div>
        );
    }
}

export default StringArrFilter;