Skip to content

Commit

Permalink
feat(select): Add an unselectable placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanoglesby08 committed Nov 29, 2017
1 parent 6786a75 commit 85ca195
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/components/Select/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ import formFieldStyles from '../FormFields.modules.scss'
import positionStyles from '../Position.modules.scss'
import iconWrapperStyles from '../Icons/IconWrapper.modules.scss'

const Select = ({ options, ...props }) => (
const Select = ({ options, placeholder, ...props }) => (
<FormField {...props}>
{({ className, ...selectProps }, showIcon, feedback) => (
<div className={positionStyles.relative}>
<select
{...selectProps}
className={joinClassNames(className, styles.select, formFieldStyles.height)}
>
{placeholder && (
<option value="" hidden disabled>
{placeholder}
</option>
)}
{options.map(({ text, value }) => (
<option key={value} value={value}>
{text}
Expand All @@ -43,7 +48,7 @@ const Select = ({ options, ...props }) => (

Select.propTypes = {
/**
* The selectable options
* The selectable options.
*/
options: PropTypes.arrayOf(
PropTypes.shape({
Expand All @@ -59,6 +64,10 @@ Select.propTypes = {
* Clarify the expected input.
*/
hint: PropTypes.string,
/**
* Show a un-selectable initial option.
*/
placeholder: PropTypes.string,
/**
* The value.
*/
Expand Down Expand Up @@ -108,6 +117,7 @@ Select.propTypes = {

Select.defaultProps = {
hint: undefined,
placeholder: undefined,
value: '',
feedback: undefined,
error: undefined,
Expand Down
14 changes: 12 additions & 2 deletions src/components/Select/__tests__/Select.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,16 @@ describe('Select', () => {
expect(findSelectElement()).toBeDisabled()
})

it('can have an un-selectable placeholder', () => {
const { findSelectElement } = doMount({ placeholder: 'Please select...' })

expect(findSelectElement()).toContainReact(
<option value="" disabled hidden>
Please select...
</option>
)
})

it('can have an error message', () => {
const { select } = doMount({ id: 'some-id', error: 'Oh no a terrible error!' })

Expand Down Expand Up @@ -336,10 +346,10 @@ describe('Select', () => {
})

it('passes additional attributes to the select element', () => {
const { findSelectElement } = doMount({ name: 'a name', placeholder: 'a placeholder' })
const { findSelectElement } = doMount({ name: 'a name', id: 'the-id' })

expect(findSelectElement()).toHaveProp('name', 'a name')
expect(findSelectElement()).toHaveProp('placeholder', 'a placeholder')
expect(findSelectElement()).toHaveProp('id', 'the-id')
})

it('does not allow custom CSS', () => {
Expand Down

0 comments on commit 85ca195

Please sign in to comment.