Skip to content

Commit

Permalink
Create helper to build public widget options for Numeric Input (#2174)
Browse files Browse the repository at this point in the history
## Summary:
Adds a function that takes a Numeric Input widget's full widget options and removes correct answer information. It also adds the function to the widget's widget export and adds a test confirming the function works as expected.

Issue: LEMS-2767

## Test plan:
- Confirm all checks pass
- Confirm the Numeric Input widget still acts as expected

Author: Myranae

Reviewers: Myranae, benchristel, handeyeco, SonicScrewdriver, jeremywiebe

Required Reviewers:

Approved By: benchristel

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

Pull Request URL: #2174
  • Loading branch information
Myranae authored Jan 31, 2025
1 parent 3be9d2c commit 97e07c8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/good-cheetahs-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@khanacademy/perseus": minor
"@khanacademy/perseus-core": minor
---

Implement a widget export function to filter out rubric data from widget options for the Numeric Input widget
1 change: 1 addition & 0 deletions packages/perseus-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@ export {default as getExpressionPublicWidgetOptions} from "./widgets/expression/
export {default as getLabelImagePublicWidgetOptions} from "./widgets/label-image/label-image-util";
export {default as getSorterPublicWidgetOptions} from "./widgets/sorter/sorter-util";
export {default as getDropdownPublicWidgetOptions} from "./widgets/dropdown/dropdown-util";
export {default as getNumericInputPublicWidgetOptions} from "./widgets/numeric-input/numeric-input-util";
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import getNumericInputPublicWidgetOptions from "./numeric-input-util";

import type {PerseusNumericInputWidgetOptions} from "../../data-schema";

describe("getNumericInputPublicWidgetOptions", () => {
it("should return the correct public options without any answer data", () => {
// Arrange
const options: PerseusNumericInputWidgetOptions = {
answers: [
{
status: "correct",
maxError: null,
strict: false,
value: 1252,
simplify: "required",
message: "",
},
],
labelText: "labelText",
size: "Normal",
coefficient: false,
rightAlign: false,
static: false,
answerForms: [{simplify: "required", name: "decimal"}],
};

// Act
const publicWidgetOptions = getNumericInputPublicWidgetOptions(options);

// Assert
expect(publicWidgetOptions).toEqual({
labelText: "labelText",
size: "Normal",
coefficient: false,
rightAlign: false,
static: false,
answerForms: [{simplify: "required", name: "decimal"}],
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type {PerseusNumericInputWidgetOptions} from "@khanacademy/perseus-core";

/**
* For details on the individual options, see the
* PerseusNumericInputWidgetOptions type
*/
type NumericInputPublicWidgetOptions = {
labelText?: PerseusNumericInputWidgetOptions["labelText"];
size: PerseusNumericInputWidgetOptions["size"];
coefficient: PerseusNumericInputWidgetOptions["coefficient"];
rightAlign?: PerseusNumericInputWidgetOptions["rightAlign"];
static: PerseusNumericInputWidgetOptions["static"];
answerForms?: PerseusNumericInputWidgetOptions["answerForms"];
};

/**
* Given a PerseusNumericInputWidgetOptions object, return a new object with only
* the public options that should be exposed to the client.
*/
function getNumericInputPublicWidgetOptions(
options: PerseusNumericInputWidgetOptions,
): NumericInputPublicWidgetOptions {
const {answers: _, ...publicWidgetOptions} = options;
return publicWidgetOptions;
}

export default getNumericInputPublicWidgetOptions;
2 changes: 2 additions & 0 deletions packages/perseus/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
getExpressionPublicWidgetOptions,
getSorterPublicWidgetOptions,
getDropdownPublicWidgetOptions,
getNumericInputPublicWidgetOptions,
} from "@khanacademy/perseus-core";
import type {LinterContextProps} from "@khanacademy/perseus-linter";
import type {
Expand Down Expand Up @@ -544,6 +545,7 @@ export type WidgetScorerFunction = (
* A union type of all the functions that provide public widget options.
*/
export type PublicWidgetOptionsFunction =
| typeof getNumericInputPublicWidgetOptions
| typeof getDropdownPublicWidgetOptions
| typeof getCategorizerPublicWidgetOptions
| typeof getOrdererPublicWidgetOptions
Expand Down
3 changes: 2 additions & 1 deletion packages/perseus/src/widgets/numeric-input/numeric-input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {KhanMath} from "@khanacademy/kmath";
import {getNumericInputPublicWidgetOptions} from "@khanacademy/perseus-core";
import {linterContextDefault} from "@khanacademy/perseus-linter";
import {scoreNumericInput} from "@khanacademy/perseus-score";
import {StyleSheet} from "aphrodite";
Expand Down Expand Up @@ -379,7 +380,7 @@ export default {
// TODO(LEMS-2656): remove TS suppression
// @ts-expect-error: Type 'UserInput' is not assignable to type 'PerseusNumericInputUserInput'.
scorer: scoreNumericInput,

getPublicWidgetOptions: getNumericInputPublicWidgetOptions,
// TODO(LEMS-2656): remove TS suppression
// @ts-expect-error: Type 'Rubric' is not assignable to type 'PerseusNumericInputRubric'
getOneCorrectAnswerFromRubric(
Expand Down

0 comments on commit 97e07c8

Please sign in to comment.