rename variables and update README

This commit is contained in:
lucataglia 2023-05-22 13:11:27 +02:00
parent a4aac76d5a
commit 032aeb6271
4 changed files with 36 additions and 21 deletions

View File

@ -137,6 +137,21 @@ Their full signatures are:
- `labelRenderer: function(keyPath, nodeType, expanded, expandable)` - `labelRenderer: function(keyPath, nodeType, expanded, expandable)`
- `valueRenderer: function(valueAsString, value, ...keyPath)` - `valueRenderer: function(valueAsString, value, ...keyPath)`
#### Customize Expandable Buttons
Passing the `expandable` props will activate in the top right corner of the JSONTree component the `expand all/collapse all` buttons. You can pass a JSON to customize the expand all/collapse all icons. The default icons are from [FontAwesome](https://fontawesome.com/).
```jsx
<JSONTree
expandable={{
defaultValue: 'expand',
expandIcon: /* your custom expand icon */,
collapseIcon: /* your custom collapse icon */,
defaultIcon: /* your custom restore to default icon */,
}}
/>
```
#### More Options #### More Options
- `shouldExpandNodeInitially: function(keyPath, data, level)` - determines if node should be expanded when it first renders (root is expanded by default) - `shouldExpandNodeInitially: function(keyPath, data, level)` - determines if node should be expanded when it first renders (root is expanded by default)

View File

@ -113,7 +113,7 @@ export default function JSONNestedNode(props: Props) {
shouldExpandNodeInitially, shouldExpandNodeInitially,
styling, styling,
} = props; } = props;
const { shouldExpandNode, setEnableDefaultButton, setShouldExpandNode } = const { expandAllState, setExpandAllState, setEnableDefaultButton } =
useExpandableButtonContext(); useExpandableButtonContext();
const [defaultExpanded] = useState<boolean>( const [defaultExpanded] = useState<boolean>(
@ -121,7 +121,7 @@ export default function JSONNestedNode(props: Props) {
isCircular isCircular
? false ? false
: (function getDefault() { : (function getDefault() {
switch (shouldExpandNode) { switch (expandAllState) {
case 'expand': case 'expand':
return true; return true;
case 'collapse': case 'collapse':
@ -139,7 +139,7 @@ export default function JSONNestedNode(props: Props) {
* could lead to a "Maximum update depth exceeded" error */ * could lead to a "Maximum update depth exceeded" error */
const expandedRef = useRef<boolean>(defaultExpanded); const expandedRef = useRef<boolean>(defaultExpanded);
switch (shouldExpandNode) { switch (expandAllState) {
case 'expand': case 'expand':
expandedRef.current = isCircular ? false : true; expandedRef.current = isCircular ? false : true;
break; break;
@ -157,9 +157,9 @@ export default function JSONNestedNode(props: Props) {
expandedRef.current = !expandedRef.current; expandedRef.current = !expandedRef.current;
setTriggerReRender((e) => !e); setTriggerReRender((e) => !e);
setEnableDefaultButton(true); setEnableDefaultButton(true);
setShouldExpandNode(undefined); setExpandAllState(undefined);
} }
}, [expandable, setEnableDefaultButton, setShouldExpandNode]); }, [expandable, setEnableDefaultButton, setExpandAllState]);
const renderedChildren = const renderedChildren =
expandedRef.current || (hideRoot && level === 0) expandedRef.current || (hideRoot && level === 0)

View File

@ -56,18 +56,18 @@ function ExpandButton({
expandableDefaultValue, expandableDefaultValue,
expandIcon, expandIcon,
}: ExpandButtonProps) { }: ExpandButtonProps) {
const { shouldExpandNode, setShouldExpandNode, setEnableDefaultButton } = const { expandAllState, setExpandAllState, setEnableDefaultButton } =
useExpandableButtonContext(); useExpandableButtonContext();
const onExpand = () => { const onExpand = () => {
setShouldExpandNode('expand'); setExpandAllState('expand');
setEnableDefaultButton(true); setEnableDefaultButton(true);
}; };
const isDefault = !shouldExpandNode || shouldExpandNode === 'default'; const isDefault = !expandAllState || expandAllState === 'default';
if ( if (
shouldExpandNode === 'collapse' || expandAllState === 'collapse' ||
(isDefault && expandableDefaultValue === 'expand') (isDefault && expandableDefaultValue === 'expand')
) { ) {
return ( return (
@ -84,18 +84,18 @@ function CollapseButton({
expandableDefaultValue, expandableDefaultValue,
collapseIcon, collapseIcon,
}: CollapseButtonProps) { }: CollapseButtonProps) {
const { shouldExpandNode, setShouldExpandNode, setEnableDefaultButton } = const { expandAllState, setExpandAllState, setEnableDefaultButton } =
useExpandableButtonContext(); useExpandableButtonContext();
const onCollapse = () => { const onCollapse = () => {
setShouldExpandNode('collapse'); setExpandAllState('collapse');
setEnableDefaultButton(true); setEnableDefaultButton(true);
}; };
const isDefault = !shouldExpandNode || shouldExpandNode === 'default'; const isDefault = !expandAllState || expandAllState === 'default';
if ( if (
shouldExpandNode === 'expand' || expandAllState === 'expand' ||
(isDefault && expandableDefaultValue === 'collapse') (isDefault && expandableDefaultValue === 'collapse')
) { ) {
return ( return (
@ -109,11 +109,11 @@ function CollapseButton({
} }
function DefaultButton({ defaultIcon }: DefaultButtonProps) { function DefaultButton({ defaultIcon }: DefaultButtonProps) {
const { setShouldExpandNode, setEnableDefaultButton } = const { setExpandAllState, setEnableDefaultButton } =
useExpandableButtonContext(); useExpandableButtonContext();
const onDefaultCollapse = () => { const onDefaultCollapse = () => {
setShouldExpandNode('default'); setExpandAllState('default');
setEnableDefaultButton(false); setEnableDefaultButton(false);
}; };

View File

@ -12,8 +12,8 @@ import { StylingFunction } from 'react-base16-styling';
interface Context { interface Context {
enableDefaultButton: boolean; enableDefaultButton: boolean;
setEnableDefaultButton: any; setEnableDefaultButton: any;
shouldExpandNode?: 'expand' | 'collapse' | 'default'; expandAllState?: 'expand' | 'collapse' | 'default';
setShouldExpandNode: any; setExpandAllState: any;
} }
interface Props { interface Props {
@ -30,16 +30,16 @@ function ExpandableButtonsContextProvider({
styling, styling,
}: Props) { }: Props) {
const [enableDefaultButton, setEnableDefaultButton] = useState(false); const [enableDefaultButton, setEnableDefaultButton] = useState(false);
const [shouldExpandNode, setShouldExpandNode] = useState(); const [expandAllState, setExpandAllState] = useState();
const value = useMemo( const value = useMemo(
() => ({ () => ({
enableDefaultButton, enableDefaultButton,
setEnableDefaultButton, setEnableDefaultButton,
shouldExpandNode, expandAllState,
setShouldExpandNode, setExpandAllState,
}), }),
[enableDefaultButton, shouldExpandNode] [enableDefaultButton, expandAllState]
); );
return ( return (