Skip to content

Commit a2d2d97

Browse files
authored
Remove aria attributes in order to read card body content (#90)
* remove aria attributes in order to read card body content * conditional aria attributes for cardbody * previewMode in storybook * missing previewMode prop
1 parent 17a7f9b commit a2d2d97

File tree

10 files changed

+258
-118
lines changed

10 files changed

+258
-118
lines changed

src/components/AnswersTable.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ export interface AnswersTableProps {
2323
onKeyPress?: () => void;
2424
contentRenderer?: JSX.Element;
2525
instructions?: JSX.Element;
26+
previewMode?: boolean;
2627
}
2728

2829
export const AnswersTable = (props: AnswersTableProps) => {
2930
let idCounter = 0;
3031

3132
const {
3233
question, hideAnswers, type = defaultAnswerType, answered_count, choicesEnabled, correct_answer_id,
33-
incorrectAnswerId, answer_id, feedback_html, correct_answer_feedback_html,
34+
incorrectAnswerId, answer_id, feedback_html, correct_answer_feedback_html, previewMode,
3435
show_all_feedback = false, tableFeedbackEnabled, hasCorrectAnswer, onChangeAnswer, onKeyPress, answerIdOrder, instructions
3536
} = props;
3637
if (hideAnswers) { return null; }
@@ -53,7 +54,7 @@ export const AnswersTable = (props: AnswersTableProps) => {
5354
onChangeAnswer: onChangeAnswer,
5455
type,
5556
answered_count,
56-
disabled: !choicesEnabled,
57+
disabled: previewMode || !choicesEnabled,
5758
show_all_feedback,
5859
tableFeedbackEnabled,
5960
onKeyPress
@@ -64,10 +65,10 @@ export const AnswersTable = (props: AnswersTableProps) => {
6465
const answersHtml = answers.map((answer, i) => {
6566
const additionalProps: { answer: AnswerType, iter: number, key: string }
6667
= {
67-
answer: {
68-
...answer,
69-
question_id: typeof question.id === 'string' ? parseInt(question.id, 10) : question.id
70-
},
68+
answer: {
69+
...answer,
70+
question_id: typeof question.id === 'string' ? parseInt(question.id, 10) : question.id
71+
},
7172
iter: i,
7273
key: `${questionAnswerProps.qid}-option-${i}`,
7374
};
@@ -103,7 +104,17 @@ export const AnswersTable = (props: AnswersTableProps) => {
103104
});
104105

105106
return (
106-
<div role="radiogroup" aria-label="Answer choices" className="answers-table">
107+
<div
108+
{
109+
...(!previewMode
110+
? {
111+
role: "radiogroup",
112+
'aria-label': "Answer choices"
113+
}
114+
: {})
115+
}
116+
className="answers-table"
117+
>
107118
{instructions}
108119
{answersHtml}
109120
</div>

src/components/Exercise.spec.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,5 +376,12 @@ describe('Exercise', () => {
376376
).toJSON();
377377
expect(tree).toMatchSnapshot();
378378
});
379+
380+
it('matches snapshot with previewMode', () => {
381+
const tree = renderer.create(
382+
<Exercise {...props} show_all_feedback previewMode />
383+
).toJSON();
384+
expect(tree).toMatchSnapshot();
385+
});
379386
});
380387
});

src/components/Exercise.stories.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ export const Default = () => {
220220
);
221221
};
222222

