Skip to content

Commit

Permalink
Merge pull request #29 from AndyOGo/feature/enhance-empty-objects-and…
Browse files Browse the repository at this point in the history
…-arrays

feat: improve empty objects and empty arrays
  • Loading branch information
AnyRoad authored Sep 1, 2024
2 parents a47e4e9 + f31a686 commit e9cbfd8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/DataRenderer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ const testButtonsExpanded = () => {
return buttons;
};

const testButtonsIfEmpty = () => {
expect(() => {
screen.getAllByRole('button', { hidden: true });
}).toThrow();
};

const testClickableNodeCollapsed = () => {
const buttons = screen.getAllByRole('button', { hidden: true });
expect(buttons.length).toBe(4);
Expand Down Expand Up @@ -143,6 +149,52 @@ describe('DataRender', () => {
expect(screen.getByText(/empty key/)).toBeInTheDocument();
});

it('should render empty objects', () => {
render(<DataRender {...commonProps} value={{}} />);
expect(screen.getByText('{')).toBeInTheDocument();
expect(screen.getByText('}')).toBeInTheDocument();
});

it('should render nested empty objects', () => {
render(<DataRender {...commonProps} value={{ nested: [] }} />);
expect(screen.getByText('nested:')).toBeInTheDocument();
expect(screen.getByText('{')).toBeInTheDocument();
expect(screen.getByText('}')).toBeInTheDocument();
});

it('should not expand empty objects', () => {
render(<DataRender {...commonProps} value={{}} shouldExpandNode={allExpanded} />);
testButtonsIfEmpty();
});

it('should not collapse empty objects', () => {
render(<DataRender {...commonProps} value={{}} shouldExpandNode={collapseAll} />);
testButtonsIfEmpty();
});

it('should render empty arrays', () => {
render(<DataRender {...commonProps} value={[]} />);
expect(screen.getByText('[')).toBeInTheDocument();
expect(screen.getByText(']')).toBeInTheDocument();
});

it('should render nested empty arrays', () => {
render(<DataRender {...commonProps} value={{ nested: [] }} />);
expect(screen.getByText('nested:')).toBeInTheDocument();
expect(screen.getByText('[')).toBeInTheDocument();
expect(screen.getByText(']')).toBeInTheDocument();
});

it('should not expand empty arrays', () => {
render(<DataRender {...commonProps} value={[]} shouldExpandNode={allExpanded} />);
testButtonsIfEmpty();
});

it('should not collapse empty arrays', () => {
render(<DataRender {...commonProps} value={[]} shouldExpandNode={collapseAll} />);
testButtonsIfEmpty();
});

it('should render arrays', () => {
render(<DataRender {...commonProps} value={[1, 2, 3]} />);
expect(screen.getByText('1')).toBeInTheDocument();
Expand Down
41 changes: 40 additions & 1 deletion src/DataRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface JsonRenderProps<T> {
}

export interface ExpandableRenderProps {
field?: string;
field: string | undefined;
value: Array<any> | object;
data: Array<[string | undefined, any]>;
openBracket: string;
Expand Down Expand Up @@ -144,6 +144,25 @@ function ExpandableObject({
);
}

export interface EmptyRenderProps {
field: string | undefined;
openBracket: string;
closeBracket: string;
lastElement: boolean;
style: StyleProps;
}

function EmptyObject({ field, openBracket, closeBracket, lastElement, style }: EmptyRenderProps) {
return (
<div className={style.basicChildStyle} role='listitem'>
{field && <span className={style.label}>{field}:</span>}
<span className={style.punctuation}>{openBracket}</span>
<span className={style.punctuation}>{closeBracket}</span>
{!lastElement && <span className={style.punctuation}>,</span>}
</div>
);
}

function JsonObject({
field,
value,
Expand All @@ -153,6 +172,16 @@ function JsonObject({
clickToExpandNode,
level
}: JsonRenderProps<Object>) {
if (Object.keys(value).length === 0) {
return EmptyObject({
field,
openBracket: '{',
closeBracket: '}',
lastElement,
style
});
}

return ExpandableObject({
field,
value,
Expand All @@ -176,6 +205,16 @@ function JsonArray({
shouldExpandNode,
clickToExpandNode
}: JsonRenderProps<Array<any>>) {
if (value.length === 0) {
return EmptyObject({
field,
openBracket: '[',
closeBracket: ']',
lastElement,
style
});
}

return ExpandableObject({
field,
value,
Expand Down
2 changes: 2 additions & 0 deletions src/stories/JsonView.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ const jsonData = {
'date property': new Date(0),
'boolean property': true,
'null property': null,
'empty array': [],
'array propery': [1, 2, 3, 4, 5],
'empty object': {},
'nested object': {
first: true,
second: 'another value',
Expand Down
4 changes: 4 additions & 0 deletions src/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
font-weight: bold;
}

.punctuation-base + .punctuation-base {
margin-left: -5px;
}

.pointer {
cursor: pointer;
}
Expand Down

0 comments on commit e9cbfd8

Please sign in to comment.