Skip to content

Commit

Permalink
select: add disabled property (fix #183)
Browse files Browse the repository at this point in the history
  • Loading branch information
zemirco committed Nov 30, 2016
1 parent 207e097 commit c76fa9a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
5 changes: 5 additions & 0 deletions components/select/Select.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
outline: 0;
}

// https://material.google.com/style/color.html#color-text-background-colors
.Select-body:disabled {
color: rgba(0, 0, 0, 0.38);
}

.Select-placeholder {
color: rgba(0, 0, 0, 0.54);
}
Expand Down
5 changes: 3 additions & 2 deletions components/select/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const MAX_LIST_LENGTH = 5
export default class Select extends React.Component {

static propTypes = {
disabled: React.PropTypes.bool,
label: React.PropTypes.string,
items: React.PropTypes.array,
placeholder: React.PropTypes.string,
Expand Down Expand Up @@ -79,7 +80,7 @@ export default class Select extends React.Component {
}

render () {
const {selectedIndex, label} = this.props
const {selectedIndex, label, disabled} = this.props
const {open} = this.state
const empty = selectedIndex === -1
const text = empty ? this.props.placeholder : this.props.items[selectedIndex]
Expand All @@ -90,7 +91,7 @@ export default class Select extends React.Component {
label &&
<span className='Select-label'>{this.props.label}</span>
}
<button className='Select-body' onClick={this.open}>
<button className='Select-body' onClick={this.open} disabled={disabled}>
<span className={empty ? 'Select-placeholder' : ''}>
{text}
</span>
Expand Down
5 changes: 5 additions & 0 deletions components/select/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ describe('Select', () => {
assert.equal(wrapper.find('.Select-placeholder').text(), 'Placeholder')
})

it('should allow disabling the button', () => {
const wrapper = mount(<Select onChange={noop} disabled />)
assert(wrapper.find('.Select-body').props().disabled)
})

it('should render a given value instead of placeholder', () => {
const wrapper = mount(
<Select
Expand Down
55 changes: 55 additions & 0 deletions examples/js/components/selectRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,52 @@ const selectWithLabelComponent =
ReactDOM.render(<App />, mountNode)`

const selectDisabled =
`
class App extends React.Component {
state = {
first: -1,
second: -1
}
onChangeFirst = (index) => {
this.setState({
first: index
})
}
onChangeSecond = (index) => {
this.setState({
second: index
})
}
render () {
return (
<div style={{display: 'flex'}}>
<Select
label='first'
onChange={this.onChangeFirst}
items={['One', 'Two', 'Three']}
selectedIndex={this.state.first}
/>
<Select
label='second'
onChange={this.onChangeSecond}
items={['Eleven', 'Twelve', 'Thirteen']}
selectedIndex={this.state.second}
disabled={this.state.first === -1}
/>
</div>
)
}
}
ReactDOM.render(<App />, mountNode)
`

export default class SelectRoute extends React.Component {

render () {
Expand Down Expand Up @@ -122,6 +168,15 @@ export default class SelectRoute extends React.Component {
collapsableCode
/>
</section>
<section>
<h2>Disabled select</h2>
<Playground
codeText={selectDisabled}
scope={{React, ReactDOM, Select}}
noRender={false}
collapsableCode
/>
</section>
<section>
<h2>Default select</h2>
<Playground
Expand Down

0 comments on commit c76fa9a

Please sign in to comment.