Skip to content

Commit

Permalink
Merge pull request #151 from flovilmart/creator
Browse files Browse the repository at this point in the history
Adds tag creator
  • Loading branch information
dcousens committed May 18, 2015
2 parents 25c77bd + 2ef9cbe commit 3345d4a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
Empty file added examples/src/.npmignore
Empty file.
34 changes: 34 additions & 0 deletions examples/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,47 @@ var SelectedValuesField = React.createClass({
}
});

var SelectedValuesFieldCreate = React.createClass({

onLabelClick: function (data, event) {
console.log(data, event);
},

render: function() {
var ops = [
{ label: 'Chocolate', value: 'chocolate' },
{ label: 'Vanilla', value: 'vanilla' },
{ label: 'Strawberry', value: 'strawberry' },
{ label: 'Caramel', value: 'caramel' },
{ label: 'Cookies and Cream', value: 'cookiescream' },
{ label: 'Peppermint', value: 'peppermint' }
];
return (
<div>
<label>{this.props.label}</label>
<Select
onOptionLabelClick={this.onLabelClick}
value="chocolate,vanilla,strawberry"
delimiter=","
multi={true}
allowCreate={true}
placeholder="Select your favourite(s)"
options={ops}
onChange={logChange} />
</div>
);
}
});



React.render(
<div>
<StatesField />
<StatesField label="States (non-searchable):" searchable={false} />
<MultiSelectField label="Multiselect:"/>
<SelectedValuesField label="Clickable labels (labels as links):" />
<SelectedValuesFieldCreate label="Clickable labels + Creation(labels as links):" />
<RemoteSelectField label="Remote Options:"/>
</div>,
document.getElementById('example')
Expand Down
25 changes: 23 additions & 2 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var Select = React.createClass({
matchPos: React.PropTypes.string, // (any|start) match the start or entire string when filtering
matchProp: React.PropTypes.string, // (any|label|value) which option property to filter on
inputProps: React.PropTypes.object, // custom attributes for the Input (in the Select-control) e.g: {'data-foo': 'bar'}

allowCreate: React.PropTypes.bool, // wether to allow creation of new entries
/*
* Allow user to make option label clickable. When this handler is defined we should
* wrap label into <a>label</a> tag.
Expand Down Expand Up @@ -66,6 +66,7 @@ var Select = React.createClass({
matchPos: 'any',
matchProp: 'any',
inputProps: {},
allowCreate: false,

onOptionLabelClick: undefined
};
Expand Down Expand Up @@ -383,6 +384,14 @@ var Select = React.createClass({
this.focusNextOption();
break;

case 188: // ,
if (this.props.allowCreate) {
event.preventDefault();
event.stopPropagation();
this.selectFocusedOption();
};
break;

default: return;
}

Expand Down Expand Up @@ -516,6 +525,9 @@ var Select = React.createClass({
},

selectFocusedOption: function() {
if (this.props.allowCreate && !this.state.focusedOption) {
return this.selectValue(this.state.inputValue);
};
return this.selectValue(this.state.focusedOption);
},

Expand Down Expand Up @@ -592,6 +604,15 @@ var Select = React.createClass({
if(this.state.filteredOptions.length > 0) {
focusedValue = focusedValue == null ? this.state.filteredOptions[0] : focusedValue;
}
// Add the current value to the filtered options in last resort
if (this.props.allowCreate && !this.state.filteredOptions.length) {
var inputValue = this.state.inputValue;
this.state.filteredOptions.push({
value: inputValue,
label: inputValue,
create: true
})
};

var ops = Object.keys(this.state.filteredOptions).map(function(key) {
var op = this.state.filteredOptions[key];
Expand All @@ -612,7 +633,7 @@ var Select = React.createClass({
if (op.disabled) {
return <div ref={ref} key={'option-' + op.value} className={optionClass}>{op.label}</div>;
} else {
return <div ref={ref} key={'option-' + op.value} className={optionClass} onMouseEnter={mouseEnter} onMouseLeave={mouseLeave} onMouseDown={mouseDown} onClick={mouseDown}>{op.label}</div>;
return <div ref={ref} key={'option-' + op.value} className={optionClass} onMouseEnter={mouseEnter} onMouseLeave={mouseLeave} onMouseDown={mouseDown} onClick={mouseDown}>{ op.create ? "Add "+op.label+" ?" : op.label}</div>;
}
}, this);

Expand Down

0 comments on commit 3345d4a

Please sign in to comment.