/
CommentFooter.tsx
238 lines (225 loc) · 4.94 KB
/
CommentFooter.tsx
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import * as React from 'react';
/**
* React Props interface
*/
interface ICommentFooterProps {
/**
* Tracks if card is expanded
*
* @type boolean
*/
expanded: boolean;
/**
* Is the card resolved
*
* @type boolean
*/
resolved: boolean;
/**
* Tracks if the reply box is active
*
* @type return function
*/
replyActive: boolean;
/**
* Function to call to parent component to open the reply box
*
* @type VoidFunction
*/
handleReplyOpen: VoidFunction;
/**
* Function to call to parent component to close the reply box
*
* @type VoidFunction
*/
handleReplyClose: VoidFunction;
/**
* Function to call to parent component to handle expanding and opening the reply box
*
* @type VoidFunction
*/
expandAndReply: VoidFunction;
/**
* Passes comment message to putComment in App.tsx
*
* @param comment Type: string - comment message
*
* @type void function
*/
getInput: (comment: string) => void;
/**
* Reverses resolve state
*
* @type: void function
*/
handleResolve: VoidFunction;
}
/**
* React States interface
*/
interface ICommentFooterStates {
commentBox: string;
}
/**
* CommentFooter React Component
*/
export class CommentFooter extends React.Component<
ICommentFooterProps,
ICommentFooterStates
> {
/**
* Constructor
*
* @param props React props
*/
constructor(props: ICommentFooterProps) {
super(props);
this.state = {
commentBox: ''
};
this.handleChangeCommentBox = this.handleChangeCommentBox.bind(this);
this.handleCommentButton = this.handleCommentButton.bind(this);
this.handleCancelButton = this.handleCancelButton.bind(this);
this.handleKeyPress = this.handleKeyPress.bind(this);
}
/**
* React render function
*/
render() {
return (
<div style={this.styles.footerArea}>
<div
style={
this.props.replyActive
? this.styles.inputBoxAreaActive
: this.styles.inputBoxAreaNotActive
}
>
{this.props.expanded && (
<textarea
className="--jp-commenting-text-area"
id={'commentBox'}
value={
this.state.commentBox.trim() === ''
? this.state.commentBox.trim()
: this.state.commentBox
}
onChange={this.handleChangeCommentBox}
onKeyPress={this.handleKeyPress}
onFocusCapture={this.props.handleReplyOpen}
placeholder="Reply..."
/>
)}
</div>
<div style={this.styles.buttonArea}>{this.getButtons()}</div>
</div>
);
}
/**
* Returns the correct buttons for different state combinations
*
* @return Type: React.ReactNode - JSX with buttons
*/
getButtons(): React.ReactNode {
if (this.props.replyActive) {
return (
<div>
{this.getCommentButton()}
{this.getCancelButton()}
</div>
);
}
}
/**
* Creates and returns reply button
*
* @return Type: React.ReactNode
*/
getCommentButton(): React.ReactNode {
return (
<button
onClick={this.handleCommentButton}
className="--jp-commenting-button-blue"
type="button"
disabled={this.state.commentBox.trim() === ''}
>
Comment
</button>
);
}
/**
* Creates and returns cancel button
*
* @return Type: React.ReactNode
*/
getCancelButton(): React.ReactNode {
return (
<button
onClick={this.handleCancelButton}
className="--jp-commenting-button-red"
type="button"
>
Cancel
</button>
);
}
/**
* Handles key events
*
* @param e Type: ? - keyboard event
*/
handleKeyPress(e: any): void {
if (
this.state.commentBox.trim() !== '' &&
e.key === 'Enter' &&
!e.shiftKey
) {
this.handleCommentButton();
document.getElementById('commentBox').blur();
}
}
// TODO: Get correct type
/**
* Handles when the comment box changes
*
* @param e Type: any - input box event
*/
handleChangeCommentBox(e: any): void {
this.setState({ commentBox: e.target.value });
}
/**
* Handles clicking the comment button
*/
handleCommentButton(): void {
this.props.getInput(this.state.commentBox);
this.setState({ commentBox: '' });
this.props.handleReplyClose();
}
handleCancelButton(): void {
this.setState({ commentBox: '' });
this.props.handleReplyClose();
}
/**
* CSS styles
*/
styles = {
footerArea: {
display: 'flex',
flexDirection: 'column' as 'column',
maxHeight: '94px',
padding: '4px'
},
buttonArea: {
display: 'flex',
marginTop: '16px'
},
inputBoxAreaActive: {
display: 'flex',
height: '60px'
},
inputBoxAreaNotActive: {
display: 'flex',
height: '14px'
}
};
}