Skip to content

Commit e9f5b24

Browse files
committed
fix: fix shortenTextAfterLength props. #15
1 parent e347cd7 commit e9f5b24

File tree

6 files changed

+26
-7
lines changed

6 files changed

+26
-7
lines changed

core/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,12 @@ export interface JsonViewProps<T extends object> extends React.DetailedHTMLProps
696696
displayDataTypes?: boolean;
697697
/** The user can copy objects and arrays to clipboard by clicking on the clipboard icon. @default true */
698698
enableClipboard?: boolean;
699+
/** When set to true, all nodes will be collapsed by default. Use an integer value to collapse at a particular depth. @default false */
700+
collapsed?: boolean | number;
699701
/** Whether to highlight updates. @default true */
700702
highlightUpdates?: boolean;
703+
/** Shorten long JSON strings, Set to `0` to disable this feature @default 30 */
704+
shortenTextAfterLength?: number;
701705
/** Callback function for when a treeNode is expanded or collapsed */
702706
onExpand?: (props: {
703707
expand: boolean;

core/src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export interface JsonViewProps<T extends object>
5858
collapsed?: boolean | number;
5959
/** Whether to highlight updates. @default true */
6060
highlightUpdates?: boolean;
61-
/** Shorten long JSON strings, Set to `0` to disable this feature @default 20 */
61+
/** Shorten long JSON strings, Set to `0` to disable this feature @default 30 */
6262
shortenTextAfterLength?: number;
6363
/** Callback function for when a treeNode is expanded or collapsed */
6464
onExpand?: (props: { expand: boolean; value?: T; keyid: string; keyName?: string | number }) => void;
@@ -107,7 +107,7 @@ const JsonView: JsonViewComponent = forwardRef<HTMLDivElement, JsonViewProps<obj
107107
collapsed,
108108
indentWidth = 15,
109109
displayObjectSize = true,
110-
shortenTextAfterLength = 20,
110+
shortenTextAfterLength = 30,
111111
highlightUpdates = true,
112112
enableClipboard = true,
113113
displayDataTypes = true,

core/src/types/String.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ it('renders <JsonView.String /> test case', async () => {
2929
const value = screen.getByTestId('value');
3030
expect(value.className).toBe('w-rjv-value');
3131
expect(value.tagName).toBe('SPAN');
32-
expect(value.innerHTML).toBe('Lorem ipsum dolor si...');
32+
expect(value.innerHTML).toBe('Lorem ipsum dolor sit amet. Lo...');
3333
await user.click(value);
3434
expect(value.innerHTML).toBe(demo.string);
3535
await user.click(value);
36-
expect(value.innerHTML).toBe('Lorem ipsum dolor si...');
36+
expect(value.innerHTML).toBe('Lorem ipsum dolor sit amet. Lo...');
3737
});

core/src/types/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import JsonView from '..';
33

44
it('renders <JsonView.String /> test case', async () => {
55
const demo = {
6-
string: 'Lorem ipsum dolor sit amet',
6+
string: 'Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet',
77
};
88
const { container } = render(
99
<JsonView value={demo}>

core/src/types/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, Fragment, PropsWithChildren, useState } from 'react';
1+
import { FC, Fragment, PropsWithChildren, useEffect, useState } from 'react';
22
import { useTypesStore } from '../store/Types';
33
import { useStore } from '../store';
44
import { ValueQuote } from '../symbol';
@@ -46,10 +46,11 @@ type TypeProps = PropsWithChildren<{
4646

4747
export const TypeString: FC<TypeProps> = ({ children = '', expandKey, keyName }) => {
4848
const { Str = {}, displayDataTypes } = useTypesStore();
49-
const { shortenTextAfterLength: length = 20 } = useStore();
49+
const { shortenTextAfterLength: length = 30 } = useStore();
5050
const { as, render, ...reset } = Str;
5151
const childrenStr = children as string;
5252
const [shorten, setShorten] = useState(length && childrenStr.length >= length);
53+
useEffect(() => setShorten(length && childrenStr.length >= length), [length]);
5354
const Comp = as || 'span';
5455
const style: React.CSSProperties = {
5556
...defalutStyle,

www/src/example/default.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const initialState: Partial<
9494
indentWidth: 15,
9595
collapsed: 2,
9696
quote: '"',
97+
shortenTextAfterLength: 50,
9798
theme: 'nord',
9899
};
99100

@@ -125,6 +126,7 @@ export function Example() {
125126
enableClipboard={state.enableClipboard}
126127
highlightUpdates={state.highlightUpdates}
127128
indentWidth={state.indentWidth}
129+
shortenTextAfterLength={state.shortenTextAfterLength}
128130
collapsed={state.collapsed}
129131
objectSortKeys={state.objectSortKeys}
130132
>
@@ -257,6 +259,18 @@ export function Example() {
257259
}
258260
/>
259261
</Label>
262+
<Label>
263+
<div>Shorten Text After Length({state.shortenTextAfterLength})</div>
264+
<input
265+
type="range"
266+
value={state.shortenTextAfterLength}
267+
onChange={(evn) =>
268+
dispatch({
269+
shortenTextAfterLength: Number(evn.target.value),
270+
})
271+
}
272+
/>
273+
</Label>
260274
</Options>
261275
</Fragment>
262276
);

0 commit comments

Comments
 (0)