diff --git a/src/introduction.mdx b/src/introduction.mdx index ebc77f7e..243fd96b 100644 --- a/src/introduction.mdx +++ b/src/introduction.mdx @@ -9,7 +9,7 @@ components that can be used to build user interfaces for freeCodeCamp projects. ## Installation -- Run the following command to install the library in your app: +- Run the following command to install the library: ```bash pnpm install @freecodecamp/ui diff --git a/src/quiz-question/quiz-question.tsx b/src/quiz-question/quiz-question.tsx index e893e39a..bf1bad68 100644 --- a/src/quiz-question/quiz-question.tsx +++ b/src/quiz-question/quiz-question.tsx @@ -3,6 +3,8 @@ import { RadioGroup } from "@headlessui/react"; import type { QuizQuestionAnswer, QuizQuestionProps } from "./types"; import { Answer } from "./answer"; +import { QuestionLabel } from "../quiz/question-label"; +import { OptionLabel } from "../quiz/option-label"; const QuestionText = ({ question, @@ -17,9 +19,7 @@ const QuestionText = ({ return ( - {position}. -   - {question} + {position}. {question} ); }; @@ -32,7 +32,7 @@ const QuestionText = ({ * but instead, it provides a `selectedAnswer` and an `onChange` props, * giving the parent component full control over the selection handling logic. */ -export const QuizQuestion = ({ +const QuizQuestion = ({ question, answers, required, @@ -44,10 +44,7 @@ export const QuizQuestion = ({ const handleChange = ( selectedOption: QuizQuestionAnswer["value"], ) => { - if (!onChange) { - return; - } - + if (!onChange) return; onChange(selectedOption); }; @@ -56,9 +53,6 @@ export const QuizQuestion = ({ onChange={handleChange} aria-required={required} disabled={disabled} - // `selectedAnswer` should not be `undefined` - // or React will automatically consider QuizQuestion an uncontrolled component - // Ref: https://react.dev/reference/react-dom/components/input#im-getting-an-error-a-component-is-changing-an-uncontrolled-input-to-be-controlled value={selectedAnswer ?? null} > @@ -67,6 +61,7 @@ export const QuizQuestion = ({ {answers.map(({ value, label, feedback, validation }) => { const checked = selectedAnswer === value; + return ( ({ ); }; + +QuizQuestion.QuestionLabel = QuestionLabel; +QuizQuestion.OptionLabel = OptionLabel; + +export { QuizQuestion }; diff --git a/src/quiz/option-label.tsx b/src/quiz/option-label.tsx new file mode 100644 index 00000000..6d2a0314 --- /dev/null +++ b/src/quiz/option-label.tsx @@ -0,0 +1,19 @@ +import React from "react"; +import { PrismFormatted } from "../prism-formatted"; + +interface OptionLabelProps { + questionText: string; +} + +const removeParagraphTags = (html: string) => html.replace(/^

|<\/p>$/g, ""); + +export const OptionLabel = ({ questionText }: OptionLabelProps) => { + return ( + ""} + /> + ); +}; diff --git a/src/quiz/question-label.tsx b/src/quiz/question-label.tsx new file mode 100644 index 00000000..02537b21 --- /dev/null +++ b/src/quiz/question-label.tsx @@ -0,0 +1,10 @@ +import React from "react"; +import { PrismFormatted } from "../prism-formatted"; + +interface QuestionLabelProps { + question: string; +} + +export const QuestionLabel = ({ question }: QuestionLabelProps) => { + return ""} />; +}; diff --git a/src/quiz/quiz.tsx b/src/quiz/quiz.tsx index dda9f497..175918bb 100644 --- a/src/quiz/quiz.tsx +++ b/src/quiz/quiz.tsx @@ -2,8 +2,10 @@ import React from "react"; import { QuizQuestion } from "../quiz-question"; import { type QuizProps } from "./types"; +import { QuestionLabel } from "./question-label"; +import { OptionLabel } from "./option-label"; -export const Quiz = ({ +const Quiz = ({ questions, disabled, required, @@ -23,3 +25,8 @@ export const Quiz = ({ ); }; + +Quiz.QuestionLabel = QuestionLabel; +Quiz.OptionLabel = OptionLabel; + +export { Quiz };