Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix label displaying for initial value #152

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const Select = React.createClass({
value = this.addTitleToValue(props, value);
let inputValue = '';
if (props.combobox) {
inputValue = value.length ? String(value[0].key) : '';
inputValue = value.length ? this.getLabelFromProps(props, value[0].key) : '';
}
this.saveInputRef = saveRef.bind(this, 'inputInstance');
this.saveInputMirrorRef = saveRef.bind(this, 'inputMirrorInstance');
Expand Down
73 changes: 56 additions & 17 deletions tests/Select.combobox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,6 @@ describe('Select.combobox', () => {
expect(wrapper.state().inputValue).toBe('1');
});

it('display correct label when value changes', () => {
const wrapper = mount(
<Select
combobox
labelInValue
value={{ value: '', key: '' }}
optionLabelProp="children"
>
<Option value="1">One</Option>
<Option value="2">Two</Option>
</Select>
);

wrapper.setProps({ value: { label: 'One', key: '1' } });
expect(wrapper.find('input').props().value).toBe('One');
});

it('fire change event immediately when user inputing', () => {
const handleChange = jest.fn();
const wrapper = mount(
Expand Down Expand Up @@ -72,4 +55,60 @@ describe('Select.combobox', () => {
dropdownWrapper.find('MenuItem').first().simulate('click');
expect(wrapper.state().inputValue).toBe('1');
});

describe('input value', () => {
const createSelect = (props) => mount(
<Select
combobox
optionLabelProp="children"
{...props}
>
<Option value="1">One</Option>
<Option value="2">Two</Option>
</Select>
);

describe('labelInValue is false', () => {
it('displays correct input value for defaultValue', () => {
const wrapper = createSelect({
defaultValue: '1',
});
expect(wrapper.find('input').props().value).toBe('One');
});

it('displays correct input value for value', () => {
const wrapper = createSelect({
value: '1',
});
expect(wrapper.find('input').props().value).toBe('One');
});
});

describe('labelInValue is true', () => {
it('displays correct input value for defaultValue', () => {
const wrapper = createSelect({
labelInValue: true,
defaultValue: { key: '1', label: 'One' },
});
expect(wrapper.find('input').props().value).toBe('One');
});

it('displays correct input value for value', () => {
const wrapper = createSelect({
labelInValue: true,
value: { key: '1', label: 'One' },
});
expect(wrapper.find('input').props().value).toBe('One');
});

it('displays correct input value when value changes', () => {
const wrapper = createSelect({
labelInValue: true,
value: { key: '' },
});
wrapper.setProps({ value: { key: '1', label: 'One' } });
expect(wrapper.find('input').props().value).toBe('One');
});
});
});
});