From 25d7b5e8d4a900d027661093df838063ae790ebe Mon Sep 17 00:00:00 2001 From: Luca Tagliabue Date: Wed, 23 Aug 2023 11:24:02 +0200 Subject: [PATCH] feat: add copyToClipboard button --- packages/react-json-tree/examples/src/App.tsx | 2 +- .../src/expandCollapseButtons.tsx | 41 +++++++++++++++++-- .../src/expandCollapseContext.tsx | 7 +++- packages/react-json-tree/src/index.tsx | 3 ++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/packages/react-json-tree/examples/src/App.tsx b/packages/react-json-tree/examples/src/App.tsx index a972ce58..67b7d1c3 100644 --- a/packages/react-json-tree/examples/src/App.tsx +++ b/packages/react-json-tree/examples/src/App.tsx @@ -190,7 +190,7 @@ const App = () => ( Sort object keys with sortObjectKeys prop.

- +

Collapsed root node

diff --git a/packages/react-json-tree/src/expandCollapseButtons.tsx b/packages/react-json-tree/src/expandCollapseButtons.tsx index bcaab8f9..0f140e26 100644 --- a/packages/react-json-tree/src/expandCollapseButtons.tsx +++ b/packages/react-json-tree/src/expandCollapseButtons.tsx @@ -2,9 +2,11 @@ import { faArrowDown, faArrowRight, faUndo, + faCopy, + faCheck, } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import React, { ReactNode } from 'react'; +import React, { ReactNode, useState } from 'react'; import { ExpandCollapseAll } from '.'; import { useExpandCollapseAllContext } from './expandCollapseContext'; import { StylingFunction } from 'react-base16-styling'; @@ -12,6 +14,7 @@ import { StylingFunction } from 'react-base16-styling'; interface Props { expandCollapseAll: ExpandCollapseAll; styling: StylingFunction; + value: unknown; } interface ExpandButtonProps { @@ -24,11 +27,17 @@ interface CollapseButtonProps { collapseIcon?: ReactNode; } +interface CopyToClipboardButtonProps { + copyToClipboardIcon?: ReactNode; + copiedToClipboardIcon?: ReactNode; + value: unknown; +} + interface DefaultButtonProps { defaultIcon?: ReactNode; } -function ExpandCollapseButtons({ expandCollapseAll, styling }: Props) { +function ExpandCollapseButtons({ expandCollapseAll, styling, value }: Props) { const { enableDefaultButton } = useExpandCollapseAllContext(); const expandableDefaultValue = expandCollapseAll?.defaultValue || 'expand'; @@ -39,6 +48,12 @@ function ExpandCollapseButtons({ expandCollapseAll, styling }: Props) { )} + + ; } +function CopyToClipboardButton({copyToClipboardIcon, copiedToClipboardIcon, value}:CopyToClipboardButtonProps) { + const [isCopied, setIsCopied] = useState(false); + + const handleOnCopyToClipboard = async () => { + await navigator.clipboard.writeText(JSON.stringify(value, null, 2)); + setIsCopied(true) + } + + if(isCopied){ + return (
+ {copiedToClipboardIcon || } +
); + } + + return ( +
+ {copyToClipboardIcon || } +
) +} + function DefaultButton({ defaultIcon }: DefaultButtonProps) { const { setExpandAllState, setEnableDefaultButton } = useExpandCollapseAllContext(); @@ -122,8 +157,6 @@ function DefaultButton({ defaultIcon }: DefaultButtonProps) { {defaultIcon || }
); - - return <>; } export default ExpandCollapseButtons; diff --git a/packages/react-json-tree/src/expandCollapseContext.tsx b/packages/react-json-tree/src/expandCollapseContext.tsx index d1258ef5..0fbc8bf9 100644 --- a/packages/react-json-tree/src/expandCollapseContext.tsx +++ b/packages/react-json-tree/src/expandCollapseContext.tsx @@ -20,6 +20,7 @@ interface Props { children: ReactNode; expandCollapseAll?: ExpandCollapseAll; styling: StylingFunction; + value: unknown; } const ExpandCollapseAllContext = createContext({} as Context); @@ -28,11 +29,12 @@ function ExpandCollapseAllContextProvider({ expandCollapseAll, children, styling, + value, }: Props) { const [enableDefaultButton, setEnableDefaultButton] = useState(false); const [expandAllState, setExpandAllState] = useState(); - const value = useMemo( + const contextValue = useMemo( () => ({ enableDefaultButton, setEnableDefaultButton, @@ -43,12 +45,13 @@ function ExpandCollapseAllContextProvider({ ); return ( - + {children} {expandCollapseAll && ( )} diff --git a/packages/react-json-tree/src/index.tsx b/packages/react-json-tree/src/index.tsx index 0af44436..eb0df18d 100644 --- a/packages/react-json-tree/src/index.tsx +++ b/packages/react-json-tree/src/index.tsx @@ -29,6 +29,8 @@ interface ExpandCollapseAll { defaultValue?: 'expand' | 'collapse'; expandIcon?: ReactNode; collapseIcon?: ReactNode; + copyToClipboardIcon?: ReactNode; + copiedToClipboardIcon?: ReactNode; defaultIcon?: ReactNode; } @@ -70,6 +72,7 @@ export function JSONTree({