-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Table): URL cell can fallback to short-text cell (#263)
If the value provided in the URL column does not use one of the following protocols: `http`,` https`, `ftp`, `ftps`, `sftp`, `mailto, the content will be displayed in a short-text cell instead.
- Loading branch information
1 parent
f4c5e15
commit 89ca583
Showing
7 changed files
with
52 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 17 additions & 14 deletions
31
packages/visualizations/src/components/Table/Cell/Format/URLFormat.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
<script lang="ts"> | ||
import { warn } from './utils'; | ||
import ShortTextFormat from './ShortTextFormat.svelte'; | ||
import { isValidUrl } from './utils'; | ||
export let rawValue: unknown; | ||
export let display = (v: string) => v; | ||
export let target: HTMLAnchorElement['target'] = '_blank'; | ||
export let rel = 'nofollow noreferrer noopener'; | ||
function format(v: unknown) { | ||
try { | ||
// eslint-disable-next-line no-new | ||
new URL(v as string); | ||
return v as string; | ||
} catch (error) { | ||
warn(v, 'URL'); | ||
return null; | ||
$: format = (v: unknown) => { | ||
if (isValidUrl(v)) { | ||
return { | ||
text: display(v), | ||
href: v, | ||
}; | ||
} | ||
} | ||
// eslint-disable-next-line no-console | ||
console.warn(`ODS Dataviz SDK - Table: no url detected in ${v}. Formatting as string.`); | ||
return { text: null, href: null }; | ||
}; | ||
$: value = format(rawValue); | ||
$: ({ text, href } = format(rawValue)); | ||
</script> | ||
|
||
{#if value} | ||
<a href={value} {target}>{display(value)}</a> | ||
{#if text} | ||
<a {href} {rel} {target}>{text}</a> | ||
{:else} | ||
{rawValue} | ||
<ShortTextFormat {rawValue} {display} /> | ||
{/if} |
6 changes: 3 additions & 3 deletions
6
packages/visualizations/src/components/Table/Cell/Format/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89ca583
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage for this commit
Coverage Report