223+
export const AnswerInteractionDisable = () => {
224+
const [selectedAnswerId, setSelectedAnswerId] = useState<number>(0);
225+
const [apiIsPending, setApiIsPending] = useState(false);
226+
const props = exerciseWithQuestionStatesProps();
227+
props.questionStates['1'].answer_id = selectedAnswerId;
228+
props.questionStates['1'].apiIsPending = apiIsPending;
229+
return (
230+
<Exercise
231+
{...props}
232+
onAnswerChange={(
233+
a: Omit<Answer, 'id'> & { id: number; question_id: number },
234+
) => {
235+
setSelectedAnswerId(a.id);
236+
}}
237+
onAnswerSave={() => setApiIsPending(true)}
238+
previewMode
239+
/>
240+
);
241+
};
242+
223243
export const DefaultWithoutFeedback = () => {
224244
const [selectedAnswerId, setSelectedAnswerId] = useState<number>(0);
225245
const [apiIsPending, setApiIsPending] = useState(false)
@@ -810,7 +830,7 @@ export const PreviewCard = () => {
810830

811831
return (
812832
<TextResizerProvider>
813-
<Exercise {...props1} className='preview-card' />
833+
<Exercise {...props1} className='preview-card' previewMode />
814834
</TextResizerProvider>
815835
);
816836
};
@@ -959,8 +979,8 @@ export const OverlayCard = () => {
959979
return (
960980
<TextResizerProvider>
961981
<h2>Exercise cards</h2>
962-
<Exercise {...props1} className='preview-card' />
963-
<Exercise {...props2} className='preview-card' />
982+
<Exercise {...props1} className='preview-card' previewMode />
983+
<Exercise {...props2} className='preview-card' previewMode />
964984
<h2>Exercise Preview cards</h2>
965985
{showDetails1 && <h2>Details 1!</h2>}
966986
<ExercisePreview

src/components/Exercise/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@ export const Exercise = styled(({
175175
scrollToQuestion,
176176
exerciseIcons,
177177
overlayChildren,
178+
previewMode = false,
178179
...props
179-
}: { className?: string } & (ExerciseWithStepDataProps | ExerciseWithQuestionStatesProps) & OverlayProps) => {
180+
}: { className?: string, previewMode?: boolean } & (ExerciseWithStepDataProps | ExerciseWithQuestionStatesProps) & OverlayProps) => {
180181
const legacyStepRender = 'feedback_html' in step;
181182
const questionsRef = React.useRef<Array<HTMLDivElement>>([]);
182183
const container = React.useRef<HTMLDivElement>(null);
@@ -236,6 +237,7 @@ export const Exercise = styled(({
236237
'canUpdateCurrentStep' in props ?
237238
props.canUpdateCurrentStep : !(i + 1 === exercise.questions.length)
238239
}
240+
previewMode={previewMode}
239241
/>
240242
)
241243
}

src/components/ExercisePreview.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export const ExercisePreview = (
8282
<Exercise
8383
exercise={exercise}
8484
className={selected ? 'preview-card is-selected' : 'preview-card'}
85+
previewMode
8586
{
8687
...(enableOverlay
8788
? {

src/components/ExerciseQuestion.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface ExerciseQuestionProps {
3838
show_all_feedback?: boolean;
3939
tableFeedbackEnabled?: boolean;
4040
hasFeedback?: ExerciseBaseProps['hasFeedback'];
41+
previewMode?: boolean;
4142
}
4243

4344
const AttemptsRemaining = ({ count }: { count: number }) => {
@@ -97,7 +98,7 @@ export const ExerciseQuestion = React.forwardRef((props: ExerciseQuestionProps,
9798
answer_id, hasMultipleAttempts, attempts_remaining, published_comments, detailedSolution,
9899
canAnswer, needsSaved, attempt_number, apiIsPending, onAnswerSave, onNextStep, canUpdateCurrentStep,
99100
displaySolution, available_points, free_response, show_all_feedback, tableFeedbackEnabled,
100-
hasFeedback
101+
hasFeedback, previewMode
101102
} = props;
102103

103104
const [shouldContinue, setShouldContinue] = React.useState(false)
@@ -129,6 +130,7 @@ export const ExerciseQuestion = React.forwardRef((props: ExerciseQuestionProps,
129130
displaySolution={displaySolution}
130131
show_all_feedback={show_all_feedback}
131132
tableFeedbackEnabled={tableFeedbackEnabled}
133+
previewMode={previewMode}
132134
>
133135
<FreeResponseReview free_response={free_response} />
134136
</Question>

src/components/Print.stories.tsx

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +0,0 @@
1-
import { ExerciseData, ExerciseQueryData, ExerciseQuestionData, StepBase } from '../../src/types';
2-
import data from '../../exercises.json';
3-
import styled from 'styled-components';
4-
import { Exercise } from './Exercise';
5-
6-
const ExerciseWrapper = styled.div`
7-
break-inside: avoid;
8-
9-
.step-card-body {
10-
padding: 24px 48px !important;
11-
}
12-
13-
.step-card-footer {
14-
display: none;
15-
}
16-
17-
.exercise-id {
18-
height: auto;
19-
}
20-
21-
.exercise-step {
22-
min-height: auto;
23-
}
24-
25-
.question-feedback {
26-
box-shadow: none !important;
27-
}
28-
29-
.openstax-answer {
30-
break-inside: avoid;
31-
32-
.answer-letter-wrapper::after {
33-
content: '' !important;
34-
}
35-
}
36-
`;
37-
38-
const exercises = (data as ExerciseQueryData).exercises as ExerciseData[];
39-
40-
const firstQuestionNumByExercise = exercises.reduce((acc, ex) => ({
41-
...acc,
42-
[ex.uuid]: acc.questionCounter + 1,
43-
questionCounter: acc.questionCounter + ex.questions.length
44-
}), {questionCounter: 0});
45-
46-
// placeholder until exercise data contains correct answer IDs
47-
const formatAnswerData = (questions: ExerciseQuestionData[]) => questions.map((q) => (
48-
{id: q.id, correct_answer_id: (q.answers.find((a) => a.correctness === '1.0')?.id || '')}));
49-
50-
const questionStateFields = {
51-
available_points: '1.0',
52-
is_completed: true,
53-
answer_id: '1',
54-
free_response: '',
55-
feedback_html: '',
56-
correct_answer_feedback_html: '',
57-
attempts_remaining: 0,
58-
attempt_number: 1,
59-
incorrectAnswerId: 0
60-
}
61-
62-
export const Default = () => (
63-
<>
64-
{data.title && <h2>Exercises for {data.title}</h2>}
65-
{exercises.map(((exercise) => {
66-
67-
const step: StepBase = {
68-
id: 1,
69-
uid: exercise.uid,
70-
available_points: '1.0',
71-
};
72-
73-
const questionStates = formatAnswerData(exercise.questions).reduce((acc, answer) => {
74-
const {id, correct_answer_id} = answer;
75-
return {...acc, [id]: {...questionStateFields, correct_answer_id}};
76-
}, {});
77-
78-
return (
79-
<ExerciseWrapper key={exercise.uid}>
80-
<Exercise
81-
key={exercise.uid}
82-
canAnswer={true}
83-
needsSaved={true}
84-
hasMultipleAttempts={false}
85-
onAnswerChange={() => undefined}
86-
onAnswerSave={() => undefined}
87-
onNextStep={() => undefined}
88-
apiIsPending={false}
89-
canUpdateCurrentStep={false}
90-
exercise={exercise}
91-
step={step}
92-
questionNumber={firstQuestionNumByExercise[exercise.uuid]}
93-
numberOfQuestions={exercises.length}
94-
questionStates={questionStates}
95-
show_all_feedback={true} />
96-
</ExerciseWrapper>
97-
)
98-
}))}
99-
</>);

src/components/Question.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export interface QuestionProps {
232232
children?: ReactNode;
233233
answerIdOrder?: ID[];
234234
choicesEnabled?: boolean;
235+
previewMode?: boolean;
235236
}
236237

237238
export const Question = React.forwardRef((props: QuestionProps, ref: React.ForwardedRef<HTMLDivElement>) => {
@@ -296,7 +297,8 @@ export const Question = React.forwardRef((props: QuestionProps, ref: React.Forwa
296297
<AnswersTable
297298
{...props}
298299
onChangeAnswer={props.onChange}
299-
hasCorrectAnswer={hasCorrectAnswer} />
300+
hasCorrectAnswer={hasCorrectAnswer}
301+
/>
300302

301303
{solution}
302304
{props.displayFormats ? <FormatsListing formats={formats} /> : undefined}

0 commit comments

Comments
 (0)