0.0.1 • Published 2 months ago

@zeta-ui/json-tree v0.0.1

Weekly downloads
-
License
-
Repository
-
Last release
2 months ago

JSON Tree

Install

npm install zeta-global/json-tree
# or
yarn add zeta-global/json-tree

Example Usage

// Import
import {
    JsonTree,
    ADD_DELTA_TYPE,
    REMOVE_DELTA_TYPE,
    UPDATE_DELTA_TYPE,
    DATA_TYPES,
    INPUT_USAGE_TYPES,
} from 'zeta-global/json-tree'

// Data
const data = {
    error: new Error('error'),
    text: 'text',
    int: 100,
    boolean: true,
    null: null,
    object: {
        text: 'text',
        int: 100,
        boolean: true,
    },
    array: [
        1,
        {
            string: 'test',
        },
    ],
}

// Component
<JsonTree data={data} />

Props

data

KeyDescriptionTypeRequiredDefault
dataData to be displayed/editedObject | ArrayTrueNone

rootName

KeyDescriptionTypeRequiredDefault
rootNameName of the root objectstringFalseroot

isCollapsed

KeyDescriptionTypeRequiredDefault
isCollapsedWhether the node is collapsed (for Array/Object/Error)FunctionFalse(keyPath, deep) => (deep !== 0)

Function parameters:

KeyDescriptionTypeExample
keyPathPath to the current node/valuestring[]['object'] for data: { object: { string: 'test' } }
deepDepth of the current nodenumber1 for data: { object: { string: 'test' } } on 'object' node
dataData of the current node/valueunknown{ string: 'test' } for data: { object: { string: 'test' } }

onFullyUpdate

KeyDescriptionTypeRequiredDefault
onFullyUpdateCallback function called upon each update with the entire new data structureFunctionFalse() => {}

Function parameters:

KeyDescriptionType
dataUpdated dataObject | Array (same type as the data prop)

onDeltaUpdate

KeyDescriptionTypeRequiredDefault
onDeltaUpdateCallback function called upon each update with only the data that has changedFunctionFalse() => {}

Function parameters:

KeyDescriptionType
dataDelta dataObject

Delta data structure:

KeyDescriptionTypeExample
typeDelta typestring'ADD_DELTA_TYPE', 'REMOVE_DELTA_TYPE', or 'UPDATE_DELTA_TYPE'
keyPathPath to the current node/valuestring[]['object'] for data: { object: { string: 'test' } }
deepDepth of the current nodenumber1 for data: { object: { string: 'test' } } on 'object' node
keyModified/created/removed key namestringNone
newValueNew valueunknownNone
oldValueOld valueunknownNone

readOnly

KeyDescriptionTypeRequiredDefault
readOnlyIf a boolean, whether the entire structure should be read-only. If a function, whether the node/value supplied to the function should be read-only (called for all nodes/values).boolean | FunctionFalse(keyName, data, keyPath, deep, dataType) => false

This function must return a boolean.

Function parameters:

KeyDescriptionTypeExample
keyNameKey name of the current node/valuestring'object' for data: { object: { string: 'test' } }
dataData of the current node/valueunknown{ string: 'test' } for data: { object: { string: 'test' } }
keyPathPath to the current node/valuestring[]['object'] for data: { object: { string: 'test' } }
deepDepth of the current nodenumber1 for data: { object: { string: 'test' } } on 'object' node
dataTypeData type of the current node/valuestring'Object', 'Array', 'Null', 'Undefined', 'Error', 'Number', ...

getStyle

KeyDescriptionTypeRequiredDefault
getStyleCallback function which should return the CSS style for each node/valueFunctionFalse(keyName, data, keyPath, deep, dataType) => {...}

Function parameters:

KeyDescriptionTypeExample
keyNameKey name of the current node/valuestring'object' for data: { object: { string: 'test' } }
datadata of the current node/valueunknown{ string: 'test' } for data: { object: { string: 'test' } }
keyPathPath to the current node/valuestring[]['object'] for data: { object: { string: 'test' } }
deepDepth of the current nodenumber1 for data: { object: { string: 'test' } } on 'object' node
dataTypeData type of the current node/valuestring'Object', 'Array', 'Null', 'Undefined', 'Error', 'Number', ...

An example of return:

{
    minus: {
        color: 'red',
    },
    plus: {
        color: 'green',
    },
    collapsed: {
        color: 'grey',
    },
    delimiter: {},
    ul: {
        padding: '0px',
        margin: '0 0 0 25px',
        listStyle: 'none',
    },
    name: {
        color: '#2287CD',
    },
    addForm: {},
}

You can see the default style definitions in src/utils/styles.js.

addButtonElement

KeyDescriptionTypeRequiredDefault
addButtonElementCustom add button element (to confirm adding a new value to an object/array)JSX.ElementFalse<button>+</button>

The library will add an onClick handler to the element.

cancelButtonElement

KeyDescriptionTypeRequiredDefault
cancelButtonElementCustom cancel button element (to cancel editing a value)JSX.ElementFalse<button>c</button>

The library will add an onClick handler to the element.

editButtonElement

KeyDescriptionTypeRequiredDefault
editButtonElementCustom edit button element (to finish editing a value)JSX.ElementFalse<button>e</button>

The library will add an onClick handler to the element.

inputElement

KeyDescriptionTypeRequiredDefault
inputElementCustom text input element (to edit a value)JSX.Element | FunctionFalse(usage, keyPath, deep, keyName, data, dataType) => <input />

The library will add a placeholder, ref, and defaultValue prop to the element. This element will be focused when possible.

Function parameters:

