Skip to content

Commit

Permalink
Add boolean fallback parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
colinking committed Sep 21, 2019
1 parent 42fd059 commit 3c4fc7d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import PropTypes from 'prop-types';
import terminalLink from 'terminal-link';

const Link = props => {
const options = {
fallback: (text, url) => {
return props.fallback ? `${text} (${url})` : text;
}
};

return (
<Text unstable__transformChildren={children => terminalLink(children, props.url)}>
<Text unstable__transformChildren={children => terminalLink(children, props.url, options)}>
{props.children}
</Text>
);
Expand All @@ -16,11 +22,13 @@ Link.propTypes = {
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
]).isRequired,
url: PropTypes.string
url: PropTypes.string,
fallback: PropTypes.bool
};

Link.defaultProps = {
url: ''
url: '',
fallback: true
};

module.exports = Link;
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ Type: `string`

The URL to link to.

#### fallback

Type: `boolean`

For unsupported terminals, `fallback` determines whether the URL should be printed in parens after the text: `My website (https://sindresorhus.com)`. Defaults to `true`.

## Related

Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ test('render fallback', t => {
console.log(lastFrame());
t.snapshot(lastFrame());
});

test('exclude fallback if disabled', t => {
process.env.FORCE_HYPERLINK = 0;
const Link = require('.');

const {lastFrame} = render(
<Link url="https://sindresorhus.com" fallback={false}>
My Website
</Link>
);
console.log(lastFrame());
t.snapshot(lastFrame());
});
6 changes: 6 additions & 0 deletions test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ Generated by [AVA](https://ava.li).
> Snapshot 1
'My Website (https://sindresorhus.com)'

## exclude fallback if disabled

> Snapshot 1
'My Website'
Binary file modified test.js.snap
Binary file not shown.

0 comments on commit 3c4fc7d

Please sign in to comment.