Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/lib/actions/tooltip.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import type { Action } from 'svelte/action';
import type { Props } from 'tippy.js';
import type { Props as TippyProps } from 'tippy.js';
import tippy from 'tippy.js';

type Props = TippyProps & {
disabled?: boolean;
};

export const tooltip: Action<HTMLElement, Partial<Props>> = (node, config) => {
const instance = tippy(node, config);
if (config.disabled) instance.disable();

return {
update({ content }) {
update({ content, disabled }) {
if (content !== instance.props.content) {
instance.setProps({
content
});
}

disabled ? instance.disable() : instance.enable();
},
destroy() {
instance.destroy();
Expand Down
3 changes: 3 additions & 0 deletions src/lib/elements/forms/inputCheckbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { FormItem, Helper } from '.';

export let label: string;
export let optionalText: string | undefined = undefined;
export let showLabel = true;
export let id: string;
export let value = false;
Expand All @@ -27,6 +28,8 @@

<FormItem>
<label class:u-hide={!showLabel} class="label" for={id}>{label}</label>
<span class:u-hide={!showLabel || !optionalText} class="optional">{optionalText}</span>

<div class="input-text-wrapper">
<input
{id}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/elements/forms/inputDateTime.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

export let label: string;
export let showLabel = true;
export let optionalText: string | undefined = undefined;
export let id: string;
export let value = '';
export let required = false;
Expand Down Expand Up @@ -39,6 +40,8 @@

<FormItem>
<label class:u-hide={!showLabel} class="label" for={id}>{label}</label>
<span class:u-hide={!showLabel || !optionalText} class="optional">{optionalText}</span>

<div class="input-text-wrapper">
<input
{id}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/elements/forms/inputNumber.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { FormItem, Helper } from '.';

export let label: string;
export let optionalText: string | undefined = undefined;
export let showLabel = true;
export let id: string;
export let value: number = null;
Expand Down Expand Up @@ -51,7 +52,9 @@
</script>

<FormItem>
<label class:u-hide={!showLabel} class="label" for={id}>{label}</label>
<label class:u-hide={!showLabel || !label} class="label" for={id}>{label}</label>
<span class:u-hide={!showLabel || !optionalText} class="optional">{optionalText}</span>

<div class="input-text-wrapper">
<input
{id}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/elements/forms/inputSelect.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

export let id: string;
export let label: string;
export let optionalText: string | undefined = undefined;
export let showLabel = true;
export let value: string | number | boolean;
export let placeholder = '';
Expand Down Expand Up @@ -33,6 +34,8 @@

<FormItem>
<label class:u-hide={!showLabel} class="label" for={id}>{label}</label>
<span class:u-hide={!showLabel || !optionalText} class="optional">{optionalText}</span>

<div class="select">
<select
{id}
Expand Down
7 changes: 5 additions & 2 deletions src/lib/elements/forms/inputText.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { FormItem, Helper } from '.';

export let label: string;
export let optionalText: string | undefined = undefined;
export let showLabel = true;
export let id: string;
export let value = '';
Expand Down Expand Up @@ -42,8 +43,8 @@
</script>

<FormItem>
<label class:u-hide={!showLabel} class="label" for={id}
>{label}
<label class:u-hide={!showLabel} class="label" for={id}>
{label}
{#if tooltip}
<span
class="icon-info"
Expand All @@ -53,6 +54,8 @@
}} />
{/if}
</label>
<span class:u-hide={!showLabel || !optionalText} class="optional">{optionalText}</span>

<div class="input-text-wrapper">
<input
{id}
Expand Down
1 change: 0 additions & 1 deletion src/lib/helpers/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export function at<T>(array: readonly T[] | [], index: number): T | undefined {

/**
* Get last item
* @date 12/12/2022 - 12:08:03 PM
*
* @export
* @template T
Expand Down
75 changes: 75 additions & 0 deletions src/lib/helpers/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Strict typed `Object.entries`
* Extracted from https://github.com/antfu/utils
*
* @category Object
*/
export function objectEntries<T extends object>(obj: T) {
return Object.entries(obj) as Array<[keyof T, T[keyof T]]>;
}

type KeyTypesMap<T> = Record<keyof T, Array<string> | ((v: unknown) => boolean) | string>;
/**
* Checks if the given value is an object of type T, given a map of keys and
* their types/validators.
*
* @category Object
*
* @template T
* @param {unknown} object
* @param {KeyTypesMap} keyTypesMap
* @returns {value is T}
*
* @example
* ```
* type ExampleType = {
* a: string;
* b: number;
* c: string | number;
* d: string[]
* e: {
* f: string;
* g: number;
* }
* }
*
* export const isExampleType = (value: unknown): value is ExampleType => {
* return isObjectType(value, {
* a: 'string',
* b: 'number',
* c: ['string', 'number'],
* d: (v) => Array.isArray(v) && v.every((v) => typeof v === 'string'),
* e: (v) => isObjectType(v, {
* f: 'string',
* g: 'number',
* }),
* });
* };
* ```
*/
export const isObjectType = <T>(
object: unknown,
keyTypesMap: Partial<KeyTypesMap<T>>
): object is T => {
if (typeof object !== 'object' || object === null) {
return false;
}

for (const [key, check] of objectEntries(keyTypesMap)) {
const value = (object as Record<keyof T, unknown>)[key];

if (typeof check === 'function') {
if (!check(value)) {
return false;
}
} else if (typeof check === 'string') {
if (typeof value !== check) {
return false;
}
} else if (!check.includes(typeof value)) {
return false;
}
}

return true;
};
10 changes: 10 additions & 0 deletions src/lib/helpers/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Capitalizes the first letter of a string
*
* @export
* @param {string} str
* @returns {string}
*/
export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { page } from '$app/stores';
import { PAGE_LIMIT } from '$lib/constants';
import CreateAttribute from '../createAttribute.svelte';
import { tooltip } from '$lib/actions/tooltip';

export let data: PageData;
let showCreateAttribute = false;
Expand All @@ -33,6 +34,44 @@
title: attribute.key
}))
];