KeyDescriptionTypeExample
usageUsage of the generated inputstringAll values are listed in INPUT_USAGE_TYPES
keyPathPath to the current node/valuestring[][] for data: { object: { string: 'test' } }
deepDepth of the current nodenumber1 for data: { object: { string: 'test' } } on 'object' node
keyKey of the current node/valuestring'object' for data: { object: { string: 'test' } }
valueValue of the keyunknown{ string: 'test' } for data: { object: { string: 'test' } } on 'object' node
dataTypeData type of the valuestringAll values are listed in DATA_TYPES

textareaElement

KeyDescriptionTypeRequiredDefault
textareaElementCustom textarea element (to edit a long value, like functions)JSX.Element | FunctionFalse(usage, keyPath, deep, keyName, data, dataType) => <textarea />

The library will add a ref and defaultValue prop to the element. This element will be focused when possible.

Function parameters:

KeyDescriptionTypeExample
usageUsage of the generated inputstringAll values are listed in INPUT_USAGE_TYPES
keyPathPath to the current node/valuestring[][] for data: { object: { string: 'test' } }
deepDepth of the current nodenumber1 for data: { object: { string: 'test' } } on 'object' node
keyKey of the current node/valuestring'object' for data: { object: { string: 'test' } }
valueValue of the keyunknown{ string: 'test' } for data: { object: { string: 'test' } } on 'object' node
dataTypeData type of the valuestringAll values are listed in DATA_TYPES

minusMenuElement

KeyDescriptionTypeRequiredDefault
minusMenuElementCustom minus menu element (to remove a value from an object/array)JSX.ElementFalse<span> - </span>

The library will add an onClick, className, and style prop to the element.

plusMenuElement

KeyDescriptionTypeRequiredDefault
plusMenuElementCustom plus menu element (to begin adding a new value to an object/array)JSX.ElementFalse<span> + </span>

The library will add an onClick, className, and style prop to the element.

beforeRemoveAction

KeyDescriptionTypeRequiredDefault
beforeRemoveActionAsync function called upon the user trying to remove a node/value with the minus menu elementFunctionFalse(key, keyPath, deep, oldValue) => new Promise(resolve => resolve())

This function must return a Promise. If the promise is resolved, the node/value will be removed. Otherwise, if rejected, nothing will be done.

Function parameters:

KeyDescriptionTypeExample
keyKey name of the current node/valuestring'object' for data: { object: { string: 'test' } }
keyPathPath to the current node/valuestring[][] for data: { object: { string: 'test' } }
deepDepth of the current nodenumber1 for data: { object: { string: 'test' } } on 'object' node
oldValueOld value of the keyunknown{ string: 'test' } for data: { object: { string: 'test' } } on 'object' node

beforeAddAction

KeyDescriptionTypeRequiredDefault
beforeAddActionAsync function called upon the user trying to add a node/value with the add menu elementFunctionFalse(key, keyPath, deep, newValue) => new Promise(resolve => resolve())

This function must return a Promise. If the promise is resolved, the node/value will be added. Otherwise, if rejected, nothing will be done.

Function parameters:

KeyDescriptionTypeExample
keyKey of the current node/valuestring'string' for data: { object: { string: 'test' } }
keyPathPath to the current node/valuestring[]['object'] for data: { object: { string: 'test' } }
deepDepth of the current nodenumber1 for data: { object: { string: 'test' } } on 'object' node
newValueNew value of the keyunknown'test' for data: { object: { string: 'test' } } on 'string' node

beforeUpdateAction

KeyDescriptionTypeRequiredDefault
beforeUpdateActionAsync function called upon the user trying to edit a node/valueFunctionFalse(key, keyPath, deep, oldValue, newValue) => new Promise(resolve => resolve())

This function must return a Promise. If the promise is resolved, the node/value will be updated. Otherwise, if rejected, nothing will be done.

Function parameters:

KeyDescriptionTypeExample
keyKey of the current node/valuestring'string' for data: { object: { string: 'test' } }
keyPathPath to the current node/valuestring[]['object'] for data: { object: { string: 'test' } }
deepDepth of the current nodenumber1 for data: { object: { string: 'test' } } on 'object' node
oldValueOld value of the keyunknown'test' for data: { object: { string: 'test' } } on 'string' node
newValueNew value of the keyunknown'update' for data: { object: { string: 'update' } } on 'string' node

logger

KeyDescriptionTypeRequiredDefault
loggerObject used to log errors caught from promises (using only the 'error' key)ObjectFalse{ error: () => {} }

onSubmitValueParser

KeyDescriptionTypeRequiredDefault
onSubmitValueParserFunction called upon every value addition/update to parse raw string data from inputElements or textareaElements into the correct object typesFunctionFalse(isEditMode, keyPath, deep, key, rawValue) => nativeParser(rawValue)

Function parameters:

KeyDescriptionTypeExample
isEditModeWhether the value is being edited on an existing node/value, otherwise it's being newly addedbooleanTrue
keyPathPath to the current node/valuestring[]['object'] for data: { object: { string: 'test' } }
deepDepth of the current nodenumber1 for data: { object: { string: 'test' } } on 'object' node
keyKey of the current node/valuestring'string' for data: { object: { string: 'test' } }
rawValueRaw string value from the inputElement or textareaElementstring'test' for data: { object: { string: 'test' } }

allowFunctionEvaluation

KeyDescriptionTypeRequiredDefault
allowFunctionEvaluationAllow strings that appear to be Javascript function definitions to be evaluated as Javascript functionsbooleanFalseTrue

Development

npm commands

Build

Build the library to dist/ using parcel.

npm run build

Publish

Publishes the library to npm. This runs a parcel build.

npm publish
0.0.1

2 months ago