Skip to content

Commit ed660cc

Browse files
author
Akos Kitta
committed
fixed the input focus when the view is activated
Signed-off-by: Akos Kitta <kittaakos@typefox.io>
1 parent 6af22ec commit ed660cc

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

arduino-ide-extension/src/browser/monitor/monitor-widget.tsx

+21-11
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ export class MonitorWidget extends ReactWidget {
8888
this.update();
8989
}
9090

91+
protected onActivateRequest(msg: Message): void {
92+
super.onActivateRequest(msg);
93+
(this.focusNode || this.node).focus();
94+
}
95+
96+
protected onFocusResolved = (element: HTMLElement | undefined) => {
97+
this.focusNode = element;
98+
}
99+
91100
protected get lineEndings(): OptionsType<SelectOption<MonitorModel.EOL>> {
92101
return [
93102
{
@@ -121,7 +130,9 @@ export class MonitorWidget extends ReactWidget {
121130
return <div className='serial-monitor-container'>
122131
<div className='head'>
123132
<div className='send'>
124-
<SerialMonitorSendField onSend={this.onSend} />
133+
<SerialMonitorSendField
134+
resolveFocus={this.onFocusResolved}
135+
onSend={this.onSend} />
125136
</div>
126137
<div className='config'>
127138
<ArduinoSelect
@@ -162,7 +173,8 @@ export class MonitorWidget extends ReactWidget {
162173

163174
export namespace SerialMonitorSendField {
164175
export interface Props {
165-
readonly onSend: (text: string) => void
176+
readonly onSend: (text: string) => void;
177+
readonly resolveFocus: (element: HTMLElement | undefined) => void;
166178
}
167179
export interface State {
168180
value: string;
@@ -171,8 +183,6 @@ export namespace SerialMonitorSendField {
171183

172184
export class SerialMonitorSendField extends React.Component<SerialMonitorSendField.Props, SerialMonitorSendField.State> {
173185

174-
protected inputField: HTMLInputElement | null;
175-
176186
constructor(props: SerialMonitorSendField.Props) {
177187
super(props);
178188
this.state = { value: '' };
@@ -181,17 +191,11 @@ export class SerialMonitorSendField extends React.Component<SerialMonitorSendFie
181191
this.handleSubmit = this.handleSubmit.bind(this);
182192
}
183193

184-
componentDidMount() {
185-
if (this.inputField) {
186-
this.inputField.focus();
187-
}
188-
}
189-
190194
render() {
191195
return <React.Fragment>
192196
<input
193197
tabIndex={-1}
194-
ref={ref => this.inputField = ref}
198+
ref={this.setRef}
195199
id='serial-monitor-send'
196200
type='text'
197201
autoComplete='off'
@@ -201,6 +205,12 @@ export class SerialMonitorSendField extends React.Component<SerialMonitorSendFie
201205
</React.Fragment>
202206
}
203207

208+
protected setRef = (element: HTMLElement | null) => {
209+
if (this.props.resolveFocus) {
210+
this.props.resolveFocus(element || undefined);
211+
}
212+
}
213+
204214
protected handleChange(event: React.ChangeEvent<HTMLInputElement>) {
205215
this.setState({ value: event.target.value });
206216
}

0 commit comments

Comments
 (0)