Skip to content

Commit

Permalink
Create StoryLabels component
Browse files Browse the repository at this point in the history
  • Loading branch information
Murilo Varela committed Jun 21, 2017
1 parent af80a47 commit be0fc30
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 23 deletions.
29 changes: 29 additions & 0 deletions app/assets/javascripts/components/jquery_wrappers/InputTagit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';

class InputTagit extends React.Component {
componentDidMount() {
this.loadTagit();
}

loadTagit() {
$(this.input).tagit(
this.tagitProperties()
).on('change', this.props.onChange);
}

tagitProperties() {
console.log(this.props);
return {
availableTags: this.props.availableLabels,
readOnly: this.props.disabled,
};
}

render () {
return (
<input ref={ input => { this.input = input } } { ...this.props.input } tabIndex="this.props.labels"/>
);
}
}

export default InputTagit;
21 changes: 21 additions & 0 deletions app/assets/javascripts/components/story/StoryLabels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import InputTagit from 'components/jquery_wrappers/InputTagit';

const StoryLabels = ({ name, className, value, availableLabels, onChange, disabled = false }) =>
<div>
<label htmlFor={name}>{ I18n.t(`activerecord.attributes.story.${name}`) }</label>
<br />
<InputTagit
onChange={onChange}
availableLabels={availableLabels}
disabled={disabled}
input={{
name,
defaultValue: value,
className: `form-control input-sm ${className}`,
disabled,
}}
/>
</div>

export default StoryLabels;
44 changes: 21 additions & 23 deletions app/assets/javascripts/views/story_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import StoryDescription from 'components/story/StoryDescription';
import StoryHistoryLocation from 'components/story/StoryHistoryLocation';
import StorySelect from 'components/story/StorySelect';
import StoryDatePicker from 'components/story/StoryDatePicker';
import StoryLabels from 'components/story/StoryLabels';

var Clipboard = require('clipboard');

Expand Down Expand Up @@ -404,12 +405,9 @@ module.exports = FormView.extend({
);

this.$el.append(
this.makeFormControl({
name: "labels",
label: true,
control: this.textField("labels"),
class: 'form-control',
disabled: this.isReadonly()
this.makeFormControl(function(div) {
const $storyTags = $('<div class="form-group" data-tags></div>');
$(div).append($storyTags);
})
);

Expand Down Expand Up @@ -441,8 +439,6 @@ module.exports = FormView.extend({
})
);

this.initTags();

this.renderNotes();

if(this.model.get('story_type') === 'release') {
Expand Down Expand Up @@ -494,6 +490,21 @@ module.exports = FormView.extend({
);
}

const tagsInput = this.$('[data-tags]')[0];
if (tagsInput) {
ReactDOM.render(
<StoryLabels
name='labels'
className='labels'
value={this.model.get('labels')}
availableLabels={this.model.collection.labels}
onChange={ (event) => this.onChangeLabels(event) }
disabled={this.isReadonly()}
/>,
tagsInput
);
}

this.renderSelects();
},

Expand Down Expand Up @@ -699,21 +710,8 @@ module.exports = FormView.extend({
this.$el.find('a.collapse').removeClass(/icons-/).addClass("icons-collapse");
},

initTags: function() {
var model = this.model;
var $input = this.$el.find("input[name='labels']");
$input.tagit({
availableTags: model.collection.labels,
readOnly: this.isReadonly()
});

// Manually bind labels for now
$input.on('change', function(){
var that = this;
setTimeout(function() {
model.set({ labels: $(that).val()});
}, 50);
});
onChangeLabels: function(event){
this.model.set({ labels: event.target.value }, {silent: true});
},

renderNotes: function() {
Expand Down

0 comments on commit be0fc30

Please sign in to comment.