Skip to content

Commit

Permalink
Add support for className and style in options
Browse files Browse the repository at this point in the history
Using the default optionRenderer, specifying `style` or
`className` means the style/class  gets applied at the same
level as `.Select-option`

Closes JedWatson#44
  • Loading branch information
bruderstein committed Aug 13, 2015
1 parent 2c97c52 commit 3edf78b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Option.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var React = require('react');
var classes = require('classnames');

var Option = React.createClass({
propTypes: {
Expand All @@ -14,11 +15,12 @@ var Option = React.createClass({
render: function() {
var obj = this.props.option;
var renderedLabel = this.props.renderFunc(obj);
var optionClasses = classes(this.props.className, obj.className);

return obj.disabled ? (
<div className={this.props.className}>{renderedLabel}</div>
<div className={optionClasses}>{renderedLabel}</div>
) : (
<div className={this.props.className}
<div className={optionClasses} style={obj.style}
onMouseEnter={this.props.mouseEnter}
onMouseLeave={this.props.mouseLeave}
onMouseDown={this.props.mouseDown}
Expand Down
34 changes: 34 additions & 0 deletions test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,40 @@ describe('Select', function() {
});
});

describe('with styled options', function () {

beforeEach(function () {

options = [
{ value: 'one', label: 'One', className: 'extra-one' },
{ value: 'two', label: 'Two', className: 'extra-two' },
{ value: 'three', label: 'Three', style: { fontSize: 25 } }
];

instance = createControl({
options: options
});
});

it('uses the given className for an option', function () {

clickArrowToOpen();
expect(React.findDOMNode(instance).querySelectorAll('.Select-option')[0], 'to have attributes',
{
class: 'extra-one'
});
});

it('uses the given style for an option', function () {

clickArrowToOpen();
expect(React.findDOMNode(instance).querySelectorAll('.Select-option')[2], 'to have attributes',
{
style: { 'font-size': '25px' }
});
});
});

describe('with allowCreate=true', function () {

beforeEach(function () {
Expand Down

0 comments on commit 3edf78b

Please sign in to comment.