InstitutePicker.tsx 4.66 KB
Newer Older
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
import * as React from 'react';
import { translate } from 'react-i18next';

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';
import Markdown from 'ui/common/markdown';
import VocabularyService from 'service/catalog/VocabularyService';

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

class SelectFaoInstituteInternal extends React.Component<ISelectFaoInstituteInternal, any> {

  protected handleInputChange = (event) => {
    this.setState({searchText: event.target.value});
    if (event.target.value) {
      VocabularyService.autocompleteWiewsTerm(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 institute = this.state.suggestions.find((inst) => inst.code === value);
    if (institute) {
      input.onChange({code: institute.code});
      this.setState({
        open: false,
        pickerList: {code: institute.code, title: institute.title},
      });
    } else {
      input.onChange('');
      this.setState({
        open: false,
        pickerList: null,
      });
    }
  }

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

  public componentWillMount() {
    const {input: {value}} = this.props;

    if (value) {
      this.setState({
        pickerList: {
          code:  value.code,
          title: value.fullName || value.code,
        },
        disabled: true,
      });
    }
  }

  public componentWillReceiveProps(nextProps) {
    if (! nextProps.input.value) {
      return this.setState({pickerList: null});
    }
    if (nextProps.input.value !== this.props.input.value) {
      const code = nextProps.input.value.code;
      const institute = this.state.suggestions.find((inst) => inst.code === code);
      if (institute) {
        this.setState({pickerList: {code: institute.code, title: institute.title}});
      } else {
        VocabularyService.getWiewsTerm(code).then((inst) => {
          this.setState({pickerList: {code: inst.code, title: inst.title}});
        });
      }
    }
  }

  public render() {
    const {label, meta, t} = this.props;
    const {pickerList: inputValue, disabled} = this.state;
    return (
      <FormControl fullWidth meta={ meta } label={ label } disabled={ disabled }>
        <Select
          onClick={ !disabled ? this.handleClick : null }
          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('partners.admin.c.institutePicker.search') }
              onChange={ this.handleInputChange }
            />
          </MenuItem>
          {
            this.state.suggestions.map((inst, index) => (
              <MenuItem onClick={ this.select(inst.code) } key={ `fao-institute-${index}` }>
                <Markdown basic source={ `${inst.code} - ${inst.title}` }/>
              </MenuItem>
            ))
          }
        </Menu>
      </FormControl>
    );
  }
}

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

class FaoInstitutePicker extends React.Component<IFaoInstitutePicker, any> {

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

    return (
      <div className={ className }>
        <Field
          name={ name }
          component={ SelectFaoInstituteInternal }
          label={ label }
          t={ t }
        />
      </div>
    );
  }
}

export default translate()(FaoInstitutePicker);