Skip to content

Commit

Permalink
fix(Select): defaultValue with async dataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
潕量 authored and tao1991123 committed Dec 18, 2018
1 parent 4ac7f41 commit 6fd23ba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/select/select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,13 @@ class Select extends Base {
super.componentWillReceiveProps(nextProps);

if ('value' in nextProps) {
// 更新valueDataSource值
// under controll
this.valueDataSource = getValueDataSource(nextProps.value, this.valueDataSource.mapValueDS, this.dataStore.getMapDS());
} else if ('defaultValue' in nextProps && (nextProps.children !== this.props.children || nextProps.dataSource !== this.props.dataSource)) {
//has defaultValue and dataSource changed
this.valueDataSource = getValueDataSource(nextProps.defaultValue, this.valueDataSource.mapValueDS, this.dataStore.getMapDS());
}

}

componentDidMount() {
Expand Down
3 changes: 2 additions & 1 deletion src/select/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ function getKeyItemByValue(value, valueMap) {
}

/**
* compute valueDataSource by new value
* @param {Array/String} value 数据
* @param {Object} mapValueDS 缓存数据 value => {value,label} 的映射关系表
* @param {Object} mapValueDS 上个value的缓存数据 value => {value,label} 的映射关系表
* @param {*} mapMenuDS 通过 dataSource 建立 value => {value,label} 的映射关系表
* @returns {Object} value: [value]; valueDS: [{value,label}]; mapValueDS: {value: {value,label}}
*/
Expand Down
19 changes: 19 additions & 0 deletions test/select/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ describe('Select', () => {
assert(wrapper.find('span.next-select em').text() === 'empty');
});

it('should support async dataSource', () => {

const DATASOURCE = [
{ label: 'TT1', value: 'test1' },
{ label: 'TT2', value: 'test2' },
{ label: 'TT3', value: 'test3' },
]

const wrapper = mount(<Select defaultValue="test2"/>);

wrapper.setProps({
dataSource: DATASOURCE,
});

wrapper.update();

assert(wrapper.find('.next-select em').text() === 'TT2');
});

it('should support not string value', (done) => {
const dataSource = [{ label: 'xxx', value: 123 }, { label: 'empty', value: false }];
const onChange = (value) => {
Expand Down

0 comments on commit 6fd23ba

Please sign in to comment.