Skip to content

Commit

Permalink
Fix LazyCountrySymbol crash with invalid code (#4157)
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Wooding <[email protected]>
  • Loading branch information
origami-z and joshwooding authored Sep 26, 2024
1 parent 3fba23f commit c9e932d
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-knives-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@salt-ds/countries": patch
---

Fixed LazyCountrySymbol crash with invalid code, and `sharp` was not working correctly.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ const composedStories = composeStories(countrySymbolStory);

describe("Given an LazyCountrySymbol", () => {
checkAccessibility(composedStories);

it("should not crash if passed in invalid code", () => {
const { LazyCountrySymbol } = composedStories;
cy.mount(
// @ts-ignore
<LazyCountrySymbol code="invalid" />,
);
});
});
15 changes: 10 additions & 5 deletions packages/countries/src/lazy-country-symbol/LazyCountrySymbol.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ export type LazyCountrySymbolProps = {

export const LazyCountrySymbol = ({
code,
sharp,
...props
}: LazyCountrySymbolProps) => {
const Component = lazyMap[code];
const mapCode = sharp ? (`${code}_Sharp` as const) : code;
const Component = lazyMap[mapCode];

if (!Component && process.env.NODE_ENV !== "production") {
console.warn(
`Setting country code to ${code} which is invalid for <LazyCountrySymbol />`,
);
if (!Component) {
if (process.env.NODE_ENV !== "production") {
console.warn(
`Setting country code to ${code} which is invalid for <LazyCountrySymbol />`,
);
}
return null;
}

return <Component {...props} />;
Expand Down
38 changes: 38 additions & 0 deletions packages/countries/stories/LazyCountrySymbol.qa.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LazyCountrySymbol, countryMetaMap } from "@salt-ds/countries";
import type { CountryCode } from "@salt-ds/countries";
import type { Meta, StoryFn } from "@storybook/react";
import { QAContainer } from "docs/components";
import { Suspense } from "react";

export default {
Expand Down Expand Up @@ -43,3 +44,40 @@ export const AllLazyCountrySymbols: StoryFn = () => {
AllLazyCountrySymbols.parameters = {
chromatic: { disableSnapshot: false },
};

export const AllLazyCountrySharpSymbols: StoryFn = (props) => {
return (
<Suspense fallback="Loading...">
{sizes.map((size) => (
<div
key={size}
style={{
display: "grid",
// Different cols, to avoid overlap in TD in Chromatic
gridTemplateColumns: "repeat(10, auto)",
gap: 8,
padding: "12px 0",
}}
>
{Object.keys(countryMetaMap)
.map(
(componentCode) => countryMetaMap[componentCode as CountryCode],
)
.map(({ countryCode }) => (
<LazyCountrySymbol
key={countryCode}
code={countryCode}
id={`${size}-${countryCode}-sharp`}
size={size}
sharp
/>
))}
</div>
))}
</Suspense>
);
};

AllLazyCountrySharpSymbols.parameters = {
chromatic: { disableSnapshot: false },
};
6 changes: 5 additions & 1 deletion packages/countries/stories/LazyCountrySymbol.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FlexLayout } from "@salt-ds/core";
import {
LazyCountrySymbol as LazyCountrySymbolComponent,
countryMetaMap,
Expand Down Expand Up @@ -28,7 +29,10 @@ export const LazyCountrySymbol: StoryFn<typeof LazyCountrySymbolComponent> = (
) => {
return (
<Suspense fallback={"Loading..."}>
<LazyCountrySymbolComponent {...args} />
<FlexLayout>
<LazyCountrySymbolComponent {...args} />
<LazyCountrySymbolComponent sharp {...args} />
</FlexLayout>
</Suspense>
);
};
Expand Down
6 changes: 5 additions & 1 deletion site/src/examples/country-symbol/LazyLoading.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { FlexLayout } from "@salt-ds/core";
import { LazyCountrySymbol } from "@salt-ds/countries";
import { Suspense } from "react";

const code = "AD" as const;

export const LazyLoading = () => (
<Suspense fallback="Loading...">
<LazyCountrySymbol code={code} />
<FlexLayout>
<LazyCountrySymbol code={code} />
<LazyCountrySymbol code={code} sharp />
</FlexLayout>
</Suspense>
);

0 comments on commit c9e932d

Please sign in to comment.