DescriptorListPicker.tsx 5.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import * as React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { translate } from 'react-i18next';

import DescriptorList from 'model/catalog/DescriptorList';
import { autocomplete, promiseDescriptorList } from 'descriptorlists/actions/public';

import { Field } from 'redux-form';
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';
import Input from '@material-ui/core/Input';
import TextField from '@material-ui/core/TextField';
import FormControl from 'ui/common/forms/FormControl';
import Select from '@material-ui/core/Select';
Matija Obreza's avatar
Matija Obreza committed
16
import Markdown from 'ui/common/markdown';
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

interface ISelectDescriptorListInternal extends React.ClassAttributes<any> {
  input: any;
  listDescriptorLists: DescriptorList[];
  autocomplete: (term: string) => Promise<DescriptorList[]>;
  loadDescriptorList: (uuid: string) => Promise<DescriptorList>;
  label: string;
  meta?: any;
  t: any;
}

class SelectDescriptorListInternal extends React.Component<ISelectDescriptorListInternal, any> {

  protected handleInputChange = (event) => {
    this.setState({ searchText: event.target.value });
    this.props.autocomplete(event.target.value).then((suggestions) => {
      this.setState({ suggestions });
    });
  }
  protected handleClick = (event) => {
    this.setState({ open: true, anchorEl: event.currentTarget });
  }
  protected handleRequestClose = () => {
    this.setState({ open: false });
  }
  protected select = (value) => (event) => {
    const { input } = this.props;
    const descriptorList = this.state.suggestions.find((dl) => dl.uuid === value);
    if (descriptorList) {
      input.onChange({ uuid: [descriptorList.uuid] });
      this.setState({
        open: false,
        pickerList: { uuid: descriptorList.uuid, title: descriptorList.title },
      });
    } else {
      input.onChange('');
      this.setState({
        open: false,
        pickerList: null,
      });
    }
  }

  private constructor(props, context) {
    super(props, context);
    this.state = {
      pickerList: null,
      open: false,
      anchorEl: null,
      searchText: '',
      suggestions: [],
    };
  }

  public componentWillMount() {
    if (this.props.input.value && this.props.input.value.uuid) {
      this.props.loadDescriptorList(this.props.input.value.uuid[0]).then((dl) => {
        this.setState({ pickerList: { uuid: dl.uuid, title: dl.title } });
      });
    }
    this.props.autocomplete('').then((data) => {
      this.setState({ suggestions: data });
    });
  }

  public componentWillReceiveProps(nextProps) {
    if (nextProps.input.value && nextProps.input.value.uuid && nextProps.input.value.uuid.length > 0) {
      const uuid = nextProps.input.value.uuid[0];
      const descriptorList = this.state.suggestions.find((dl) => dl.uuid === uuid);
      if (descriptorList) {
        this.setState({ pickerList: { uuid: descriptorList.uuid, title: descriptorList.title } });
      } else {
        this.props.loadDescriptorList(uuid).then((dl) => {
          this.setState({ pickerList: { uuid: dl.uuid, title: dl.title } });
        });
      }
    } else {
      this.setState({ pickerList: null });
    }
  }

  public render() {
    const { label, meta, t } = this.props;
    const inputValue = this.state.pickerList && this.state.pickerList.uuid ? this.state.pickerList.uuid : '';
    return (
      <FormControl fullWidth meta={ meta } label={ label }>
        <Select
          onClick={ this.handleClick }
          value={ inputValue }
          input={ <Input/> }
          MenuProps={ {
            open: false,
          } }
        >
          {
            this.state.pickerList ? (
              <MenuItem value={ inputValue }>
                <Markdown basic source={ this.state.pickerList.title }/>
              </MenuItem>) : null
          }
        </Select>
        <Menu
          anchorEl={ this.state.anchorEl }
          open={ this.state.open }
          onClose={ this.handleRequestClose }
        >
          <MenuItem>
            <TextField
              fullWidth
              value={ this.state.searchText }
              label={ t('descriptors.dashboard.c.listPicker.search') }
              onChange={ this.handleInputChange }
            />
          </MenuItem>
          {
            this.state.suggestions.map((dl, index) => (
              <MenuItem onClick={ this.select(dl.uuid) } key={ `descriptor-list-${index}` }>
                <Markdown basic source={ dl.title }/>
              </MenuItem>
            ))
          }
        </Menu>
      </FormControl>
    );
  }
}

interface IDescriptorListPicker extends React.ClassAttributes<any> {
  name: string;
  label: string;
  className?: string;
  autocomplete: (term: string) => Promise<DescriptorList[]>;
  promiseDescriptorList: (uuid: string) => Promise<DescriptorList>;
  t: any;
}

class DescriptorListPicker extends React.Component<IDescriptorListPicker, any> {

  public render() {
    const { name, label, className, autocomplete, promiseDescriptorList, t } = this.props;

    return (
      <div className={ className }>
        <Field
          name={ name }
          component={ SelectDescriptorListInternal }
          label={ label }
          autocomplete={ autocomplete }
          loadDescriptorList={ promiseDescriptorList }
          t={ t }
        />
      </div>
    );
  }
}

const mapDispatchToProps = (dispatch) => bindActionCreators({
  autocomplete,
  promiseDescriptorList,
}, dispatch);

export default translate()(connect(null, mapDispatchToProps)(DescriptorListPicker));