11import React from 'react' ;
22import { connect } from 'react-redux' ;
33import { Controlled as CodeMirror } from 'react-codemirror2' ;
4+ import Tags from 'react-tagging-input' ;
45
56import 'codemirror/lib/codemirror.css' ;
67
78import Title from './common/Title' ;
8- import Input from './common/Input' ;
99import ListBoxWithSearch from './ListBoxWithSearch' ;
1010import * as actions from '../actions' ;
1111
@@ -20,28 +20,35 @@ class NewSnippet extends React.Component {
2020 tags : [ ] ,
2121 syntax : '' , // eslint-disable-line react/no-unused-state
2222 } ;
23+ this . onKeyPress = ( e ) => {
24+ if ( e . which === 13 ) { e . preventDefault ( ) ; }
25+ } ;
2326 this . postSnippet = this . postSnippet . bind ( this ) ;
2427 this . onSyntaxClick = this . onSyntaxClick . bind ( this ) ;
2528 this . onInputChange = this . onInputChange . bind ( this ) ;
29+ this . onTagAdded = this . onTagAdded . bind ( this ) ;
30+ this . onTagRemoved = this . onTagRemoved . bind ( this ) ;
2631 }
2732
2833 componentDidMount ( ) {
2934 const { dispatch } = this . props ;
30-
3135 dispatch ( actions . fetchSyntaxes ) ;
3236 }
3337
38+ onTagAdded ( tag ) {
39+ this . setState ( { tags : [ ...this . state . tags , tag ] } ) ;
40+ }
41+
42+ onTagRemoved ( tag ) {
43+ this . setState ( { tags : this . state . tags . filter ( item => item !== tag ) } ) ;
44+ }
45+
3446 onSyntaxClick ( syntax ) {
3547 this . setState ( { syntax } ) ; // eslint-disable-line react/no-unused-state
3648 }
3749
3850 onInputChange ( e ) {
39- const { name } = e . target ;
40- let { value } = e . target ;
41-
42- if ( name === 'tags' ) {
43- value = value . split ( ',' ) . map ( item => item . trim ( ) ) ;
44- }
51+ const { name, value } = e . target ;
4552
4653 this . setState ( { [ name ] : value } ) ;
4754 }
@@ -56,20 +63,28 @@ class NewSnippet extends React.Component {
5663 return (
5764 [
5865 < Title title = "New snippet" key = "New Snippet Title" /> ,
59- < form className = "new-snippet" key = "New Snippet" onSubmit = { this . postSnippet } >
66+ < form
67+ className = "new-snippet"
68+ key = "New Snippet"
69+ onKeyPress = { this . onKeyPress }
70+ onSubmit = { this . postSnippet }
71+ role = "presentation"
72+ >
6073 < div className = "new-snippet-code-wrapper" >
6174 < div className = "new-snippet-code-header" >
62- < Input
75+ < input
76+ className = "input"
6377 placeholder = "Title"
6478 name = "title"
65- onChangeHandler = { this . onInputChange }
79+ type = "text"
6680 value = { this . state . title }
81+ onChange = { this . onInputChange }
6782 />
68- < Input
69- placeholder = "Tags (separate tags by comma)"
70- name = "tags "
71- onChangeHandler = { this . onInputChange }
72- value = { this . state . tags . toString ( ) }
83+ < Tags
84+ tags = { this . state . tags }
85+ placeholder = "Tags "
86+ onAdded = { this . onTagAdded }
87+ onRemoved = { this . onTagRemoved }
7388 />
7489 </ div >
7590 < div className = "new-snippet-code" >
0 commit comments