Skip to content

Commit

Permalink
KeypadAPI: use it in more spots (#627)
Browse files Browse the repository at this point in the history
* type things better

* changeset
  • Loading branch information
handeyeco authored Jul 25, 2023
1 parent acafa72 commit 04e68d1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-kids-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/math-input": major
---

Change keypadElement from LegacyKeypad to KeypadAPI
9 changes: 4 additions & 5 deletions packages/math-input/src/components/input/math-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ import {
wonderBlocksBlue,
offBlack,
} from "../common-style";
import LegacyKeypad from "../keypad-legacy";

import CursorHandle from "./cursor-handle";
import DragListener from "./drag-listener";
import MathWrapper from "./math-wrapper";
import {scrollIntoView} from "./scroll-into-view";

import type {Cursor} from "../../types";
import type {Cursor, KeypadAPI} from "../../types";

const constrainingFrictionFactor = 0.8;

type Props = {
keypadElement: LegacyKeypad;
keypadElement?: KeypadAPI;
onBlur: () => void;
onChange: (value: string, callback: any) => void;
onFocus: () => void;
Expand Down Expand Up @@ -267,7 +266,7 @@ class MathInput extends React.Component<Props, State> {
/** Gets and cache they bounds of the keypadElement */
_getKeypadBounds: () => any = () => {
if (!this._keypadBounds) {
const node = this.props.keypadElement.getDOMNode();
const node = this.props.keypadElement?.getDOMNode();
this._cacheKeypadBounds(node);
}
return this._keypadBounds;
Expand Down Expand Up @@ -341,7 +340,7 @@ class MathInput extends React.Component<Props, State> {
focus: () => void = () => {
// Pass this component's handleKey method to the keypad so it can call
// it whenever it needs to trigger a keypress action.
this.props.keypadElement.setKeyHandler((key) => {
this.props.keypadElement?.setKeyHandler((key) => {
const cursor = this.mathField.pressKey(key);

// Trigger an `onChange` if the value in the input changed, and hide
Expand Down
18 changes: 11 additions & 7 deletions packages/math-input/src/full-math-input.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from "react";

import MobileKeypad from "./components/keypad/mobile-keypad";
import {KeypadAPI} from "./types";

import {KeypadInput, KeypadType, LegacyKeypad} from "./index";

Expand All @@ -11,19 +12,22 @@ export default {
export const Basic = () => {
const [value, setValue] = React.useState("");
// Reference to the keypad
const [keypadElement, setKeypadElement] = React.useState<any>(null);
const [keypadElement, setKeypadElement] = React.useState<KeypadAPI>();
// Whether to use Expression or Fraction keypad
const [expression, setExpression] = React.useState<boolean>(true);
// Whether to use v1 or v2 keypad
const [legacyKeypad, setLegacyKeypad] = React.useState<boolean>(false);

React.useEffect(() => {
keypadElement?.configure({
keypadType: expression
? KeypadType.EXPRESSION
: KeypadType.FRACTION,
extraKeys: expression ? ["x", "y", "PI", "THETA"] : [],
});
keypadElement?.configure(
{
keypadType: expression
? KeypadType.EXPRESSION
: KeypadType.FRACTION,
extraKeys: expression ? ["x", "y", "PI", "THETA"] : [],
},
() => {},
);
}, [keypadElement, expression]);

return (
Expand Down

0 comments on commit 04e68d1

Please sign in to comment.