Skip to content

Commit

Permalink
refactor(RichText): update getRichTextProps, stricter types
Browse files Browse the repository at this point in the history
  • Loading branch information
chohner committed Nov 21, 2024
1 parent 5e7b35f commit 6eba4db
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 24 deletions.
5 changes: 1 addition & 4 deletions app/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ export default function Header({ heading, content }: HeaderProps) {
<div className="ds-stack-16">
<Heading {...heading} />
{content && (
<RichText
className={`ds-heading-03-reg ${content.className ?? ""}`}
markdown={content.markdown}
/>
<RichText className="ds-heading-03-reg" markdown={content.markdown} />
)}
</div>
);
Expand Down
10 changes: 4 additions & 6 deletions app/components/RichText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { sanatize } from "~/services/security/sanatizeHtml";
import { StandaloneLink } from "./StandaloneLink";

export const RichTextPropsSchema = z.object({
id: z.number().optional(),
markdown: z.string(),
className: z.string().optional(),
});

const CSS_HEADING_CLASSES = [
Expand Down Expand Up @@ -35,11 +33,12 @@ const defaultRenderer: Partial<Renderer> = {
const RichText = ({
markdown,
renderer,
className,
id,
className = "",
...props
}: RichTextProps & {
renderer?: Partial<Renderer>;
id?: string;
className?: string;
}) => {
const marked = new Marked({
renderer: renderer ?? defaultRenderer,
Expand All @@ -52,8 +51,7 @@ const RichText = ({
return (
<div
{...props}
id={id?.toString()}
className={`rich-text ds-stack-8 ${className ?? ""}`}
className={`rich-text ds-stack-8 ${className}`}
dangerouslySetInnerHTML={{ __html: sanatize(html) }}
/>
);
Expand Down
7 changes: 1 addition & 6 deletions app/components/__test__/RichText.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ describe("RichText component", () => {
});

it("should handle empty markdown", () => {
const markdown = "";
const className = "custom-class";
const { container } = render(
<RichText markdown={markdown} className={className} />,
);

const { container } = render(<RichText markdown={""} />);
expect(container).toBeEmptyDOMElement();
});

Expand Down
5 changes: 4 additions & 1 deletion app/components/cookieBanner/CookieBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ export function CookieBanner({
<div>
<div className="ds-stack-8">
{content.paragraphs.map((paragraph) => (
<RichText key={paragraph.id} markdown={paragraph.markdown} />
<RichText
key={paragraph.markdown}
markdown={paragraph.markdown}
/>
))}
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/components/inputs/Textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const Textarea = ({
</InputLabel>
)}
{description && (
<RichText className={"ds-body-01-reg"} markdown={description} />
<RichText className="ds-body-01-reg" markdown={description} />
)}
{details && <DetailsSummary {...details} />}
<textarea
Expand Down
10 changes: 4 additions & 6 deletions app/services/cms/models/StrapiParagraph.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { z } from "zod";
import { RichTextPropsSchema } from "~/components/RichText";
import { omitNull } from "~/util/omitNull";
import { type RichTextProps } from "~/components/RichText";
import { HasOptionalStrapiIdSchema } from "./HasStrapiId";

export const StrapiParagraphSchema = z
Expand All @@ -13,7 +12,6 @@ export const StrapiParagraphComponentSchema = StrapiParagraphSchema.extend({

export type StrapiParagraph = z.infer<typeof StrapiParagraphSchema>;

export const getRichTextProps = (cmsData: StrapiParagraph) => {
const markdown = cmsData.text;
return RichTextPropsSchema.parse(omitNull({ ...cmsData, markdown }));
};
export const getRichTextProps = (cmsData: StrapiParagraph): RichTextProps => ({
markdown: cmsData.text,
});

0 comments on commit 6eba4db

Please sign in to comment.