DictionarySelect.js 1.2 KB
Newer Older
1 2
import React, { PureComponent } from 'react';
import { Select } from 'antd';
sin's avatar
sin committed
3
import DictionaryContext from './DictionaryContext';
4 5

export default class DictionarySelect extends PureComponent {
sin's avatar
sin committed
6
  renderSelect(children) {
7
    // const { initialValue } = this.props['data-__meta'];
8 9 10 11 12 13 14 15
    const propsX = {
      ...this.props,
    };
    if (propsX.value !== undefined || propsX.value !== null) {
      propsX.value = `${propsX.value}`;
    }
    if (propsX.value === 'undefined' || propsX.value === 'null') {
      delete propsX.value;
16 17
    }
    return <Select {...propsX}>{children}</Select>;
18 19 20
  }

  render() {
sin's avatar
sin committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    const { dicKey } = this.props;
    return (
      <DictionaryContext.Consumer>
        {context => {
          const dicValues = context[dicKey];
          let nodes = [];
          if (dicValues) {
            nodes = Object.keys(dicValues).map(value => {
              const text = dicValues[value];
              return (
                <Select.Option key={value} value={value}>
                  {text}
                </Select.Option>
              );
            });
          }
          return this.renderSelect(nodes);
        }}
      </DictionaryContext.Consumer>
    );
41 42
  }
}