diff --git a/CHANGELOG.md b/CHANGELOG.md index dda0731..a37b996 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,12 @@ +## 2.0.4 + +_Oct 22, 2024_ + +- fix: typo initalCollapsedDepth > initialCollapsedDepth +- chore: improved jsdoc for the props + ## 2.0.3 + _Oct 20, 2024_ - fix: xml validation diff --git a/README.md b/README.md index a50e0ac..4ec1417 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ When the xml is invalid, invalidXml component will be returned.\ Allow collapse/expand tags by click on them. When tag is collapsed its content and attributes are hidden.\ **Default**: false -### initalCollapsedDepth (number) +### initialCollapsedDepth (number) When the **collapsible** is true, this set the level that will be started as collapsed. For example, if you want to everything starts as collapsed, set 0.\ **Default**: undefined diff --git a/package.json b/package.json index 839a136..afb21c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-xml-viewer", - "version": "2.0.3", + "version": "2.0.4", "description": "Simple xml viewer component for React", "author": "alissonmbr", "license": "MIT", diff --git a/src/components/XMLViewer.tsx b/src/components/XMLViewer.tsx index 60a262b..e7a1a6b 100644 --- a/src/components/XMLViewer.tsx +++ b/src/components/XMLViewer.tsx @@ -15,12 +15,18 @@ export default function XMLViewer(props: XMLViewerProps): JSX.Element { indentSize = 2, invalidXml, initalCollapsedDepth, + initialCollapsedDepth, } = props; const [theme, setTheme] = useState(() => ({ ...defaultTheme, ...customTheme })); const { json, valid } = useXMLViewer(xml); const context = useMemo( - () => ({ theme, collapsible, indentSize, initalCollapsedDepth }), - [theme, collapsible, indentSize, initalCollapsedDepth], + () => ({ + theme, + collapsible, + indentSize, + initialCollapsedDepth: initialCollapsedDepth ?? initalCollapsedDepth, + }), + [theme, collapsible, indentSize, initalCollapsedDepth, initialCollapsedDepth], ); useEffect(() => { diff --git a/src/components/__tests__/XMLViewer.test.tsx b/src/components/__tests__/XMLViewer.test.tsx index d9526be..75cc995 100644 --- a/src/components/__tests__/XMLViewer.test.tsx +++ b/src/components/__tests__/XMLViewer.test.tsx @@ -145,7 +145,7 @@ describe('XMLViewer', () => { ` - render(); + render(); expect(screen.getAllByText('root')).toHaveLength(2); expect(screen.getAllByText('level1')).toHaveLength(2); expect(screen.queryByText('this should be collapsed')).toBeNull(); diff --git a/src/components/types.ts b/src/components/types.ts index 845ea92..bf2a099 100644 --- a/src/components/types.ts +++ b/src/components/types.ts @@ -1,33 +1,49 @@ export interface Theme { /** + * The tag name color (``) * + * @default #d43900 */ tagColor?: string; /** + * The text color (`Text`) * + * @default #333 */ textColor?: string; /** + * The attribute key color (``) * + * @default #2a7ab0 */ attributeKeyColor?: string; /** + * The attribute value color (` `) * + * @default #008000 */ attributeValueColor?: string; /** + * The separators colors (`<, >, , =, `) * + * @default #333 */ separatorColor?: string; /** + * The comment color (``) * + * @default #aaa */ commentColor?: string; /** + * the cdata element color (``) * + * @default #1D781D */ cdataColor?: string; /** + * The font family + * * @default monospace */ fontFamily?: string; @@ -35,28 +51,54 @@ export interface Theme { export interface XMLViewerProps { /** - * A xml in string format + * A xml string to prettify. */ xml: string; /** + * An object to customize the default theme. * + * @default + * ```js + * { + * tagColor: '#d43900', + * textColor: '#333', + * attributeKeyColor: '#2a7ab0', + * attributeValueColor: '#008000', + * separatorColor: '#333', + * commentColor: '#aaa', + * cdataColor: '#1d781d', + * fontFamily: 'monospace', + * } + * ``` */ theme?: Theme; /** - * + * The size of the indentation. + * * @default 2 */ indentSize?: number; /** + * When the xml is invalid, invalidXml component will be returned. * + * @default
Invalid XML!
*/ invalidXml?: JSX.Element; /** + * Allow collapse/expand tags by click on them. When tag is collapsed its content and attributes are hidden. + * * @default false */ collapsible?: boolean; /** - * + * @deprecated use the initialCollapsedDepth instead */ initalCollapsedDepth?: number; + /** + * When the **collapsible** is true, this set the level that will be started as collapsed. + * For example, if you want to everything starts as collapsed, set 0. + * + * @default undefined + */ + initialCollapsedDepth?: number; } diff --git a/src/hooks/useCollapsible.ts b/src/hooks/useCollapsible.ts index 5b32a7c..cc428c9 100644 --- a/src/hooks/useCollapsible.ts +++ b/src/hooks/useCollapsible.ts @@ -3,17 +3,17 @@ import _isNil from 'lodash/isNil'; import { useEffect, useState } from 'react'; export function useCollapsible(level: number) { - const { collapsible, initalCollapsedDepth } = useXMLViewerContext(); + const { collapsible, initialCollapsedDepth } = useXMLViewerContext(); const [collapsed, setCollapsed] = useState(() => - _isNil(initalCollapsedDepth) || !collapsible ? false : level >= initalCollapsedDepth, + _isNil(initialCollapsedDepth) || !collapsible ? false : level >= initialCollapsedDepth, ); const toggleCollapsed = () => setCollapsed((currentCollapsed) => !currentCollapsed); useEffect(() => { setCollapsed( - _isNil(initalCollapsedDepth) || !collapsible ? false : level >= initalCollapsedDepth, + _isNil(initialCollapsedDepth) || !collapsible ? false : level >= initialCollapsedDepth, ); - }, [initalCollapsedDepth, level, collapsible]); + }, [initialCollapsedDepth, level, collapsible]); return { collapsed, diff --git a/src/stories/XMLViewer.stories.tsx b/src/stories/XMLViewer.stories.tsx index a05cd0b..bc7ff92 100644 --- a/src/stories/XMLViewer.stories.tsx +++ b/src/stories/XMLViewer.stories.tsx @@ -69,7 +69,6 @@ const meta = { argTypes: { invalidXml: { table: { - disable: true, }, }, @@ -85,6 +84,6 @@ export const Primary: Story = { indentSize: 2, collapsible: true, theme: defaultTheme, - initalCollapsedDepth: undefined + initialCollapsedDepth: undefined, }, }; diff --git a/src/types/index.ts b/src/types/index.ts index 2a7c9a8..5ded8b1 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -17,5 +17,5 @@ export interface IXmlViewerContext { collapsible: boolean; indentSize: number; theme: Theme; - initalCollapsedDepth?: number; + initialCollapsedDepth?: number; }