Skip to content

Commit a8c5b6c

Browse files
jiayushemartin-henz
authored andcommitted
Use onChange instead of ref for forms (#535)
1 parent 0d0eb40 commit a8c5b6c

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/components/workspace/ControlBar.tsx

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ interface IExternal {
7777
symbols: string[];
7878
}
7979

80-
class ControlBar extends React.PureComponent<ControlBarProps, {}> {
80+
class ControlBar extends React.PureComponent<ControlBarProps, { value: string }> {
8181
public static defaultProps: Partial<ControlBarProps> = {
8282
hasChapterSelect: false,
8383
hasSaveButton: false,
@@ -89,15 +89,15 @@ class ControlBar extends React.PureComponent<ControlBarProps, {}> {
8989
};
9090

9191
private inviteInputElem: React.RefObject<HTMLInputElement>;
92-
private joinInputElem: React.RefObject<HTMLInputElement>;
9392
private shareInputElem: React.RefObject<HTMLInputElement>;
9493

9594
constructor(props: ControlBarProps) {
9695
super(props);
96+
this.state = { value: '' };
97+
this.handleChange = this.handleChange.bind(this);
9798
this.selectShareInputText = this.selectShareInputText.bind(this);
9899
this.selectInviteInputText = this.selectInviteInputText.bind(this);
99100
this.inviteInputElem = React.createRef();
100-
this.joinInputElem = React.createRef();
101101
this.shareInputElem = React.createRef();
102102
}
103103

@@ -167,15 +167,15 @@ class ControlBar extends React.PureComponent<ControlBarProps, {}> {
167167
xmlhttp.send();
168168
}
169169
};
170-
const handleStartJoining = (e: React.FormEvent<HTMLFormElement>) => {
170+
const handleStartJoining = (event: React.FormEvent<HTMLFormElement>) => {
171171
const xmlhttp = new XMLHttpRequest();
172172
xmlhttp.onreadystatechange = () => {
173173
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
174174
// Successfully reached server to verify ID
175175
const state = JSON.parse(xmlhttp.responseText).state;
176176
if (state === true) {
177177
// Session ID exists
178-
this.props.handleSetEditorSessionId!(this.joinInputElem.current!.value);
178+
this.props.handleSetEditorSessionId!(this.state!.value);
179179
} else {
180180
this.props.handleInvalidEditorSessionId!();
181181
this.props.handleSetEditorSessionId!('');
@@ -185,14 +185,9 @@ class ControlBar extends React.PureComponent<ControlBarProps, {}> {
185185
this.props.handleSetEditorSessionId!('');
186186
}
187187
};
188-
xmlhttp.open(
189-
'GET',
190-
'https://' + LINKS.SHAREDB_SERVER + 'gists/' + this.joinInputElem.current!.value,
191-
true
192-
);
188+
xmlhttp.open('GET', 'https://' + LINKS.SHAREDB_SERVER + 'gists/' + this.state!.value, true);
193189
xmlhttp.send();
194-
195-
e.preventDefault();
190+
event.preventDefault();
196191
};
197192
const inviteButton = this.props.hasCollabEditing ? (
198193
<Popover popoverClassName="Popover-share" inheritDarkTheme={false}>
@@ -212,7 +207,7 @@ class ControlBar extends React.PureComponent<ControlBarProps, {}> {
212207
{controlButton('Join', IconNames.LOG_IN)}
213208
<>
214209
<form onSubmit={handleStartJoining}>
215-
<input defaultValue="" ref={this.joinInputElem} />
210+
<input type="text" value={this.state.value} onChange={this.handleChange} />
216211
<span className={Classes.POPOVER_DISMISS}>
217212
{controlButton('', IconNames.KEY_ENTER, null, { type: 'submit' })}
218213
</span>
@@ -223,9 +218,17 @@ class ControlBar extends React.PureComponent<ControlBarProps, {}> {
223218
undefined
224219
);
225220
const leaveButton = this.props.hasCollabEditing
226-
? controlButton('Leave', IconNames.FEED, () => this.props.handleSetEditorSessionId!(''), {
227-
iconColor: this.props.websocketStatus === 0 ? Colors.RED3 : Colors.GREEN3
228-
})
221+
? controlButton(
222+
'Leave',
223+
IconNames.FEED,
224+
() => {
225+
this.props.handleSetEditorSessionId!('');
226+
this.setState({ value: '' });
227+
},
228+
{
229+
iconColor: this.props.websocketStatus === 0 ? Colors.RED3 : Colors.GREEN3
230+
}
231+
)
229232
: undefined;
230233
const chapterSelectButton = this.props.hasChapterSelect
231234
? chapterSelect(this.props.sourceChapter, this.props.handleChapterSelect)
@@ -315,6 +318,10 @@ class ControlBar extends React.PureComponent<ControlBarProps, {}> {
315318
);
316319
}
317320

321+
private handleChange(event: React.ChangeEvent<HTMLInputElement>) {
322+
this.setState({ value: event.target.value });
323+
}
324+
318325
private selectShareInputText() {
319326
if (this.shareInputElem.current !== null) {
320327
this.shareInputElem.current.focus();

0 commit comments

Comments
 (0)