Skip to content

Commit

Permalink
👌 fix with more types and test
Browse files Browse the repository at this point in the history
  • Loading branch information
arunachalam-monk committed Jun 20, 2024
1 parent e890f36 commit 8319c26
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
4 changes: 2 additions & 2 deletions documentation/src/components/Sights/TopBar/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { labels, sights, vehicles } from '@monkvision/sights';
import { SearchBar } from '@site/src/components';
import { SightCard } from '@site/src/components/Sights/SightCard';
import clsx from 'clsx';
import React, { FunctionComponentElement, useRef } from 'react';
import React, { ReactElement, useRef } from 'react';
import styles from './TopBar.module.css';

export interface ListItem {
id: string;
lookups: string[];
component: FunctionComponentElement<typeof SightCard>;
component: ReactElement;
}

export interface Tab {
Expand Down
9 changes: 3 additions & 6 deletions documentation/src/pages/Sights/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { LabelTable, ListItem, SightCard, Tab, tabs, TopBar } from '@site/src/components';
import { LabelTable, ListItem, Tab, tabs, TopBar } from '@site/src/components';
import Layout from '@theme/Layout';
import React, { FunctionComponentElement, useMemo, useState } from 'react';
import React, { ReactElement, useMemo, useState } from 'react';
import styles from './styles.module.css';

function lookupItems(
lookup: string,
items: ListItem[],
): FunctionComponentElement<typeof SightCard>[] {
function lookupItems(lookup: string, items: ListItem[]): ReactElement[] {
const str = lookup.toLowerCase();
const filtered = str
? items.filter((item) => item.lookups.some((lookupStr) => lookupStr.includes(str)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function SVGElement<T extends keyof JSXIntrinsicSVGElements = 'svg'>({
element,
groupIds,
getAttributes,
style: attributes.style,
style: attributes.style ?? {},
});
const tagAttr = { ...attributes, ...customAttributes, ...passThroughProps } as any;
const innerHTML = useInnerHTML({ element, groupIds, getInnerText });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function tranformJsxAttribute(attr: HtmlAttribute): JsxAttribute {
* Custom hook used to map HTML attributes to their JSX counterpart (ex: "class" becomes "className", properly mapping
* inline style values etc.).
*/
export function useJSXTransformAttributes(element: Element): JSX.IntrinsicElements {
export function useJSXTransformAttributes(element: Element): Partial<JSX.IntrinsicElements> {
return useMemo(
() =>
element.getAttributeNames().reduce((prev, attr) => {
Expand All @@ -42,8 +42,8 @@ export function useJSXTransformAttributes(element: Element): JSX.IntrinsicElemen
return {
...prev,
[key]: value,
} as Partial<JSX.IntrinsicElements>;
}, {}) as JSX.IntrinsicElements,
};
}, {}),
[element],
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ describe('SVGElement component', () => {
<SVGElement element={element} groupIds={groupIds} getAttributes={getAttributes} />,
);

expect(useCustomAttributes).toHaveBeenCalledWith({ element, groupIds, getAttributes });
expect(useCustomAttributes).toHaveBeenCalledWith({
element,
groupIds,
getAttributes,
style: {},
});
unmount();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,17 @@ describe('useCustomAttributes hook', () => {
expect(result.current).toEqual(customAttr);
unmount();
});
it('should apply style correctly', () => {
const elementStyle = { stroke: '#fff' },
customStyle = { color: 'test-color' };
const element = { tagName: 'path', style: { stroke: '#fff' } } as SVGSVGElement;
const groupIds = ['test-id-1', 'test-id-2'];
const getAttributes = jest.fn(() => ({ style: customStyle }));
const { result, unmount } = renderHook(useCustomAttributes, {
initialProps: { element, groupIds, getAttributes, style: elementStyle },
});
expect(getAttributes).toHaveBeenCalledWith(element, groupIds);
expect(result.current).toEqual({ style: { ...elementStyle, ...customStyle } });
unmount();
});
});

0 comments on commit 8319c26

Please sign in to comment.