Skip to content
This repository has been archived by the owner on May 17, 2023. It is now read-only.

Commit

Permalink
Allow to have nothing selected on Select (#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
Franco Correa authored and siddharthkp committed Sep 28, 2018
1 parent 2a55d0f commit 45c235b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
12 changes: 10 additions & 2 deletions core/components/atoms/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const Select = ({ options, ...props }) => {

return (
<StyledSelect {...props} {...Automation('select')}>
{/* First option will be selected if there is no value passed as a prop */}
<option disabled hidden selected={!props.value} value="" {...Automation('select.option')}>
{props.placeholder}
</option>

{options.map((option, index) => (
<option key={index} value={option.value} {...Automation('select.option')}>
{option.text}
Expand All @@ -41,11 +46,14 @@ Select.propTypes = {
/** Value selected by default */
value: PropTypes.any,
/** onChange transparently passed to select */
onChange: PropTypes.func
onChange: PropTypes.func,
/** String to show when the first empty choice is selected */
placeholder: PropTypes.string
}

Select.defaultProps = {
readOnly: false
readOnly: false,
placeholder: ''
}

export default Select
Expand Down
2 changes: 2 additions & 0 deletions core/components/atoms/select/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
```jsx
<Select {props}
placeholder="Select an option..."
options={[
{ text: 'One', value: 1 },
{ text: 'Two', value: 2 },
Expand All @@ -32,6 +33,7 @@ You can also indicate that a `<Select>` should be read-only by setting the `read
```js
<Select
readOnly
value={1}
options={[{ text: 'One', value: 1 }, { text: 'Two', value: 2 }, { text: 'Three', value: 3 }]}
onChange={event => console.log(event)}
/>
Expand Down
27 changes: 16 additions & 11 deletions core/components/atoms/select/select.story.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,23 @@ storiesOf('Select').add('readonly', () => (
storiesOf('Select').add('stressed', () => (
<Example title="stressed - 56 characters with 300px width">
<Select
value={1}
style={{ width: 300 }}
options={[{
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
value: 1
}, {
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
value: 2
}, {
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
value: 3
}]}
options={[
{
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
value: 1
},
{
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
value: 2
},
{
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
value: 3
}
]}
onChange={event => console.log(event)}
/>
</Example>
))
))
10 changes: 10 additions & 0 deletions internal/test/snapshot/__snapshots__/select.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,18 @@ exports[`Select renders with automation attribute 1`] = `
<select
className="c0"
data-cosmos-key="select"
placeholder=""
readOnly={false}
>
<option
data-cosmos-key="select.option"
disabled={true}
hidden={true}
selected={true}
value=""
>
</option>
<option
data-cosmos-key="select.option"
value={1}
Expand Down
7 changes: 6 additions & 1 deletion internal/test/unit/automation-attributes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ describe('Automation data attributes', () => {
const select = shallow(<Select options={[{ text: 'One', value: 1 }]} />)

expect(select.prop('data-cosmos-key')).toEqual('select')
expect(select.children().prop('data-cosmos-key')).toEqual('select.option')
expect(
select
.children()
.first()
.prop('data-cosmos-key')
).toEqual('select.option')
})

it('Switch', () => {
Expand Down

0 comments on commit 45c235b

Please sign in to comment.