Skip to content

Commit

Permalink
Create helper to build public widget options for Dropdown (#2166)
Browse files Browse the repository at this point in the history
## Summary:
Adds a function that takes Dropdown's full widget options and filters out answer data. It also adds this function to Dropdown's widget export and adds a test confirming the function does what is expected.

Issue: LEMS-2757

## Test plan:
- Confirm all checks pass
- Confirm Dropdown still works as expected

Author: Myranae

Reviewers: benchristel, jeremywiebe, handeyeco

Required Reviewers:

Approved By: benchristel

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

Pull Request URL: #2166
  • Loading branch information
Myranae authored Jan 30, 2025
1 parent a21fd90 commit 3c4c6bc
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/twelve-spies-lie.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 Dropdown 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 @@ -117,3 +117,4 @@ export {default as getCategorizerPublicWidgetOptions} from "./widgets/categorize
export {default as getExpressionPublicWidgetOptions} from "./widgets/expression/expression-util";
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";
32 changes: 32 additions & 0 deletions packages/perseus-core/src/widgets/dropdown/dropdown-util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import getDropdownPublicWidgetOptions from "./dropdown-util";

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

describe("getDropdownPublicWidgetOptions", () => {
it("should return the correct public options without any answer data", () => {
// Arrange
const options: PerseusDropdownWidgetOptions = {
choices: [
{content: "1", correct: false},
{content: "2", correct: false},
{content: "3", correct: true},
],
placeholder: "Select an option",
static: false,
visibleLabel: "Test Label",
ariaLabel: "Test Aria Label",
};

// Act
const publicWidgetOptions = getDropdownPublicWidgetOptions(options);

// Assert
expect(publicWidgetOptions).toEqual({
choices: [{content: "1"}, {content: "2"}, {content: "3"}],
placeholder: "Select an option",
static: false,
visibleLabel: "Test Label",
ariaLabel: "Test Aria Label",
});
});
});
31 changes: 31 additions & 0 deletions packages/perseus-core/src/widgets/dropdown/dropdown-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type {PerseusDropdownWidgetOptions} from "@khanacademy/perseus-core";

/**
* For details on the individual options, see the
* PerseusDropdownWidgetOptions type
*/
type DropdownPublicWidgetOptions = {
choices: ReadonlyArray<{content: string}>;
placeholder: PerseusDropdownWidgetOptions["placeholder"];
static: PerseusDropdownWidgetOptions["static"];
visibleLabel?: PerseusDropdownWidgetOptions["visibleLabel"];
ariaLabel?: PerseusDropdownWidgetOptions["ariaLabel"];
};

/**
* Given a PerseusDropdownWidgetOptions object, return a new object with only
* the public options that should be exposed to the client.
*/
function getDropdownPublicWidgetOptions(
options: PerseusDropdownWidgetOptions,
): DropdownPublicWidgetOptions {
return {
choices: options.choices.map((choice) => ({content: choice.content})),
placeholder: options.placeholder,
static: options.static,
visibleLabel: options.visibleLabel,
ariaLabel: options.ariaLabel,
};
}

export default getDropdownPublicWidgetOptions;
2 changes: 2 additions & 0 deletions packages/perseus/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
getCategorizerPublicWidgetOptions,
getExpressionPublicWidgetOptions,
getSorterPublicWidgetOptions,
getDropdownPublicWidgetOptions,
} from "@khanacademy/perseus-core";
import type {LinterContextProps} from "@khanacademy/perseus-linter";
import type {
Expand Down Expand Up @@ -543,6 +544,7 @@ export type WidgetScorerFunction = (
* A union type of all the functions that provide public widget options.
*/
export type PublicWidgetOptionsFunction =
| typeof getDropdownPublicWidgetOptions
| typeof getCategorizerPublicWidgetOptions
| typeof getOrdererPublicWidgetOptions
| typeof getExpressionPublicWidgetOptions
Expand Down
2 changes: 2 additions & 0 deletions packages/perseus/src/widgets/dropdown/dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {getDropdownPublicWidgetOptions} from "@khanacademy/perseus-core";
import {scoreDropdown, validateDropdown} from "@khanacademy/perseus-score";
import {Id, View} from "@khanacademy/wonder-blocks-core";
import {SingleSelect, OptionItem} from "@khanacademy/wonder-blocks-dropdown";
Expand Down Expand Up @@ -176,4 +177,5 @@ export default {
// TODO(LEMS-2656): remove TS suppression
// @ts-expect-error: Type 'UserInput' is not assignable to type 'PerseusDropdownUserInput'.
validator: validateDropdown,
getPublicWidgetOptions: getDropdownPublicWidgetOptions,
} satisfies WidgetExports<typeof Dropdown>;

0 comments on commit 3c4c6bc

Please sign in to comment.