-
Notifications
You must be signed in to change notification settings - Fork 54
/
CellTitleInput.component.js
76 lines (67 loc) · 1.8 KB
/
CellTitleInput.component.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { Component } from 'react';
import PropTypes from 'prop-types';
import theme from './CellTitle.module.scss';
/**
* Title input mode.
* - It initializes the input value
* - It adds handlers on form submit, ESC (cancel) and blur (submit) events
*/
export default class CellTitleInput extends Component {
constructor(props) {
super(props);
this.onBlur = this.onBlur.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
this.onSubmit = this.onSubmit.bind(this);
}
componentDidMount() {
this.titleInput.value = this.props.cellData;
}
onKeyUp(event) {
if (event.key === 'Esc' || event.key === 'Escape') {
this.props.onEditCancel(event, this.props.rowData);
}
}
onBlur(event) {
this.onSubmit(event);
}
onSubmit(event) {
event.preventDefault();
this.props.onEditSubmit(event, {
value: this.titleInput.value,
model: this.props.rowData,
});
}
render() {
return (
<form onSubmit={this.onSubmit} className={theme['edit-form']}>
<label aria-hidden="true" hidden className="sr-only" htmlFor={this.props.id}>
{this.props.label || 'title'}
</label>
<input
id={this.props.id}
ref={input => {
this.titleInput = input;
}}
onBlur={this.onBlur}
onKeyUp={this.onKeyUp}
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus
/>
</form>
);
}
}
CellTitleInput.propTypes = {
/** The id prefix. */
id: PropTypes.string,
/** The input value. */
cellData: PropTypes.string.isRequired,
/** The cancel callback on ESC keydown. */
onEditCancel: PropTypes.func.isRequired,
/** The submit callback on ENTER keydown or blur. */
onEditSubmit: PropTypes.func.isRequired,
/** The collection item. */
rowData: PropTypes.object, // eslint-disable-line react/forbid-prop-types
/** The input label. */
label: PropTypes.string,
};