From dca9b0ff277bd039d94ca0d768690b7078a8b8c4 Mon Sep 17 00:00:00 2001 From: tyson Date: Wed, 4 Sep 2019 16:22:55 +1000 Subject: [PATCH] Added ability to resolve share URL using a function. --- src/utils/createShareButton.jsx | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/utils/createShareButton.jsx b/src/utils/createShareButton.jsx index 4226b701..37e128ac 100644 --- a/src/utils/createShareButton.jsx +++ b/src/utils/createShareButton.jsx @@ -70,7 +70,10 @@ class ShareButton extends PureComponent { onClick: PropTypes.func, opts: PropTypes.object, openWindow: PropTypes.bool, - url: PropTypes.string.isRequired, + url: PropTypes.oneOfType([ + PropTypes.string, + PropTypes.func, + ]).isRequired, role: PropTypes.string, style: PropTypes.object, windowWidth: PropTypes.number, @@ -94,7 +97,7 @@ class ShareButton extends PureComponent { tabIndex: '0', } - onClick = (e) => { + onClick = async (e) => { const { disabled, onClick, @@ -108,7 +111,7 @@ class ShareButton extends PureComponent { e.preventDefault(); - const link = this.link(); + const link = await this.link(); const clickHandler = openWindow ? () => this.openWindow(link) : () => onClick(link); @@ -151,8 +154,14 @@ class ShareButton extends PureComponent { windowOpen(link, windowConfig, onShareWindowClose); } - link() { - const { url, opts, networkLink } = this.props; + async link() { + let { url } = this.props; + const { opts, networkLink } = this.props; + + if (typeof url === 'function') { + url = await Promise.resolve(url()); + } + return networkLink(url, opts); }