@@ -2,15 +2,18 @@ import React from 'react'
22import PropTypes from 'prop-types'
33
44class EditableLabel extends React . Component {
5- state = { text : '' }
5+ constructor ( { value} ) {
6+ super ( )
7+ this . state = { value : value }
8+ }
69
710 getText = el => {
811 return el . innerText
912 }
1013
1114 onTextChange = ev => {
12- const text = this . getText ( ev . target )
13- this . setState ( { text : text } )
15+ const value = this . getText ( ev . target )
16+ this . setState ( { value : value } )
1417 }
1518
1619 componentDidMount ( ) {
@@ -20,20 +23,35 @@ class EditableLabel extends React.Component {
2023 }
2124
2225 onBlur = ( ) => {
23- this . props . onChange ( this . state . text )
26+ this . props . onChange ( this . state . value )
2427 }
2528
2629 onPaste = ev => {
2730 ev . preventDefault ( )
28- const text = ev . clipboardData . getData ( 'text' )
29- document . execCommand ( 'insertText' , false , text )
31+ const value = ev . clipboardData . getData ( 'text' )
32+ document . execCommand ( 'insertText' , false , value )
3033 }
3134
3235 getClassName = ( ) => {
33- const placeholder = this . state . text === '' ? 'comPlainTextContentEditable--has-placeholder' : ''
36+ const placeholder = this . state . value === '' ? 'comPlainTextContentEditable--has-placeholder' : ''
3437 return `comPlainTextContentEditable ${ placeholder } `
3538 }
3639
40+ onKeyDown = ( e ) => {
41+ if ( e . keyCode === 13 ) {
42+ this . props . onChange ( this . state . value )
43+ this . refDiv . blur ( )
44+ e . preventDefault ( )
45+ }
46+ if ( e . keyCode === 27 ) {
47+ this . refDiv . value = this . props . value
48+ this . setState ( { value : this . props . value } )
49+ // this.refDiv.blur()
50+ e . preventDefault ( )
51+ e . stopPropagation ( )
52+ }
53+ }
54+
3755 render ( ) {
3856 return (
3957 < div
@@ -43,21 +61,26 @@ class EditableLabel extends React.Component {
4361 onPaste = { this . onPaste }
4462 onBlur = { this . onBlur }
4563 onInput = { this . onTextChange }
46- placeholder = { this . props . placeholder }
47- />
64+ onKeyDown = { this . onKeyDown }
65+ placeholder = { this . props . value . length == 0 ? false : this . props . placeholder }
66+ > { this . props . value } </ div >
4867 )
4968 }
5069}
5170
52- EditableLabel . defaultProps = {
53- onChange : ( ) => { } ,
54- placeholder : '' ,
55- autoFocus : false
56- }
5771EditableLabel . propTypes = {
5872 onChange : PropTypes . func ,
5973 placeholder : PropTypes . string ,
60- autoFocus : PropTypes . bool
74+ autoFocus : PropTypes . bool ,
75+ inline : PropTypes . bool ,
76+ value : PropTypes . string ,
6177}
6278
79+ EditableLabel . defaultProps = {
80+ onChange : ( ) => { } ,
81+ placeholder : '' ,
82+ autoFocus : false ,
83+ inline : false ,
84+ value : ''
85+ }
6386export default EditableLabel
0 commit comments