Skip to content

Commit 2a3d83c

Browse files
committed
Refactor InlintInput
1 parent 3381ad9 commit 2a3d83c

6 files changed

Lines changed: 119 additions & 87 deletions

File tree

src/components/Lane/LaneHeader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const LaneHeaderComponent = ({
1212
<LaneHeader onDoubleClick={onDoubleClick}>
1313
<Title style={titleStyle}>
1414
{editLaneTitle ?
15-
<InlineInput value={title} border placeholder={t('placeholder.title')} onChange={updateTitle} /> :
15+
<InlineInput value={title} border placeholder={t('placeholder.title')} onSave={updateTitle} /> :
1616
title
1717
}
1818
</Title>
Lines changed: 85 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,101 @@
11
import React from 'react'
22
import PropTypes from 'prop-types'
33
import {InlineInput} from 'styles/Base'
4+
import autosize from 'autosize'
45

56
class InlineInputController extends React.Component {
6-
onFocus = (e) => e.target.select()
7+
onFocus = (e) => e.target.select()
78

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+
}
1516

16-
onBlur = () => {
17-
this.updateValue()
18-
}
17+
onBlur = () => {
18+
this.updateValue()
19+
}
1920

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+
}
3039

31-
getValue = () => this.refInput.value
40+
getValue = () => this.refInput.value
41+
setValue = (value) => this.refInput.value=value
3242

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+
}
3848

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+
}
4155

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+
}
5181
}
52-
}
5382

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+
}
5991

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+
}
65100

66-
export default InlineInputController
101+
export default InlineInputController

src/controllers/BoardContainer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class BoardContainer extends Component {
121121
onLaneClick,
122122
onLaneAdd,
123123
onLaneDelete,
124+
onLaneUpdate,
124125
editable,
125126
canAddLanes,
126127
laneStyle,

src/controllers/Lane.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ Lane.propTypes = {
278278
cardDraggable: PropTypes.bool,
279279
cardDragClass: PropTypes.string,
280280
canAddLanes: PropTypes.bool,
281+
onLaneUpdate: PropTypes.func,
281282
t: PropTypes.func.isRequired
282283
}
283284

@@ -287,6 +288,7 @@ Lane.defaultProps = {
287288
labelStyle: {},
288289
label: undefined,
289290
editable: false,
291+
onLaneUpdate: () => {},
290292
onCardAdd: () => {},
291293
}
292294

src/styles/Base.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import styled, { createGlobalStyle } from 'styled-components'
1+
import styled, { css, createGlobalStyle } from 'styled-components'
22

33
export const GlobalStyle = createGlobalStyle`
44
@@ -259,20 +259,29 @@ export const CardForm = styled.div`
259259
`
260260

261261
export const InlineInput = styled.textarea`
262-
overflow: hidden;
262+
overflow-x: hidden; /* for Firefox (issue #5) */
263263
word-wrap: break-word;
264+
min-height: 28px;
265+
max-height: 112px; /* optional, but recommended */
264266
resize: none;
265267
width: 100%;
266268
height: 28px;
269+
font-size: 15px;
267270
line-height: 20px;
268271
background-color: transparent;
269272
box-shadow: none;
273+
box-sizing: border-box;
270274
border-radius: 3px;
271275
border: 0;
272276
padding: 4px 8px;
273277
outline: 0;
278+
${props => props.border && css`
279+
&:focus {
280+
box-shadow: inset 0 0 0 2px #0079bf;
281+
}
282+
`
283+
}
274284
&:focus {
275285
background-color: white;
276-
box-shadow: inset 0 0 0 2px #0079bf;
277286
}
278287
`

0 commit comments

Comments
 (0)