Skip to content

Some enhancements #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ const Component = () => (
value={object}
// just define it
valueTypes={[
{
is: (value) => typeof value === 'string' && value.startsWith('https://i.imgur.com'),
Component: (props) => <Image height={50} width={50} src={props.value} alt={props.value}/>,
},
// or
createDataType(
(value) => typeof value === 'string' &&
value.startsWith('https://i.imgur.com'),
(props) => {
return <Image height={50} width={50} src={props.value}
alt={props.value}/>
}
(value) => typeof value === 'string' && value.startsWith('https://i.imgur.com'),
(props) => <Image height={50} width={50} src={props.value} alt={props.value}/>,
)
]}
/>
Expand Down
16 changes: 9 additions & 7 deletions src/components/DataKeyPair.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,20 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
: null
}
{
(isRoot && rootName !== false)
? <>&quot;{rootName}&quot;</>
: isRoot && rootName === false
? <></>
: !isRoot && KeyRenderer.when(downstreamProps)
(isRoot
? (
rootName !== false
? (quotesOnKeys ? <>&quot;{rootName}&quot;</> : <>{rootName}</>)
: null
)
: KeyRenderer.when(downstreamProps)
? <KeyRenderer {...downstreamProps} />
: nestedIndex === undefined && (
isNumberKey
? <Box component='span'
style={{ color: numberKeyColor }}>{key}</Box>
? <Box component='span' style={{ color: numberKeyColor }}>{key}</Box>
: quotesOnKeys ? <>&quot;{key}&quot;</> : <>{key}</>
)
)
}
{
(
Expand Down
4 changes: 1 addition & 3 deletions src/components/DataTypes/Object.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const PreObjectType: React.FC<DataItemProps<object>> = (props) => {
<CircularArrowsIcon sx={{
fontSize: 12,
color: textColor,
mx: sizeOfValue ? 0.5 : 0
mx: 0.5,
}}/>
{isTrap}
</>
Expand Down Expand Up @@ -123,7 +123,6 @@ export const ObjectType: React.FC<DataItemProps<object>> = (props) => {
if (iterator && !Array.isArray(value)) {
const elements = []
if (value instanceof Map) {
let _count = 0
for (const item of value) {
// fixme: key might be a object, array, or any value for the `Map<any, any>`
const [k, value] = item
Expand All @@ -132,7 +131,6 @@ export const ObjectType: React.FC<DataItemProps<object>> = (props) => {
<DataKeyPair key={key} path={[...props.path, key]} value={value}
editable={false}/>
)
_count++
}
} else {
let count = 0
Expand Down
38 changes: 20 additions & 18 deletions src/hooks/useInspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,31 @@ export function useInspect (path: (string | number)[], value: any, nestedIndex?:
store => store.defaultInspectDepth)
useEffect(() => {
const inspect = getInspectCache(path, nestedIndex)
if (inspect === undefined) {
if (nestedIndex !== undefined) {
setInspectCache(path, false, nestedIndex)
} else {
// do not inspect when it is a cycle reference, otherwise there will have a loop
const inspect = isTrap
? false
: depth < defaultInspectDepth
setInspectCache(path, inspect)
}
if (inspect !== undefined) {
return
}
if (nestedIndex !== undefined) {
setInspectCache(path, false, nestedIndex)
} else {
// do not inspect when it is a cycle reference, otherwise there will have a loop
const inspect = isTrap
? false
: depth < defaultInspectDepth
setInspectCache(path, inspect)
}
}, [defaultInspectDepth, depth, getInspectCache, isTrap, nestedIndex, path, setInspectCache])
const [inspect, set] = useState<boolean>(() => {
const shouldInspect = getInspectCache(path, nestedIndex)
if (shouldInspect === undefined) {
if (nestedIndex !== undefined) {
return false
}
return isTrap
? false
: depth < defaultInspectDepth
if (shouldInspect !== undefined) {
return shouldInspect
}
if (nestedIndex !== undefined) {
return false
}
return shouldInspect
return isTrap
? false
: depth < defaultInspectDepth

})
const setInspect = useCallback<Dispatch<SetStateAction<boolean>>>((apply) => {
set((oldState) => {
Expand Down
22 changes: 11 additions & 11 deletions src/stores/typeRegistry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const {
} = createStore<ReturnType<typeof createTypeRegistryStore>>()

const objectType: DataType<object> = {
is: (value): value is object => typeof value === 'object',
is: (value) => typeof value === 'object',
Component: ObjectType,
PreComponent: PreObjectType,
PostComponent: PostObjectType
Expand Down Expand Up @@ -110,7 +110,7 @@ export function predefined (): DataType<any>[] {

registerType<boolean>(
{
is: (value): value is boolean => typeof value === 'boolean',
is: (value) => typeof value === 'boolean',
...createEasyType(
'bool',
({ value }) => <>{value ? 'true' : 'false'}</>,
Expand All @@ -133,7 +133,7 @@ export function predefined (): DataType<any>[] {

registerType<Date>(
{
is: (value): value is Date => value instanceof Date,
is: (value) => value instanceof Date,
...createEasyType(
'date',
({ value }) => <>{value.toLocaleTimeString('en-us', displayOptions)}</>,
Expand All @@ -146,7 +146,7 @@ export function predefined (): DataType<any>[] {

registerType<null>(
{
is: (value): value is null => value === null,
is: (value) => value === null,
...createEasyType(
'null',
() => {
Expand All @@ -171,7 +171,7 @@ export function predefined (): DataType<any>[] {

registerType<undefined>(
{
is: (value): value is undefined => value === undefined,
is: (value) => value === undefined,
...createEasyType(
'undefined',
() => {
Expand All @@ -198,7 +198,7 @@ export function predefined (): DataType<any>[] {

registerType<string>(
{
is: (value): value is string => typeof value === 'string',
is: (value) => typeof value === 'string',
...createEasyType(
'string',
(props) => {
Expand Down Expand Up @@ -238,7 +238,7 @@ export function predefined (): DataType<any>[] {

registerType<Function>(
{
is: (value): value is Function => typeof value === 'function',
is: (value) => typeof value === 'function',
Component: FunctionType,
PreComponent: PreFunctionType,
PostComponent: PostFunctionType
Expand All @@ -249,7 +249,7 @@ export function predefined (): DataType<any>[] {

registerType<number>(
{
is: (value): value is number => typeof value === 'number' && isNaN(value),
is: (value) => typeof value === 'number' && isNaN(value),
...createEasyType(
'NaN',
() => {
Expand All @@ -276,7 +276,7 @@ export function predefined (): DataType<any>[] {

registerType<number>(
{
is: (value): value is number => typeof value === 'number' &&
is: (value) => typeof value === 'number' &&
!isInt(value),
...createEasyType(
'float',
Expand All @@ -291,7 +291,7 @@ export function predefined (): DataType<any>[] {

registerType<number>(
{
is: (value): value is number => typeof value === 'number' && isInt(value),
is: (value) => typeof value === 'number' && isInt(value),
...createEasyType(
'int',
({ value }) => <>{`${value}`}</>,
Expand All @@ -305,7 +305,7 @@ export function predefined (): DataType<any>[] {

registerType<bigint>(
{
is: (value): value is bigint => typeof value === 'bigint',
is: (value) => typeof value === 'bigint',
...createEasyType(
'bigint',
({ value }) => <>{`${value}n`}</>,
Expand Down
2 changes: 1 addition & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type DataType<ValueType = unknown> = {
/**
* Whether the value belongs to the data type
*/
is: (value: unknown) => value is ValueType
is: (value: unknown) => boolean
Component: React.ComponentType<DataItemProps<ValueType>>
Editor?: React.ComponentType<EditorProps<ValueType>>
PreComponent?: React.ComponentType<DataItemProps<ValueType>>
Expand Down
8 changes: 4 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function createDataType<ValueType = unknown> (
is: (value: unknown) => boolean,
Component: React.ComponentType<DataItemProps<ValueType>>
): {
is: (value: unknown) => value is ValueType
is: (value: unknown) => boolean
Component: React.ComponentType<DataItemProps<ValueType>>
}
// case 2: you only render with a single component with editor
Expand All @@ -37,7 +37,7 @@ export function createDataType<ValueType = unknown> (
Component: React.ComponentType<DataItemProps<ValueType>>,
Editor: React.ComponentType<EditorProps<ValueType>>
): {
is: (value: unknown) => value is ValueType
is: (value: unknown) => boolean
Component: React.ComponentType<DataItemProps<ValueType>>
Editor: React.ComponentType<DataItemProps<ValueType>>
}
Expand All @@ -49,7 +49,7 @@ export function createDataType<ValueType = unknown> (
PreComponent: React.ComponentType<DataItemProps<ValueType>>,
PostComponent: React.ComponentType<DataItemProps<ValueType>>
): {
is: (value: unknown) => value is ValueType
is: (value: unknown) => boolean
Component: React.ComponentType<DataItemProps<ValueType>>
PreComponent: React.ComponentType<DataItemProps<ValueType>>
PostComponent: React.ComponentType<DataItemProps<ValueType>>
Expand All @@ -62,7 +62,7 @@ export function createDataType<ValueType = unknown> (
PreComponent: React.ComponentType<DataItemProps<ValueType>>,
PostComponent: React.ComponentType<DataItemProps<ValueType>>
): {
is: (value: unknown) => value is ValueType
is: (value: unknown) => boolean
Component: React.ComponentType<DataItemProps<ValueType>>
Editor: React.ComponentType<DataItemProps<ValueType>>
PreComponent: React.ComponentType<DataItemProps<ValueType>>
Expand Down
6 changes: 3 additions & 3 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('render <JsonViewer/> with multiple instances', () => {
value={undefined}
valueTypes={[
{
is: (() => true) as any,
is: () => true,
Component: () => {
return <>first viewer</>
}
Expand All @@ -153,7 +153,7 @@ describe('render <JsonViewer/> with multiple instances', () => {
value={undefined}
valueTypes={[
{
is: (() => true) as any,
is: () => true,
Component: () => {
return <>second viewer</>
}
Expand Down Expand Up @@ -233,7 +233,7 @@ describe('render <JsonViewer/> with props', () => {
render(<JsonViewer value={undefined} valueTypes={[]}/>)
render(<JsonViewer value={undefined} valueTypes={[
{
is: (value: unknown): value is string => typeof value === 'string',
is: (value) => typeof value === 'string',
Component: (props) => {
expectTypeOf(props.value).toMatchTypeOf<unknown>()
return null
Expand Down