Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LEMS-2868] Answerless ServerItemRendererWithDebugUI #2289

Merged
merged 6 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-moons-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": patch
---

Update ServerItemRendererWithDebugUI to optionally use answerless data for rendering
13 changes: 13 additions & 0 deletions packages/perseus/src/widgets/expression/expression.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ type WrappedKeypadContextProps = {
item: PerseusItem;
customKeypad: boolean;
isMobile?: boolean;
answerless?: boolean;
};

const WrappedKeypadContext = ({
item,
customKeypad,
isMobile = false,
answerless = false,
}: WrappedKeypadContextProps) => {
return (
<TestKeypadContextWrapper>
Expand All @@ -37,6 +39,7 @@ const WrappedKeypadContext = ({
return (
<ServerItemRendererWithDebugUI
item={item}
answerless={answerless}
keypadElement={keypadElement}
// Hardcoding the V2 Keypad to true as the Storybook Args
// were not working.
Expand Down Expand Up @@ -122,6 +125,16 @@ export const ExpressionItem3 = (args: StoryArgs): React.ReactElement => {
);
};

export const AnswerlessExpression = (args: StoryArgs): React.ReactElement => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this used? I don't see it referenced in this PR at all.

Copy link
Contributor Author

@handeyeco handeyeco Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how Storybook works, by exporting the function in a .stories.tsx file it shows up in SB.

Screenshot 2025-03-21 at 9 23 17 AM

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️ I didn't take note this was in a .stories.tsx file!

return (
<WrappedKeypadContext
item={expressionItem3}
customKeypad={false}
answerless
/>
);
};

export default {
title: "Perseus/Widgets/Expression",
argTypes: {customKeypad: {control: "boolean"}},
Expand Down
96 changes: 43 additions & 53 deletions packages/perseus/src/widgets/expression/expression.testdata.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
ItemExtras,
type PerseusExpressionWidgetOptions,
type Version,
type PerseusItem,
type PerseusAnswerArea,
expressionLogic,
} from "@khanacademy/perseus-core";

const createItemJson = (
widgetOptions: PerseusExpressionWidgetOptions,
version: Version,
Copy link
Contributor

@Myranae Myranae Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like changing version to widgetVersion, adding extra keys, and modifying item 3 below is all that was done here. Did you add an item without answers somewhere? I think the big red and green chunks were just moved code, but I might have missed something.

widgetVersion = expressionLogic.version,
): PerseusItem => {
return {
question: {
Expand All @@ -19,7 +19,7 @@ const createItemJson = (
type: "expression",
graded: true,
options: widgetOptions,
version: version,
version: widgetVersion,
},
},
},
Expand All @@ -36,62 +36,55 @@ const createItemJson = (
};

export const expressionItemWithAnswer = (answer: string): PerseusItem => {
return createItemJson(
{
answerForms: [
{
considered: "correct",
form: false,
simplify: false,
value: answer,
},
],
times: false,
buttonSets: ["basic"],
functions: [],
buttonsVisible: "always",
},
{major: 1, minor: 0},
);
};

export const expressionItemWithLabels = createItemJson(
{
answerForms: [],
times: false,
buttonSets: ["basic"],
functions: [],
buttonsVisible: "always",
ariaLabel: "Test aria label",
visibleLabel: "Test visible label",
},
{major: 1, minor: 0},
);

export const expressionItem2: PerseusItem = createItemJson(
{
return createItemJson({
answerForms: [
{
considered: "correct",
form: false,
simplify: false,
value: "123-x",
},
{
considered: "correct",
form: false,
simplify: false,
value: "x-123",
value: answer,
},
],
times: false,
buttonSets: ["basic"],
functions: ["f", "g", "h"],
functions: [],
buttonsVisible: "always",
extraKeys: ["x"],
},
{major: 1, minor: 0},
);
extraKeys: [],
});
};

export const expressionItemWithLabels = createItemJson({
answerForms: [],
times: false,
buttonSets: ["basic"],
functions: [],
buttonsVisible: "always",
ariaLabel: "Test aria label",
visibleLabel: "Test visible label",
extraKeys: [],
});

export const expressionItem2: PerseusItem = createItemJson({
answerForms: [
{
considered: "correct",
form: false,
simplify: false,
value: "123-x",
},
{
considered: "correct",
form: false,
simplify: false,
value: "x-123",
},
],
times: false,
buttonSets: ["basic"],
functions: ["f", "g", "h"],
buttonsVisible: "always",
extraKeys: ["x"],
});

const expressionItem3Options: PerseusExpressionWidgetOptions = {
answerForms: [
Expand Down Expand Up @@ -126,12 +119,9 @@ const expressionItem3Options: PerseusExpressionWidgetOptions = {
buttonsVisible: "focused",
visibleLabel: "number of cm",
ariaLabel: "number of centimeters",
extraKeys: ["z", "a"],
};

export const expressionItem3: PerseusItem = createItemJson(
expressionItem3Options,
{
major: 1,
minor: 0,
},
);
22 changes: 19 additions & 3 deletions testing/server-item-renderer-with-debug-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import {View} from "@khanacademy/wonder-blocks-core";
import {Strut} from "@khanacademy/wonder-blocks-layout";
import * as React from "react";

import {
type PerseusItem,
type KEScore,
type PerseusRenderer,
splitPerseusItem,
} from "@khanacademy/perseus-core";
import {scorePerseusItem} from "@khanacademy/perseus-score";

import * as Perseus from "../packages/perseus/src/index";
Expand All @@ -14,18 +20,19 @@ import {storybookDependenciesV2} from "./test-dependencies";

import type {APIOptions} from "../packages/perseus/src/types";
import type {KeypadAPI} from "@khanacademy/math-input";
import type {PerseusItem, KEScore} from "@khanacademy/perseus-core";

type Props = {
item: PerseusItem;
apiOptions?: APIOptions;
keypadElement?: KeypadAPI | null | undefined;
answerless?: boolean;
};

export const ServerItemRendererWithDebugUI = ({
item,
apiOptions,
keypadElement,
answerless = false,
}: Props): React.ReactElement => {
const ref = React.useRef<Perseus.ServerItemRendererComponent>(null);
const [state, setState] = React.useState<KEScore | null | undefined>(null);
Expand All @@ -51,6 +58,15 @@ export const ServerItemRendererWithDebugUI = ({
);
};

const renderedQuestion: PerseusRenderer = answerless
? splitPerseusItem(item.question)
: item.question;

const renderedItem: PerseusItem = {
...item,
question: renderedQuestion,
};

return (
<SideBySide
rendererTitle="Renderer"
Expand All @@ -60,7 +76,7 @@ export const ServerItemRendererWithDebugUI = ({
ref={ref}
problemNum={0}
apiOptions={options}
item={item}
item={renderedItem}
dependencies={storybookDependenciesV2}
keypadElement={keypadElement}
/>
Expand All @@ -87,7 +103,7 @@ export const ServerItemRendererWithDebugUI = ({
<KEScoreUI score={state} />
</>
}
jsonObject={item}
jsonObject={renderedItem}
/>
);
};
Loading