Skip to content

Commit

Permalink
[benc/matrix] Create a function to get the public options for a Matri…
Browse files Browse the repository at this point in the history
…x widget
  • Loading branch information
benchristel committed Feb 7, 2025
1 parent 649e6b1 commit 3e8e93d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/perseus-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,4 @@ export {default as getNumberLinePublicWidgetOptions} from "./widgets/number-line
export {default as getRadioPublicWidgetOptions} from "./widgets/radio/radio-util";
export {default as getTablePublicWidgetOptions} from "./widgets/table/table-util";
export {default as getIFramePublicWidgetOptions} from "./widgets/iframe/iframe-util";
export {default as getMatrixPublicWidgetOptions} from "./widgets/matrix/matrix-util";
27 changes: 27 additions & 0 deletions packages/perseus-core/src/widgets/matrix/matrix-util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import getMatrixPublicWidgetOptions from "./matrix-util";

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

describe("getMatrixPublicWidgetOptions", () => {
it("removes the `answers` field", () => {
const options: PerseusMatrixWidgetOptions = {
answers: [
[1, 2],
[3, 4],
],
cursorPosition: [0, 0],
matrixBoardSize: [2, 2],
prefix: "the prefix",
suffix: "the suffix",
};

const publicOptions = getMatrixPublicWidgetOptions(options);

expect(publicOptions).toEqual({
cursorPosition: [0, 0],
matrixBoardSize: [2, 2],
prefix: "the prefix",
suffix: "the suffix",
});
});
});
8 changes: 8 additions & 0 deletions packages/perseus-core/src/widgets/matrix/matrix-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type {PerseusMatrixWidgetOptions} from "@khanacademy/perseus-core";

export default function getMatrixPublicWidgetOptions(
options: PerseusMatrixWidgetOptions,
) {
const {answers: _, ...publicOptions} = options;
return publicOptions;
}
4 changes: 3 additions & 1 deletion packages/perseus/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type {
getRadioPublicWidgetOptions,
getTablePublicWidgetOptions,
getIFramePublicWidgetOptions,
getMatrixPublicWidgetOptions,
} from "@khanacademy/perseus-core";
import type {LinterContextProps} from "@khanacademy/perseus-linter";
import type {
Expand Down Expand Up @@ -504,7 +505,8 @@ export type PublicWidgetOptionsFunction =
| typeof getCSProgramPublicWidgetOptions
| typeof getNumberLinePublicWidgetOptions
| typeof getTablePublicWidgetOptions
| typeof getGrapherPublicWidgetOptions;
| typeof getGrapherPublicWidgetOptions
| typeof getMatrixPublicWidgetOptions;

export type WidgetExports<
T extends React.ComponentType<any> & Widget = React.ComponentType<any>,
Expand Down
2 changes: 2 additions & 0 deletions packages/perseus/src/widgets/matrix/matrix.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
getMatrixPublicWidgetOptions,
getMatrixSize,
type PerseusMatrixWidgetAnswers,
type PerseusMatrixWidgetOptions,
Expand Down Expand Up @@ -572,4 +573,5 @@ export default {
transform: propTransform,
staticTransform: staticTransform,
isLintable: true,
getPublicWidgetOptions: getMatrixPublicWidgetOptions,
} satisfies WidgetExports<typeof Matrix>;

0 comments on commit 3e8e93d

Please sign in to comment.