Skip to content

Commit

Permalink
Move default editor props for NumberLine (#2147)
Browse files Browse the repository at this point in the history
## Summary:
This one is a little tricky because it's called `showTooltip` in the data schema but was called `showTooltips` in the editor. I think it was a bug? So I tried to strengthen the types a little.

Issue: LEMS-2737

## Test plan:
NumberLine should continue to be editable and the widgets the editor makes should be rendered correctly.

Author: handeyeco

Reviewers: handeyeco, jeremywiebe

Required Reviewers:

Approved By: jeremywiebe

Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x)

Pull Request URL: #2147
  • Loading branch information
handeyeco authored Jan 24, 2025
1 parent 8a48960 commit ebf3695
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 43 deletions.
6 changes: 6 additions & 0 deletions .changeset/yellow-baboons-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@khanacademy/perseus-core": minor
"@khanacademy/perseus-editor": patch
---

Move upgrade logic for NumberLine to Perseus Core
2 changes: 1 addition & 1 deletion packages/perseus-core/src/data-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ export type PerseusNumberLineWidgetOptions = {
// This controls the initial position of the point along the number line
initialX: number | null | undefined;
// Show tooltips
showTooltip?: boolean;
showTooltips?: boolean;
// When true, the answer is displayed and is immutable
static: boolean;
};
Expand Down
2 changes: 2 additions & 0 deletions packages/perseus-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export {default as matrixLogic} from "./widgets/matrix";
export type {MatrixDefaultWidgetOptions} from "./widgets/matrix";
export {default as measurerLogic} from "./widgets/measurer";
export type {MeasurerDefaultWidgetOptions} from "./widgets/measurer";
export {default as numberLineLogic} from "./widgets/number-line";
export type {NumberLineDefaultWidgetOptions} from "./widgets/number-line";
export {default as numericInputLogic} from "./widgets/numeric-input";
export type {NumericInputDefaultWidgetOptions} from "./widgets/numeric-input";
export {default as ordererLogic} from "./widgets/orderer";
Expand Down
44 changes: 44 additions & 0 deletions packages/perseus-core/src/widgets/number-line/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type {PerseusNumberLineWidgetOptions} from "../../data-schema";
import type {WidgetLogic} from "../logic-export.types";

export type NumberLineDefaultWidgetOptions = Pick<
PerseusNumberLineWidgetOptions,
| "range"
| "labelRange"
| "labelStyle"
| "labelTicks"
| "divisionRange"
| "numDivisions"
| "snapDivisions"
| "tickStep"
| "correctRel"
| "correctX"
| "initialX"
| "showTooltips"
>;

const defaultWidgetOptions: NumberLineDefaultWidgetOptions = {
range: [0, 10],

labelRange: [null, null],
labelStyle: "decimal",
labelTicks: true,

divisionRange: [1, 12],
numDivisions: 5,
snapDivisions: 2,

tickStep: null,
correctRel: "eq",
correctX: null,
initialX: null,

showTooltips: false,
};

const numberLineWidgetLogic: WidgetLogic = {
name: "number-line",
defaultWidgetOptions,
};

export default numberLineWidgetLogic;
65 changes: 25 additions & 40 deletions packages/perseus-editor/src/widgets/number-line-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/* eslint-disable @khanacademy/ts-no-error-suppressions */
import {number as knumber} from "@khanacademy/kmath";
import {components, EditorJsonify} from "@khanacademy/perseus";
import {
numberLineLogic,
type NumberLineDefaultWidgetOptions,
} from "@khanacademy/perseus-core";
import {Checkbox} from "@khanacademy/wonder-blocks-form";
import PropTypes from "prop-types";
import * as React from "react";
import _ from "underscore";

import type {Changeable} from "@khanacademy/perseus";

const {ButtonGroup, InfoTip, NumberInput, RangeInput} = components;

type Range = [number, number];
Expand All @@ -15,52 +20,32 @@ const bound = (x: number, gt: number, lt: number): number =>

const EN_DASH = "\u2013";

type Props = any;

class NumberLineEditor extends React.Component<Props> {
static propTypes = {
range: PropTypes.arrayOf(PropTypes.number).isRequired,

labelRange: PropTypes.arrayOf(PropTypes.number).isRequired,
labelStyle: PropTypes.string.isRequired,
labelTicks: PropTypes.bool,
type Props = {
range: number[];

divisionRange: PropTypes.arrayOf(PropTypes.number).isRequired,
numDivisions: PropTypes.number.isRequired,
snapDivisions: PropTypes.number,
labelRange: ReadonlyArray<number>;
labelStyle: string;
labelTicks: boolean;

tickStep: PropTypes.number,
correctRel: PropTypes.oneOf(["lt", "gt", "le", "ge", "eq"]),
correctX: PropTypes.number,
initialX: PropTypes.number,
isTickCtrl: PropTypes.bool,
divisionRange: ReadonlyArray<number>;
numDivisions: number;
snapDivisions: number;

onChange: PropTypes.func.isRequired,
tickStep: number;
correctRel: "lt" | "gt" | "le" | "ge" | "eq";
correctX: number;
initialX: number;
isTickCtrl?: boolean;

static: PropTypes.bool,
showTooltips: PropTypes.bool,
};
static?: boolean;
showTooltips: boolean;
} & Changeable.ChangeableProps;

class NumberLineEditor extends React.Component<Props> {
static widgetName = "number-line" as const;

static defaultProps: Props = {
range: [0, 10],

labelRange: [null, null],
labelStyle: "decimal",
labelTicks: true,

divisionRange: [1, 12],
numDivisions: 5,
snapDivisions: 2,

tickStep: null,
correctRel: "eq",
correctX: null,
initialX: null,

showTooltips: false,
};
static defaultProps: NumberLineDefaultWidgetOptions =
numberLineLogic.defaultWidgetOptions;

onRangeChange: (arg1: Range) => void = (range) => {
// Changing the range constrains the initial position, as well as the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const parseNumberLineWidget: Parser<NumberLineWidget> = parseWidget(
correctRel: optional(nullable(string)),
correctX: nullable(number),
initialX: optional(nullable(number)),
showTooltip: optional(boolean),
showTooltips: optional(boolean),
static: defaulted(boolean, () => false),
}),
);
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const question2: PerseusRenderer = {
correctRel: "eq",
correctX: 0.5,
initialX: null,
showTooltip: false,
showTooltips: false,
isTickCtrl: true,
},
},
Expand Down

0 comments on commit ebf3695

Please sign in to comment.