Skip to content

Commit 2a1950c

Browse files
authored
Merge pull request #152 from yesmeck/fix-combobox-initial-value
Fix label displaying for initial value
2 parents 6b72cb9 + 843c2ee commit 2a1950c

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

src/Select.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const Select = React.createClass({
121121
value = this.addTitleToValue(props, value);
122122
let inputValue = '';
123123
if (props.combobox) {
124-
inputValue = value.length ? String(value[0].key) : '';
124+
inputValue = value.length ? this.getLabelFromProps(props, value[0].key) : '';
125125
}
126126
this.saveInputRef = saveRef.bind(this, 'inputInstance');
127127
this.saveInputMirrorRef = saveRef.bind(this, 'inputMirrorInstance');

tests/Select.combobox.spec.js

+56-17
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,6 @@ describe('Select.combobox', () => {
2727
expect(wrapper.state().inputValue).toBe('1');
2828
});
2929

30-
it('display correct label when value changes', () => {
31-
const wrapper = mount(
32-
<Select
33-
combobox
34-
labelInValue
35-
value={{ value: '', key: '' }}
36-
optionLabelProp="children"
37-
>
38-
<Option value="1">One</Option>
39-
<Option value="2">Two</Option>
40-
</Select>
41-
);
42-
43-
wrapper.setProps({ value: { label: 'One', key: '1' } });
44-
expect(wrapper.find('input').props().value).toBe('One');
45-
});
46-
4730
it('fire change event immediately when user inputing', () => {
4831
const handleChange = jest.fn();
4932
const wrapper = mount(
@@ -72,4 +55,60 @@ describe('Select.combobox', () => {
7255
dropdownWrapper.find('MenuItem').first().simulate('click');
7356
expect(wrapper.state().inputValue).toBe('1');
7457
});
58+
59+
describe('input value', () => {
60+
const createSelect = (props) => mount(
61+
<Select
62+
combobox
63+
optionLabelProp="children"
64+
{...props}
65+
>
66+
<Option value="1">One</Option>
67+
<Option value="2">Two</Option>
68+
</Select>
69+
);
70+
71+
describe('labelInValue is false', () => {
72+
it('displays correct input value for defaultValue', () => {
73+
const wrapper = createSelect({
74+
defaultValue: '1',
75+
});
76+
expect(wrapper.find('input').props().value).toBe('One');
77+
});
78+
79+
it('displays correct input value for value', () => {
80+
const wrapper = createSelect({
81+
value: '1',
82+
});
83+
expect(wrapper.find('input').props().value).toBe('One');
84+
});
85+
});
86+
87+
describe('labelInValue is true', () => {
88+
it('displays correct input value for defaultValue', () => {
89+
const wrapper = createSelect({
90+
labelInValue: true,
91+
defaultValue: { key: '1', label: 'One' },
92+
});
93+
expect(wrapper.find('input').props().value).toBe('One');
94+
});
95+
96+
it('displays correct input value for value', () => {
97+
const wrapper = createSelect({
98+
labelInValue: true,
99+
value: { key: '1', label: 'One' },
100+
});
101+
expect(wrapper.find('input').props().value).toBe('One');
102+
});
103+
104+
it('displays correct input value when value changes', () => {
105+
const wrapper = createSelect({
106+
labelInValue: true,
107+
value: { key: '' },
108+
});
109+
wrapper.setProps({ value: { key: '1', label: 'One' } });
110+
expect(wrapper.find('input').props().value).toBe('One');
111+
});
112+
});
113+
});
75114
});

0 commit comments

Comments
 (0)