diff --git a/.changeset/big-plums-confess.md b/.changeset/big-plums-confess.md new file mode 100644 index 0000000000..1f69d99f2f --- /dev/null +++ b/.changeset/big-plums-confess.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/perseus-editor": patch +--- + +HintEditor: migrate to React refs diff --git a/packages/perseus-editor/src/hint-editor.tsx b/packages/perseus-editor/src/hint-editor.tsx index 31af0da3eb..77db5e398e 100644 --- a/packages/perseus-editor/src/hint-editor.tsx +++ b/packages/perseus-editor/src/hint-editor.tsx @@ -301,6 +301,7 @@ type CombinedHintsEditorProps = { */ class CombinedHintsEditor extends React.Component { static HintEditor: typeof HintEditor = HintEditor; + hintEditors: Record = {}; static defaultProps: { highlightLint: boolean; @@ -353,9 +354,7 @@ class CombinedHintsEditor extends React.Component { const hint = hints.splice(i, 1)[0]; hints.splice(i + dir, 0, hint); this.props.onChange({hints: hints}, () => { - // eslint-disable-next-line react/no-string-refs - // @ts-expect-error - TS2339 - Property 'focus' does not exist on type 'ReactInstance'. - this.refs["hintEditor" + (i + dir)].focus(); + this.hintEditors[i + dir]?.focus(); }); }; @@ -365,42 +364,29 @@ class CombinedHintsEditor extends React.Component { ]); this.props.onChange({hints: hints}, () => { const i = hints.length - 1; - // eslint-disable-next-line react/no-string-refs - // @ts-expect-error - TS2339 - Property 'focus' does not exist on type 'ReactInstance'. - this.refs["hintEditor" + i].focus(); + this.hintEditors[i]?.focus(); }); }; getSaveWarnings: () => any = () => { - return _.chain(this.props.hints) + return this.props.hints .map((hint, i) => { - return _.map( - // eslint-disable-next-line react/no-string-refs - // @ts-expect-error - TS2339 - Property 'getSaveWarnings' does not exist on type 'ReactInstance'. - this.refs["hintEditor" + i].getSaveWarnings(), - (issue) => "Hint " + (i + 1) + ": " + issue, - ); + return this.hintEditors[i] + ?.getSaveWarnings() + .map((issue) => "Hint " + (i + 1) + ": " + issue); }) - .flatten(true) - .value(); + .flat(); }; - serialize: (options?: any) => ReadonlyArray = ( - options: any, - ) => { + serialize(options?: any): ReadonlyArray { return this.props.hints.map((hint, i) => { return this.serializeHint(i, options); }); - }; + } - serializeHint: (index: number, options?: any) => PerseusRenderer = ( - index, - options, - ) => { - // eslint-disable-next-line react/no-string-refs - // @ts-expect-error - TS2339 - Property 'serialize' does not exist on type 'ReactInstance'. - return this.refs["hintEditor" + index].serialize(options); - }; + serializeHint(index: number, options?: any): PerseusRenderer { + return this.hintEditors[index]!.serialize(options)!; + } render(): React.ReactNode { const {itemId, hints} = this.props; @@ -409,7 +395,9 @@ class CombinedHintsEditor extends React.Component { (hint, i) => { return ( { + this.hintEditors[i] = editor; + }} key={"hintEditor" + i} isFirst={i === 0} isLast={i + 1 === hints.length}