优化字典 select 和 text
This commit is contained in:
@@ -1,20 +1,32 @@
|
||||
import React, { PureComponent } from 'react';
|
||||
import { Select } from 'antd';
|
||||
import DictionaryContext from './DictionaryContext';
|
||||
|
||||
export default class DictionarySelect extends PureComponent {
|
||||
renderOptions() {
|
||||
const { list } = this.props;
|
||||
return list.map(item => {
|
||||
return (
|
||||
<Select.Option key={item.value} value={item.value}>
|
||||
{item.text}
|
||||
</Select.Option>
|
||||
);
|
||||
});
|
||||
renderSelect(children) {
|
||||
return <Select {...this.props}>{children}</Select>;
|
||||
}
|
||||
|
||||
render() {
|
||||
const options = this.renderOptions();
|
||||
return <Select {...this.props}>{options}</Select>;
|
||||
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>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user