diff --git a/.storybook/src/3.components/display-field.tsx b/.storybook/src/3.components/display-field.tsx index 5a6ebfe..5909e4b 100644 --- a/.storybook/src/3.components/display-field.tsx +++ b/.storybook/src/3.components/display-field.tsx @@ -27,7 +27,7 @@ storiesOf("Components | Display Field", module) return ( - + @@ -36,7 +36,6 @@ storiesOf("Components | Display Field", module) - ); } @@ -91,20 +90,31 @@ storiesOf("Components | Display Field", module) return ( - - - - + + + + + + + + + + + ); } diff --git a/packages/display-field/src/DisplayField.test.tsx b/packages/display-field/src/DisplayField.test.tsx index 2d38401..4132abd 100644 --- a/packages/display-field/src/DisplayField.test.tsx +++ b/packages/display-field/src/DisplayField.test.tsx @@ -70,5 +70,22 @@ describe("Display field", () => { expect(await window.navigator.clipboard.readText()).toEqual(value); }) + + it('should copy custom value to clipboard', async () => { + wrapper.setProps({ + valueToCopy:'Custom Value', + copy:true, + link: { + href:"http://example.com", + target:'_blank' + } + }) + const anchor = wrapper.find(Anchor); + expect(anchor.length).toEqual(2); + const copy = anchor.at(1); + copy.simulate('click'); + expect(await window.navigator.clipboard.readText()).toEqual('Custom Value'); + + }) }); }); diff --git a/packages/display-field/src/DisplayField.tsx b/packages/display-field/src/DisplayField.tsx index 89f9d58..a7dac9b 100644 --- a/packages/display-field/src/DisplayField.tsx +++ b/packages/display-field/src/DisplayField.tsx @@ -26,6 +26,7 @@ interface Props extends StyledThemeProps { as?: PolymorphicType; link?: string | AnchorProps, copy?: boolean, + valueToCopy?: string, label?: string, } @@ -68,6 +69,7 @@ const LabelBox = styled(Box)` export const DisplayField: React.FunctionComponent = ( { value, + valueToCopy, link, label, as = 'p', @@ -87,9 +89,10 @@ export const DisplayField: React.FunctionComponent = ( } = theme; const WithLink = ({link, children}) => { - return link ? - {children} - : children; + return link ? + + {children} + : children; } const WithLabel = ({label, children}) => { @@ -102,7 +105,12 @@ export const DisplayField: React.FunctionComponent = ( const WithCopyIcon = ({children}) => { return copy ? {children} - copyToClipboard(value)} title={'Copy to clipboard'}> + { + copyToClipboard(valueToCopy || value) + }} + title={'Copy to clipboard'}> : children @@ -114,9 +122,7 @@ export const DisplayField: React.FunctionComponent = ( - { - copyToClipboard(value) - }}> + {value}