function formatArray(array: any[]) {
if (array.length === 0) return '[ ]';

let formattedFields: string[] = [];
for (const item of array) {
if (typeof item === 'string') {
formattedFields.push(`"${item}"`);
} else {
formattedFields.push(`${item}`);
}
}

return `[${formattedFields.join(', ')}]`;
}

function formatColumn(column: any) {
let formattedColumn: string;

if (typeof column === 'string') {
formattedColumn = column;
} else if (Array.isArray(column)) {
formattedColumn = formatArray(column);
} else if (!column) {
formattedColumn = 'n/a';
} else {
formattedColumn = `${column}`;
}

return {
value:
formattedColumn.length > 20
? `${formattedColumn.slice(0, 20)}...`
: formattedColumn,
truncated: formattedColumn.length > 20,
whole: formattedColumn
};
}
</script>

<Container>
Expand Down Expand Up @@ -70,8 +109,15 @@
</Copy>
</TableCell>
{#each columns as column}
{@const formatted = formatColumn(document[column.key])}
<TableCell>
{document[column.key] ?? 'n/a'}
<div
use:tooltip={{
content: formatted.whole,
disabled: !formatted.truncated
}}>
{formatted.value}
</div>
</TableCell>
{/each}
</TableRowLink>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
export let id: string;
export let label: string;
export let value: string | number | boolean;
export let optionalText: string | undefined = undefined;
export let attribute:
| Models.AttributeBoolean
| Models.AttributeEmail
Expand Down Expand Up @@ -43,13 +44,15 @@
{id}
{label}
{attribute}
{optionalText}
bind:value />
{:else}
<svelte:component
this={attributesTypeMap[attribute.type]}
{id}
{label}
{attribute}
{optionalText}
bind:value />
{/if}
{/if}
Loading