|
1 | 1 | import React from 'react' |
2 | 2 | import PropTypes from 'prop-types' |
3 | 3 | import {InlineInput} from 'styles/Base' |
| 4 | +import autosize from 'autosize' |
4 | 5 |
|
5 | 6 | class InlineInputController extends React.Component { |
6 | | - onFocus = (e) => e.target.select() |
| 7 | + onFocus = (e) => e.target.select() |
7 | 8 |
|
8 | | - // This is the way to select all text if mouse clicked |
9 | | - onMouseDown = (e) => { |
10 | | - if (document.activeElement != e.target) { |
11 | | - e.preventDefault() |
12 | | - this.refInput.focus() |
13 | | - } |
14 | | - } |
| 9 | + // This is the way to select all text if mouse clicked |
| 10 | + onMouseDown = (e) => { |
| 11 | + if (document.activeElement != e.target) { |
| 12 | + e.preventDefault() |
| 13 | + this.refInput.focus() |
| 14 | + } |
| 15 | + } |
15 | 16 |
|
16 | | - onBlur = () => { |
17 | | - this.updateValue() |
18 | | - } |
| 17 | + onBlur = () => { |
| 18 | + this.updateValue() |
| 19 | + } |
19 | 20 |
|
20 | | - onKeyDown = (e) => { |
21 | | - if(e.keyCode == 13) { |
22 | | - this.refInput.blur() |
23 | | - e.preventDefault() |
24 | | - } |
25 | | - if(e.keyCode == 27) { |
26 | | - this.refInput.blur() |
27 | | - e.preventDefault() |
28 | | - } |
29 | | - } |
| 21 | + onKeyDown = (e) => { |
| 22 | + if(e.keyCode == 13) { |
| 23 | + this.refInput.blur() |
| 24 | + e.preventDefault() |
| 25 | + } |
| 26 | + if(e.keyCode == 27) { |
| 27 | + this.setValue(this.props.value) |
| 28 | + this.refInput.blur() |
| 29 | + e.preventDefault() |
| 30 | + } |
| 31 | + if(e.keyCode == 9) { |
| 32 | + if (this.getValue().length == 0) { |
| 33 | + this.props.onCancel() |
| 34 | + } |
| 35 | + this.refInput.blur() |
| 36 | + e.preventDefault() |
| 37 | + } |
| 38 | + } |
30 | 39 |
|
31 | | - getValue = () => this.refInput.value |
| 40 | + getValue = () => this.refInput.value |
| 41 | + setValue = (value) => this.refInput.value=value |
32 | 42 |
|
33 | | - updateValue = () => { |
34 | | - if (this.getValue() != this.props.value) { |
35 | | - this.props.onChange(this.getValue()) |
36 | | - } |
37 | | - } |
| 43 | + updateValue = () => { |
| 44 | + if (this.getValue() != this.props.value) { |
| 45 | + this.props.onSave(this.getValue()) |
| 46 | + } |
| 47 | + } |
38 | 48 |
|
39 | | - render() { |
40 | | - const {autoFocus, value, placeholder} = this.props |
| 49 | + setRef = (ref) => { |
| 50 | + this.refInput = ref |
| 51 | + if (this.props.resize != 'none') { |
| 52 | + autosize(this.refInput) |
| 53 | + } |
| 54 | + } |
41 | 55 |
|
42 | | - return <InlineInput |
43 | | - ref={ref => (this.refInput = ref)} |
44 | | - onMouseDown={this.onMouseDown} |
45 | | - onFocus={this.onFocus} |
46 | | - onBlur={this.onBlur} |
47 | | - onKeyDown={this.onKeyDown} |
48 | | - placeholder={value.length == 0 ? false : placeholder} |
49 | | - defaultValue={value} |
50 | | - /> |
| 56 | + componentWillReceiveProps(nextProps) { |
| 57 | + this.setValue(nextProps.value) |
| 58 | + } |
| 59 | + |
| 60 | + render() { |
| 61 | + const {autoFocus, border, value, placeholder} = this.props |
| 62 | + |
| 63 | + return <InlineInput |
| 64 | + ref={this.setRef} |
| 65 | + border={border} |
| 66 | + onMouseDown={this.onMouseDown} |
| 67 | + onFocus={this.onFocus} |
| 68 | + onBlur={this.onBlur} |
| 69 | + onKeyDown={this.onKeyDown} |
| 70 | + placeholder={value.length == 0 ? undefined : placeholder} |
| 71 | + defaultValue={value} |
| 72 | + autoComplete="off" |
| 73 | + autoCorrect="off" |
| 74 | + autoCapitalize="off" |
| 75 | + spellCheck="false" |
| 76 | + dataGramm="false" |
| 77 | + rows={1} |
| 78 | + autoFocus={autoFocus} |
| 79 | + /> |
| 80 | + } |
51 | 81 | } |
52 | | - } |
53 | 82 |
|
54 | | - InlineInputController.defaultProps = { |
55 | | - onChange: () => {}, |
56 | | - placeholder: '', |
57 | | - value: '', |
58 | | - } |
| 83 | +InlineInputController.propTypes = { |
| 84 | + onSave: PropTypes.func, |
| 85 | + border: PropTypes.bool, |
| 86 | + placeholder: PropTypes.string, |
| 87 | + value: PropTypes.string, |
| 88 | + autoFocus: PropTypes.bool, |
| 89 | + resize: PropTypes.oneOf(['none', 'vertical', 'horizontal']), |
| 90 | +} |
59 | 91 |
|
60 | | - InlineInputController.propTypes = { |
61 | | - onChange: PropTypes.func, |
62 | | - placeholder: PropTypes.string, |
63 | | - value: PropTypes.string |
64 | | - } |
| 92 | +InlineInputController.defaultProps = { |
| 93 | + onSave: () => {}, |
| 94 | + placeholder: '', |
| 95 | + value: '', |
| 96 | + border: false, |
| 97 | + autoFocus: false, |
| 98 | + resize: 'none' |
| 99 | +} |
65 | 100 |
|
66 | | - export default InlineInputController |
| 101 | +export default InlineInputController |
0 commit comments