handle default button visibility

This commit is contained in:
lucataglia 2023-05-19 18:47:21 +02:00
parent b12babbcd9
commit 1f6d0291ee
4 changed files with 38 additions and 10 deletions

View File

@ -112,6 +112,7 @@ export default function JSONNestedNode(props: Props) {
shouldExpandNodeInitially, shouldExpandNodeInitially,
shouldExpandNode, shouldExpandNode,
styling, styling,
setEnableDefaultButton,
} = props; } = props;
const [expanded, setExpanded] = useState<boolean>( const [expanded, setExpanded] = useState<boolean>(
@ -131,8 +132,11 @@ export default function JSONNestedNode(props: Props) {
}, [defaultExpanded, shouldExpandNode]); }, [defaultExpanded, shouldExpandNode]);
const handleClick = useCallback(() => { const handleClick = useCallback(() => {
if (expandable) setExpanded(!expanded); if (expandable) {
}, [expandable, expanded]); setExpanded(!expanded);
setEnableDefaultButton(true);
}
}, [expandable, expanded, setEnableDefaultButton]);
const renderedChildren = const renderedChildren =
expanded || (hideRoot && level === 0) expanded || (hideRoot && level === 0)

View File

@ -5,6 +5,8 @@ import { Expandable } from '.';
interface Props { interface Props {
expandable?: Expandable; expandable?: Expandable;
enableDefaultButton: boolean;
setEnableDefaultButton: any;
shouldExpandNode?: 'expand' | 'collapse' | 'default'; shouldExpandNode?: 'expand' | 'collapse' | 'default';
setShouldExpandNode: any; setShouldExpandNode: any;
} }
@ -14,6 +16,7 @@ interface ExpandButtonProps {
expandIcon?: ReactNode; expandIcon?: ReactNode;
shouldExpandNode?: 'expand' | 'collapse' | 'default'; shouldExpandNode?: 'expand' | 'collapse' | 'default';
setShouldExpandNode: any; setShouldExpandNode: any;
setEnableDefaultButton: any;
} }
interface CollapseButtonProps { interface CollapseButtonProps {
@ -21,15 +24,19 @@ interface CollapseButtonProps {
collapseIcon?: ReactNode; collapseIcon?: ReactNode;
shouldExpandNode?: 'expand' | 'collapse' | 'default'; shouldExpandNode?: 'expand' | 'collapse' | 'default';
setShouldExpandNode: any; setShouldExpandNode: any;
setEnableDefaultButton: any;
} }
interface DefaultButtonProps { interface DefaultButtonProps {
defaultIcon?: ReactNode; defaultIcon?: ReactNode;
setShouldExpandNode: any; setShouldExpandNode: any;
setEnableDefaultButton: any;
} }
function ExpandableButtons({ function ExpandableButtons({
expandable, expandable,
enableDefaultButton,
setEnableDefaultButton,
setShouldExpandNode, setShouldExpandNode,
shouldExpandNode shouldExpandNode
}: Props){ }: Props){
@ -41,15 +48,17 @@ function ExpandableButtons({
return ( return (
<div style={{position: 'absolute', display: 'flex', justifyContent:'center', alignItems: 'center', gap:'1rem', top: '1rem', right: '1rem', cursor: 'pointer'}}> <div style={{position: 'absolute', display: 'flex', justifyContent:'center', alignItems: 'center', gap:'1rem', top: '1rem', right: '1rem', cursor: 'pointer'}}>
<DefaultButton {enableDefaultButton && <DefaultButton
defaultIcon={expandable?.defaultIcon} defaultIcon={expandable?.defaultIcon}
setShouldExpandNode={setShouldExpandNode} setShouldExpandNode={setShouldExpandNode}
/> setEnableDefaultButton={setEnableDefaultButton}
/>}
<ExpandButton <ExpandButton
expandableDefaultValue={expandableDefaultValue} expandableDefaultValue={expandableDefaultValue}
expandIcon={expandable?.expandIcon} expandIcon={expandable?.expandIcon}
setShouldExpandNode={setShouldExpandNode} setShouldExpandNode={setShouldExpandNode}
setEnableDefaultButton={setEnableDefaultButton}
shouldExpandNode={shouldExpandNode} shouldExpandNode={shouldExpandNode}
/> />
@ -57,14 +66,18 @@ function ExpandableButtons({
expandableDefaultValue={expandable?.defaultValue} expandableDefaultValue={expandable?.defaultValue}
collapseIcon={expandable?.collapseIcon} collapseIcon={expandable?.collapseIcon}
setShouldExpandNode={setShouldExpandNode} setShouldExpandNode={setShouldExpandNode}
setEnableDefaultButton={setEnableDefaultButton}
shouldExpandNode={shouldExpandNode} shouldExpandNode={shouldExpandNode}
/> />
</div> </div>
) )
} }
function ExpandButton({ expandableDefaultValue, expandIcon, shouldExpandNode, setShouldExpandNode }: ExpandButtonProps) { function ExpandButton({ expandableDefaultValue, expandIcon, shouldExpandNode, setEnableDefaultButton, setShouldExpandNode }: ExpandButtonProps) {
const onExpand = () => setShouldExpandNode('expand'); const onExpand = () => {
setShouldExpandNode('expand');
setEnableDefaultButton(true);
}
const isDefault = !shouldExpandNode ||shouldExpandNode === 'default' const isDefault = !shouldExpandNode ||shouldExpandNode === 'default'
@ -79,8 +92,11 @@ function ExpandButton({ expandableDefaultValue, expandIcon, shouldExpandNode, se
return <></>; return <></>;
} }
function CollapseButton({ expandableDefaultValue, collapseIcon, shouldExpandNode, setShouldExpandNode }: CollapseButtonProps) { function CollapseButton({ expandableDefaultValue, collapseIcon, shouldExpandNode, setEnableDefaultButton, setShouldExpandNode }: CollapseButtonProps) {
const onCollapse = () => setShouldExpandNode('collapse'); const onCollapse = () => {
setShouldExpandNode('collapse');
setEnableDefaultButton(true);
}
const isDefault = !shouldExpandNode ||shouldExpandNode === 'default' const isDefault = !shouldExpandNode ||shouldExpandNode === 'default'
@ -95,8 +111,11 @@ function ExpandButton({ expandableDefaultValue, expandIcon, shouldExpandNode, se
return <></>; return <></>;
} }
function DefaultButton({defaultIcon, setShouldExpandNode }:DefaultButtonProps) { function DefaultButton({defaultIcon, setEnableDefaultButton, setShouldExpandNode }:DefaultButtonProps) {
const onDefaultCollapse = () => setShouldExpandNode('default'); const onDefaultCollapse = () => {
setShouldExpandNode('default');
setEnableDefaultButton(false)
}
return ( return (
<div role="presentation" onClick={onDefaultCollapse}> <div role="presentation" onClick={onDefaultCollapse}>

View File

@ -58,6 +58,7 @@ export function JSONTree({
collectionLimit = 50, collectionLimit = 50,
sortObjectKeys = false, sortObjectKeys = false,
}: Props) { }: Props) {
const [enableDefaultButton, setEnableDefaultButton] = useState(false);
const [shouldExpandNode, setShouldExpandNode] = useState(); const [shouldExpandNode, setShouldExpandNode] = useState();
const styling = useMemo( const styling = useMemo(
@ -77,6 +78,7 @@ export function JSONTree({
valueRenderer={valueRenderer} valueRenderer={valueRenderer}
shouldExpandNodeInitially={shouldExpandNodeInitially} shouldExpandNodeInitially={shouldExpandNodeInitially}
shouldExpandNode={shouldExpandNode} shouldExpandNode={shouldExpandNode}
setEnableDefaultButton={setEnableDefaultButton}
hideRoot={hideRoot} hideRoot={hideRoot}
getItemString={getItemString} getItemString={getItemString}
postprocessValue={postprocessValue} postprocessValue={postprocessValue}
@ -86,6 +88,8 @@ export function JSONTree({
<ExpandableButtons <ExpandableButtons
expandable={expandable} expandable={expandable}
enableDefaultButton={enableDefaultButton}
setEnableDefaultButton={setEnableDefaultButton}
shouldExpandNode={shouldExpandNode} shouldExpandNode={shouldExpandNode}
setShouldExpandNode={setShouldExpandNode} setShouldExpandNode={setShouldExpandNode}
/> />

View File

@ -53,6 +53,7 @@ export interface CommonExternalProps {
isCustomNode: IsCustomNode; isCustomNode: IsCustomNode;
collectionLimit: number; collectionLimit: number;
sortObjectKeys: SortObjectKeys; sortObjectKeys: SortObjectKeys;
setEnableDefaultButton: any;
} }
export interface CommonInternalProps extends CommonExternalProps { export interface CommonInternalProps extends CommonExternalProps {