Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove findDOMNode from text-input #1919

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions packages/perseus/src/components/text-input.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @khanacademy/ts-no-error-suppressions */
import {Errors, PerseusError} from "@khanacademy/perseus-core";
import {TextField} from "@khanacademy/wonder-blocks-form";
import * as React from "react";
import ReactDOM from "react-dom";

import type {StyleType} from "@khanacademy/wonder-blocks-core";

Expand Down Expand Up @@ -31,6 +31,7 @@ function uniqueIdForInput(prefix = "input-") {
}

class TextInput extends React.Component<Props> {
inputRef = React.createRef<HTMLInputElement>();
static defaultProps: DefaultProps = {
value: "",
disabled: false,
Expand All @@ -47,45 +48,46 @@ class TextInput extends React.Component<Props> {
}
}

_getInput: () => HTMLInputElement = () => {
if (!this.inputRef.current) {
throw new PerseusError(
"Input ref accessed before set",
Errors.Internal,
);
}

return this.inputRef.current;
};

focus: () => void = () => {
// @ts-expect-error - TS2531 - Object is possibly 'null'. | TS2339 - Property 'focus' does not exist on type 'Element | Text'.
ReactDOM.findDOMNode(this).focus();
this._getInput().focus();
};

blur: () => void = () => {
// @ts-expect-error - TS2531 - Object is possibly 'null'. | TS2339 - Property 'blur' does not exist on type 'Element | Text'.
ReactDOM.findDOMNode(this).blur();
this._getInput().blur();
};

getValue: () => string | null | undefined = () => {
// @ts-expect-error - TS2339 - Property 'value' does not exist on type 'Element | Text'.
return ReactDOM.findDOMNode(this)?.value;
return this.inputRef.current?.value;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't use _getInput here because the original code is actually checking to see if the node exists before trying to access it.

};

getStringValue: () => string | null | undefined = () => {
// @ts-expect-error - TS2339 - Property 'value' does not exist on type 'Element | Text'.
return ReactDOM.findDOMNode(this)?.value.toString();
return this.inputRef.current?.value.toString();
};

setSelectionRange: (arg1: number, arg2: number) => void = (
selectionStart,
selectionEnd,
) => {
// @ts-expect-error - TS2531 - Object is possibly 'null'. | TS2339 - Property 'setSelectionRange' does not exist on type 'Element | Text'.
ReactDOM.findDOMNode(this).setSelectionRange(
selectionStart,
selectionEnd,
);
this._getInput().setSelectionRange(selectionStart, selectionEnd);
};

getSelectionStart: () => number = () => {
// @ts-expect-error - TS2531 - Object is possibly 'null'. | TS2339 - Property 'selectionStart' does not exist on type 'Element | Text'.
return ReactDOM.findDOMNode(this).selectionStart;
getSelectionStart: () => number | null = () => {
return this._getInput().selectionStart;
};

getSelectionEnd: () => number = () => {
// @ts-expect-error - TS2531 - Object is possibly 'null'. | TS2339 - Property 'selectionEnd' does not exist on type 'Element | Text'.
return ReactDOM.findDOMNode(this).selectionEnd;
getSelectionEnd: () => number | null = () => {
return this._getInput().selectionEnd;
};

render(): React.ReactNode {
Expand All @@ -104,6 +106,7 @@ class TextInput extends React.Component<Props> {

return (
<TextField
ref={this.inputRef}
style={style}
disabled={disabled}
id={this.id}
Expand Down
Loading