Skip to content

Commit 7688fdc

Browse files
committed
add listener to focus input on windows focus
1 parent b4f8d53 commit 7688fdc

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/features/common/components/code-editor/editor.component.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Props = React.HTMLAttributes<HTMLDivElement> & {
1818
textareaId?: string;
1919
textareaClassName?: string;
2020
autoFocus?: boolean;
21+
focusOnWindowFocus?: boolean;
2122
disabled?: boolean;
2223
form?: string;
2324
maxLength?: number;
@@ -87,6 +88,15 @@ export class EditorComponent extends React.Component<Props, State> {
8788

8889
componentDidMount() {
8990
this._recordCurrentState();
91+
if (this.props.focusOnWindowFocus) {
92+
window.addEventListener("focus", this._focusInput);
93+
}
94+
}
95+
96+
componentWillUnmount() {
97+
if (this.props.focusOnWindowFocus) {
98+
window.removeEventListener("focus", this._focusInput);
99+
}
90100
}
91101

92102
private _recordCurrentState = () => {
@@ -161,6 +171,13 @@ export class EditorComponent extends React.Component<Props, State> {
161171
this._history.offset++;
162172
};
163173

174+
private _focusInput = () => {
175+
const input = this._input;
176+
177+
if (!input) return;
178+
input.focus();
179+
};
180+
164181
private _updateInput = (record: Record) => {
165182
const input = this._input;
166183

@@ -466,7 +483,7 @@ export class EditorComponent extends React.Component<Props, State> {
466483
selectionStart,
467484
selectionEnd,
468485
},
469-
true,
486+
true
470487
);
471488

472489
this.props.onValueChange(value);
@@ -516,6 +533,7 @@ export class EditorComponent extends React.Component<Props, State> {
516533
insertSpaces,
517534
ignoreTabKey,
518535
preClassName,
536+
focusOnWindowFocus,
519537
...rest
520538
} = this.props;
521539

0 commit comments

Comments
 (0)