@ternarytech-io/gears-ui 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +2 -1
- package/src/components/TAccentPicker.vue +41 -41
- package/src/components/TContainerPicker.vue +22 -22
- package/src/components/TDensityPicker.vue +19 -19
- package/src/components/TEditableTable.vue +203 -203
- package/src/components/TFontPicker.vue +23 -23
- package/src/components/TIconPicker.vue +109 -0
- package/src/components/TNavPositionPicker.vue +22 -22
- package/src/components/TScaledFrame.vue +36 -36
- package/src/components/TShell.vue +82 -72
- package/src/components/TSortableTree.vue +73 -73
- package/src/components/TSortableTreeList.vue +60 -60
- package/src/components/TSparkline.vue +34 -34
- package/src/components/TStatCard.vue +56 -56
- package/src/components/TThemeBasePicker.vue +22 -22
- package/src/components/sortableTree.ts +19 -19
- package/src/data/tablerIcons.ts +5 -0
- package/src/index.ts +230 -227
- package/src/plugins/charts.ts +4 -4
- package/src/styles/gears.css +21 -12
package/README.md
CHANGED
|
@@ -102,7 +102,7 @@ add a page, add an entry to `preview/registry.ts` and a folder of examples.
|
|
|
102
102
|
| Group | Components |
|
|
103
103
|
| --- | --- |
|
|
104
104
|
| Elements | `TButton`, `TBadge`, `TAvatar`, `TAlert`, `TCard`, `TIcon`, `TTooltip`, `TEmpty`, `TSpinner`, `TSkeleton`, `TProgress`, `TSeparator` |
|
|
105
|
-
| Forms | `TFormField`, `TInput`, `TInputNumber`, `TTextarea`, `TSelect`, `TSelectMenu`, `TInputTags`, `TCheckbox`, `TSwitch`, `TRadioGroup` (list, cards or tiles with a picture), `TFieldGroup`, `TFileUpload`, `TImageUpload` (one image, checked for shape and size), `TCodeEditor` |
|
|
105
|
+
| Forms | `TFormField`, `TInput`, `TInputNumber`, `TTextarea`, `TSelect`, `TSelectMenu`, `TInputTags`, `TCheckbox`, `TSwitch`, `TRadioGroup` (list, cards or tiles with a picture), `TFieldGroup`, `TFileUpload`, `TImageUpload` (one image, checked for shape and size), `TIconPicker` (any Tabler icon, by name, with a search; the list is generated by `npm run icons`), `TCodeEditor` |
|
|
106
106
|
| Theme | `TAccentPicker`, `TFontPicker`, `TRadiusPicker`, `TThemeBasePicker` (Tabler's theme settings as pickers), `TNavPositionPicker`, `TDensityPicker`, `TContainerPicker` (layout), `TThemeScope` (applies a `ThemeSettings`' look and density to what's inside it); `themeAccents`, `themeFonts`, `themeBases`, `themeRadii`, `themeNavs`, `themeDensities`, `themeContainers` |
|
|
107
107
|
| Overlays | `TModal`, `TSlideover`, `TDropdown`, `TToaster` (with `useToast()`) |
|
|
108
108
|
| Navigation | `TTabs`, `TPagination`, `TSteps`, `TNav`, `TPanelList`, `TCommandPalette`, `TSearchButton` |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ternarytech-io/gears-ui",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Gears UI: Vue 3 components on Tabler, used by the Gears Studio and the applications it builds.",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"dev": "vite",
|
|
43
43
|
"build": "vue-tsc --noEmit && vite build",
|
|
44
44
|
"typecheck": "vue-tsc --noEmit",
|
|
45
|
+
"icons": "node scripts/generate-icons.mjs",
|
|
45
46
|
"preview": "vite preview"
|
|
46
47
|
},
|
|
47
48
|
"peerDependencies": {
|
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// A theme's accent color: Tabler's named colors as swatches (its color input). With `custom`, a
|
|
3
|
-
// last swatch picks any color (#rrggbb) with the browser's color picker; it shows the custom color
|
|
4
|
-
// while one is chosen. The `custom` slot goes after the swatches, for another picker.
|
|
5
|
-
import { computed, useId } from 'vue'
|
|
6
|
-
import TIcon from './TIcon.vue'
|
|
7
|
-
import { isCustomAccent, themeAccents } from '../theme'
|
|
8
|
-
import type { ThemeAccent } from '../types'
|
|
9
|
-
|
|
10
|
-
defineProps<{ custom?: boolean; disabled?: boolean; legend?: string }>()
|
|
11
|
-
|
|
12
|
-
const model = defineModel<ThemeAccent | null | undefined>()
|
|
13
|
-
const name = useId()
|
|
14
|
-
const customColor = computed(() => isCustomAccent(model.value ?? undefined) ? model.value! : null)
|
|
15
|
-
const label = (color: string) => color[0].toUpperCase() + color.slice(1)
|
|
16
|
-
</script>
|
|
17
|
-
|
|
18
|
-
<template>
|
|
19
|
-
<div role="radiogroup" :aria-label="legend ?? 'Accent color'">
|
|
20
|
-
<div v-if="legend" class="form-label">{{ legend }}</div>
|
|
21
|
-
<div class="t-accent-swatches align-items-center">
|
|
22
|
-
<label v-for="color in themeAccents" :key="color" class="form-colorinput" :title="label(color)">
|
|
23
|
-
<input type="radio" :name="name" :value="color" class="form-colorinput-input" :checked="model === color" :disabled="disabled"
|
|
24
|
-
:aria-label="label(color)" @change="model = color">
|
|
25
|
-
<span :class="['form-colorinput-color', `bg-${color}`]" />
|
|
26
|
-
</label>
|
|
27
|
-
<label v-if="custom" :class="['form-colorinput t-accent-custom', { active: customColor }]" :title="customColor ? `Custom: ${customColor}` : 'Custom color'">
|
|
28
|
-
<input type="color" class="t-accent-custom-input" :value="customColor ?? '#3673d1'" :disabled="disabled" aria-label="Custom color"
|
|
29
|
-
@input="model = ($event.target as HTMLInputElement).value">
|
|
30
|
-
<span class="form-colorinput-color d-flex align-items-center justify-content-center" :style="customColor ? { background: customColor } : undefined">
|
|
31
|
-
<TIcon :name="customColor ? 'check' : 'color-picker'" :class="customColor ? 'text-white' : 'text-secondary'" />
|
|
32
|
-
</span>
|
|
33
|
-
</label>
|
|
34
|
-
<label v-else-if="customColor" class="form-colorinput" :title="customColor">
|
|
35
|
-
<input type="radio" :name="name" class="form-colorinput-input" checked :disabled="disabled" :aria-label="`Custom: ${customColor}`">
|
|
36
|
-
<span class="form-colorinput-color" :style="{ background: customColor }" />
|
|
37
|
-
</label>
|
|
38
|
-
<slot name="custom" />
|
|
39
|
-
</div>
|
|
40
|
-
</div>
|
|
41
|
-
</template>
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// A theme's accent color: Tabler's named colors as swatches (its color input). With `custom`, a
|
|
3
|
+
// last swatch picks any color (#rrggbb) with the browser's color picker; it shows the custom color
|
|
4
|
+
// while one is chosen. The `custom` slot goes after the swatches, for another picker.
|
|
5
|
+
import { computed, useId } from 'vue'
|
|
6
|
+
import TIcon from './TIcon.vue'
|
|
7
|
+
import { isCustomAccent, themeAccents } from '../theme'
|
|
8
|
+
import type { ThemeAccent } from '../types'
|
|
9
|
+
|
|
10
|
+
defineProps<{ custom?: boolean; disabled?: boolean; legend?: string }>()
|
|
11
|
+
|
|
12
|
+
const model = defineModel<ThemeAccent | null | undefined>()
|
|
13
|
+
const name = useId()
|
|
14
|
+
const customColor = computed(() => isCustomAccent(model.value ?? undefined) ? model.value! : null)
|
|
15
|
+
const label = (color: string) => color[0].toUpperCase() + color.slice(1)
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<template>
|
|
19
|
+
<div role="radiogroup" :aria-label="legend ?? 'Accent color'">
|
|
20
|
+
<div v-if="legend" class="form-label">{{ legend }}</div>
|
|
21
|
+
<div class="t-accent-swatches align-items-center">
|
|
22
|
+
<label v-for="color in themeAccents" :key="color" class="form-colorinput" :title="label(color)">
|
|
23
|
+
<input type="radio" :name="name" :value="color" class="form-colorinput-input" :checked="model === color" :disabled="disabled"
|
|
24
|
+
:aria-label="label(color)" @change="model = color">
|
|
25
|
+
<span :class="['form-colorinput-color', `bg-${color}`]" />
|
|
26
|
+
</label>
|
|
27
|
+
<label v-if="custom" :class="['form-colorinput t-accent-custom', { active: customColor }]" :title="customColor ? `Custom: ${customColor}` : 'Custom color'">
|
|
28
|
+
<input type="color" class="t-accent-custom-input" :value="customColor ?? '#3673d1'" :disabled="disabled" aria-label="Custom color"
|
|
29
|
+
@input="model = ($event.target as HTMLInputElement).value">
|
|
30
|
+
<span class="form-colorinput-color d-flex align-items-center justify-content-center" :style="customColor ? { background: customColor } : undefined">
|
|
31
|
+
<TIcon :name="customColor ? 'check' : 'color-picker'" :class="customColor ? 'text-white' : 'text-secondary'" />
|
|
32
|
+
</span>
|
|
33
|
+
</label>
|
|
34
|
+
<label v-else-if="customColor" class="form-colorinput" :title="customColor">
|
|
35
|
+
<input type="radio" :name="name" class="form-colorinput-input" checked :disabled="disabled" :aria-label="`Custom: ${customColor}`">
|
|
36
|
+
<span class="form-colorinput-color" :style="{ background: customColor }" />
|
|
37
|
+
</label>
|
|
38
|
+
<slot name="custom" />
|
|
39
|
+
</div>
|
|
40
|
+
</div>
|
|
41
|
+
</template>
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// Whether an application's page sits in a box, centered on wide screens with the background
|
|
3
|
-
// around it, or fills the width. Each tile is a sketch of the page.
|
|
4
|
-
import TRadioGroup from './TRadioGroup.vue'
|
|
5
|
-
import { themeContainers } from '../theme'
|
|
6
|
-
import type { ThemeContainer } from '../types'
|
|
7
|
-
|
|
8
|
-
defineProps<{ disabled?: boolean; legend?: string }>()
|
|
9
|
-
|
|
10
|
-
const model = defineModel<ThemeContainer | null | undefined>()
|
|
11
|
-
</script>
|
|
12
|
-
|
|
13
|
-
<template>
|
|
14
|
-
<TRadioGroup v-model="model" :items="themeContainers" variant="tiles" tile-width="8rem" :disabled="disabled" :legend="legend">
|
|
15
|
-
<template #preview="{ item }">
|
|
16
|
-
<span :class="['t-sketch t-sketch-left', { 't-sketch-boxed': item.value === 'boxed' }]">
|
|
17
|
-
<span class="t-sketch-nav" />
|
|
18
|
-
<span class="t-sketch-page"><span /><span /><span /></span>
|
|
19
|
-
</span>
|
|
20
|
-
</template>
|
|
21
|
-
</TRadioGroup>
|
|
22
|
-
</template>
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// Whether an application's page sits in a box, centered on wide screens with the background
|
|
3
|
+
// around it, or fills the width. Each tile is a sketch of the page.
|
|
4
|
+
import TRadioGroup from './TRadioGroup.vue'
|
|
5
|
+
import { themeContainers } from '../theme'
|
|
6
|
+
import type { ThemeContainer } from '../types'
|
|
7
|
+
|
|
8
|
+
defineProps<{ disabled?: boolean; legend?: string }>()
|
|
9
|
+
|
|
10
|
+
const model = defineModel<ThemeContainer | null | undefined>()
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<template>
|
|
14
|
+
<TRadioGroup v-model="model" :items="themeContainers" variant="tiles" tile-width="8rem" :disabled="disabled" :legend="legend">
|
|
15
|
+
<template #preview="{ item }">
|
|
16
|
+
<span :class="['t-sketch t-sketch-left', { 't-sketch-boxed': item.value === 'boxed' }]">
|
|
17
|
+
<span class="t-sketch-nav" />
|
|
18
|
+
<span class="t-sketch-page"><span /><span /><span /></span>
|
|
19
|
+
</span>
|
|
20
|
+
</template>
|
|
21
|
+
</TRadioGroup>
|
|
22
|
+
</template>
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// How much room an application's content gets: padding inside cards, tables, buttons and fields,
|
|
3
|
-
// and the gaps between them. Each tile shows rows at that spacing.
|
|
4
|
-
import TRadioGroup from './TRadioGroup.vue'
|
|
5
|
-
import { themeDensities } from '../theme'
|
|
6
|
-
import type { ThemeDensity } from '../types'
|
|
7
|
-
|
|
8
|
-
defineProps<{ disabled?: boolean; legend?: string }>()
|
|
9
|
-
|
|
10
|
-
const model = defineModel<ThemeDensity | null | undefined>()
|
|
11
|
-
</script>
|
|
12
|
-
|
|
13
|
-
<template>
|
|
14
|
-
<TRadioGroup v-model="model" :items="themeDensities" variant="tiles" tile-width="8rem" :disabled="disabled" :legend="legend">
|
|
15
|
-
<template #preview="{ item }">
|
|
16
|
-
<span :class="['t-sketch-rows', `t-sketch-${item.value}`]"><span /><span /><span /><span /></span>
|
|
17
|
-
</template>
|
|
18
|
-
</TRadioGroup>
|
|
19
|
-
</template>
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// How much room an application's content gets: padding inside cards, tables, buttons and fields,
|
|
3
|
+
// and the gaps between them. Each tile shows rows at that spacing.
|
|
4
|
+
import TRadioGroup from './TRadioGroup.vue'
|
|
5
|
+
import { themeDensities } from '../theme'
|
|
6
|
+
import type { ThemeDensity } from '../types'
|
|
7
|
+
|
|
8
|
+
defineProps<{ disabled?: boolean; legend?: string }>()
|
|
9
|
+
|
|
10
|
+
const model = defineModel<ThemeDensity | null | undefined>()
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<template>
|
|
14
|
+
<TRadioGroup v-model="model" :items="themeDensities" variant="tiles" tile-width="8rem" :disabled="disabled" :legend="legend">
|
|
15
|
+
<template #preview="{ item }">
|
|
16
|
+
<span :class="['t-sketch-rows', `t-sketch-${item.value}`]"><span /><span /><span /><span /></span>
|
|
17
|
+
</template>
|
|
18
|
+
</TRadioGroup>
|
|
19
|
+
</template>
|
|
@@ -1,203 +1,203 @@
|
|
|
1
|
-
<script setup lang="ts" generic="Row extends Record<string, any>">
|
|
2
|
-
// An editable table: TTable with a toolbar (a title, search, and actions), pages, and per-column kinds
|
|
3
|
-
// (text, number, date, yes/no) that decide how cells are written and aligned. `selectable` rows
|
|
4
|
-
// show how many are chosen and the `bulk-actions` slot in the footer, so the table doesn't move.
|
|
5
|
-
// `truncate` cuts a column's long values to one line (the whole value on hover). When `editable`, a
|
|
6
|
-
// column marked `editable` is changed in place: click anywhere in the cell, then Enter (or leave
|
|
7
|
-
// it) to keep the change, Esc to drop it; each change emits `edit` for the caller to save. By
|
|
8
|
-
// default the table searches, sorts and pages
|
|
9
|
-
// the rows it has; `manual` leaves that to the caller (the rows are one page of `total`), who
|
|
10
|
-
// watches v-model:search, v-model:sort and v-model:page.
|
|
11
|
-
import { computed, nextTick, ref, watch } from 'vue'
|
|
12
|
-
import TButton from './TButton.vue'
|
|
13
|
-
import TCard from './TCard.vue'
|
|
14
|
-
import TCheckbox from './TCheckbox.vue'
|
|
15
|
-
import TInput from './TInput.vue'
|
|
16
|
-
import TPagination from './TPagination.vue'
|
|
17
|
-
import TTable from './TTable.vue'
|
|
18
|
-
import type { EditableTableColumn, TableEdit, TableColumn, TableSort } from '../types'
|
|
19
|
-
|
|
20
|
-
const props = withDefaults(defineProps<{
|
|
21
|
-
columns: EditableTableColumn<Row>[]
|
|
22
|
-
rows: Row[]
|
|
23
|
-
rowKey?: string | ((row: Row) => string | number)
|
|
24
|
-
title?: string
|
|
25
|
-
subtitle?: string
|
|
26
|
-
searchable?: boolean
|
|
27
|
-
// Rows per page; 0 shows them all.
|
|
28
|
-
pageSize?: number
|
|
29
|
-
selectable?: boolean
|
|
30
|
-
editable?: boolean
|
|
31
|
-
manual?: boolean
|
|
32
|
-
// The rows there are, for `manual` grids.
|
|
33
|
-
total?: number
|
|
34
|
-
loading?: boolean
|
|
35
|
-
empty?: string
|
|
36
|
-
striped?: boolean
|
|
37
|
-
// Rows can be clicked; each click emits `select`.
|
|
38
|
-
clickable?: boolean
|
|
39
|
-
}>(), { rowKey: 'id', searchable: true, pageSize: 25, empty: 'Nothing to show.' })
|
|
40
|
-
|
|
41
|
-
const emit = defineEmits<{ edit: [edit: TableEdit<Row>]; select: [row: Row] }>()
|
|
42
|
-
const search = defineModel<string>('search', { default: '' })
|
|
43
|
-
const sort = defineModel<TableSort | null>('sort', { default: null })
|
|
44
|
-
const page = defineModel<number>('page', { default: 1 })
|
|
45
|
-
const selected = defineModel<(string | number)[]>('selected', { default: () => [] })
|
|
46
|
-
|
|
47
|
-
const value = (row: Row, column: EditableTableColumn<Row>) => column.value ? column.value(row) : row[column.key]
|
|
48
|
-
const keyOf = (row: Row) => typeof props.rowKey === 'function' ? props.rowKey(row) : row[props.rowKey]
|
|
49
|
-
|
|
50
|
-
const numberFormat = new Intl.NumberFormat(undefined, { maximumFractionDigits: 2 })
|
|
51
|
-
const dateFormat = new Intl.DateTimeFormat(undefined, { dateStyle: 'medium' })
|
|
52
|
-
const dateTimeFormat = new Intl.DateTimeFormat(undefined, { dateStyle: 'medium', timeStyle: 'short' })
|
|
53
|
-
/** A cell as text: the column's format, else by its kind. */
|
|
54
|
-
function text(row: Row, column: EditableTableColumn<Row>): string {
|
|
55
|
-
const v = value(row, column)
|
|
56
|
-
if (column.format) return column.format(v, row)
|
|
57
|
-
if (v == null || v === '') return '—'
|
|
58
|
-
switch (column.type) {
|
|
59
|
-
case 'number': return typeof v === 'number' || (typeof v === 'string' && v.trim() !== '' && Number.isFinite(Number(v))) ? numberFormat.format(Number(v)) : String(v)
|
|
60
|
-
case 'boolean': return v ? 'Yes' : 'No'
|
|
61
|
-
case 'date': {
|
|
62
|
-
const d = v instanceof Date ? v : new Date(String(v))
|
|
63
|
-
if (Number.isNaN(d.getTime())) return String(v)
|
|
64
|
-
// A date alone (2026-11-01) has no time to show.
|
|
65
|
-
return typeof v === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(v) ? dateFormat.format(d) : dateTimeFormat.format(d)
|
|
66
|
-
}
|
|
67
|
-
default: return typeof v === 'boolean' ? (v ? 'Yes' : 'No') : String(v)
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// TTable's columns: numbers to the right, sortable unless said otherwise.
|
|
72
|
-
const tableColumns = computed<TableColumn<Row>[]>(() => props.columns.map(c => ({
|
|
73
|
-
...c, align: c.align ?? (c.type === 'number' ? 'end' : undefined), sortable: c.sortable ?? true
|
|
74
|
-
})))
|
|
75
|
-
|
|
76
|
-
// ---- Search, sort and pages (unless manual) ----
|
|
77
|
-
const matching = computed(() => {
|
|
78
|
-
const term = search.value.trim().toLowerCase()
|
|
79
|
-
if (props.manual || !term) return props.rows
|
|
80
|
-
return props.rows.filter(row => props.columns.some(c => text(row, c).toLowerCase().includes(term)))
|
|
81
|
-
})
|
|
82
|
-
const sorted = computed(() => {
|
|
83
|
-
const by = sort.value
|
|
84
|
-
const column = by && props.columns.find(c => c.key === by.key)
|
|
85
|
-
if (props.manual || !by || !column) return matching.value
|
|
86
|
-
const factor = by.direction === 'asc' ? 1 : -1
|
|
87
|
-
return [...matching.value].sort((a, b) => {
|
|
88
|
-
const x = value(a, column) as any
|
|
89
|
-
const y = value(b, column) as any
|
|
90
|
-
if (x == null) return y == null ? 0 : 1
|
|
91
|
-
if (y == null) return -1
|
|
92
|
-
const numeric = column.type === 'number' || (typeof x === 'number' && typeof y === 'number')
|
|
93
|
-
return (numeric ? Number(x) - Number(y) : String(x).localeCompare(String(y), undefined, { numeric: true })) * factor
|
|
94
|
-
})
|
|
95
|
-
})
|
|
96
|
-
const count = computed(() => props.manual ? props.total ?? props.rows.length : sorted.value.length)
|
|
97
|
-
const shown = computed(() => {
|
|
98
|
-
if (props.manual || !props.pageSize) return sorted.value
|
|
99
|
-
const start = (page.value - 1) * props.pageSize
|
|
100
|
-
return sorted.value.slice(start, start + props.pageSize)
|
|
101
|
-
})
|
|
102
|
-
// A new search starts at the first page; a page past the end moves back.
|
|
103
|
-
watch(search, () => { page.value = 1 })
|
|
104
|
-
watch(count, total => {
|
|
105
|
-
const last = Math.max(1, Math.ceil(total / (props.pageSize || total || 1)))
|
|
106
|
-
if (page.value > last) page.value = last
|
|
107
|
-
})
|
|
108
|
-
const range = computed(() => {
|
|
109
|
-
if (!count.value) return ''
|
|
110
|
-
if (!props.pageSize) return `${count.value} row${count.value === 1 ? '' : 's'}`
|
|
111
|
-
const from = (page.value - 1) * props.pageSize + 1
|
|
112
|
-
return `${from}–${Math.min(from + props.pageSize - 1, count.value)} of ${count.value}`
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
// ---- Editing in place ----
|
|
116
|
-
const editing = ref<{ key: string | number; column: string } | null>(null)
|
|
117
|
-
const draft = ref<string | boolean>('')
|
|
118
|
-
const editor = ref<{ $el?: HTMLElement } | null>(null)
|
|
119
|
-
const canEdit = (column: EditableTableColumn<Row>) => !!(props.editable && column.editable)
|
|
120
|
-
|
|
121
|
-
async function startEdit(row: Row, column: EditableTableColumn<Row>) {
|
|
122
|
-
if (!canEdit(column)) return
|
|
123
|
-
const v = value(row, column)
|
|
124
|
-
draft.value = column.type === 'boolean' ? !!v : v == null ? '' : column.type === 'date' ? String(v).slice(0, 10) : String(v)
|
|
125
|
-
editing.value = { key: keyOf(row), column: column.key }
|
|
126
|
-
await nextTick()
|
|
127
|
-
editor.value?.$el?.querySelector?.('input')?.focus()
|
|
128
|
-
}
|
|
129
|
-
const isEditing = (row: Row, column: EditableTableColumn<Row>) => editing.value?.key === keyOf(row) && editing.value?.column === column.key
|
|
130
|
-
|
|
131
|
-
function commit(row: Row, column: EditableTableColumn<Row>) {
|
|
132
|
-
if (!isEditing(row, column)) return
|
|
133
|
-
editing.value = null
|
|
134
|
-
const before = value(row, column)
|
|
135
|
-
const raw = draft.value
|
|
136
|
-
const next = column.type === 'number' ? (raw === '' ? null : Number(raw)) : column.type === 'boolean' ? raw === true : raw === '' ? null : raw
|
|
137
|
-
if (column.type === 'number' && next !== null && !Number.isFinite(next)) return
|
|
138
|
-
if (next === before || (next == null && (before == null || before === ''))) return
|
|
139
|
-
emit('edit', { row, key: column.key, value: next })
|
|
140
|
-
}
|
|
141
|
-
function cancel() { editing.value = null }
|
|
142
|
-
|
|
143
|
-
const inputType = (column: EditableTableColumn<Row>) => column.type === 'number' ? 'number' : column.type === 'date' ? 'date' : 'text'
|
|
144
|
-
</script>
|
|
145
|
-
|
|
146
|
-
<template>
|
|
147
|
-
<TCard flush class="t-editable-table mb-0">
|
|
148
|
-
<template v-if="title || searchable || $slots.actions" #header>
|
|
149
|
-
<div class="d-flex flex-wrap align-items-center gap-2 w-100">
|
|
150
|
-
<div v-if="title" class="me-auto min-w-0">
|
|
151
|
-
<h3 class="card-title text-truncate">{{ title }}</h3>
|
|
152
|
-
<div v-if="subtitle" class="card-subtitle small text-secondary mb-0">{{ subtitle }}</div>
|
|
153
|
-
</div>
|
|
154
|
-
<TInput v-if="searchable" v-model="search" icon="search" size="sm" placeholder="Search…" aria-label="Search the rows"
|
|
155
|
-
:class="['t-editable-table-search', { 'ms-auto': !title }]" />
|
|
156
|
-
<slot name="actions" />
|
|
157
|
-
</div>
|
|
158
|
-
</template>
|
|
159
|
-
|
|
160
|
-
<TTable v-model:sort="sort" v-model:selected="selected" :columns="tableColumns" :rows="shown" :row-key="rowKey" :loading="loading"
|
|
161
|
-
:empty="search && rows.length ? 'No rows match the search.' : empty" card :striped="striped" :selectable="selectable" :clickable="clickable"
|
|
162
|
-
manual-sort :cell-clickable="(row, column) => canEdit(column as EditableTableColumn<Row>) && !isEditing(row, column as EditableTableColumn<Row>)"
|
|
163
|
-
@select="row => emit('select', row)" @cell-click="(row, column) => startEdit(row, column as EditableTableColumn<Row>)">
|
|
164
|
-
<template #cell="{ row, column }">
|
|
165
|
-
<slot :name="`${column.key}-cell`" :row="row" :value="value(row, column as EditableTableColumn<Row>)">
|
|
166
|
-
<div v-if="isEditing(row, column as EditableTableColumn<Row>)" class="t-editable-table-editor" @click.stop>
|
|
167
|
-
<TCheckbox v-if="(column as EditableTableColumn<Row>).type === 'boolean'" ref="editor" v-model="draft as boolean" class="mb-0"
|
|
168
|
-
:label="column.label ?? column.key" @update:model-value="commit(row, column as EditableTableColumn<Row>)" />
|
|
169
|
-
<TInput v-else ref="editor" v-model="draft as string" size="sm" :type="inputType(column as EditableTableColumn<Row>)" :aria-label="column.label ?? column.key"
|
|
170
|
-
@keydown.enter.prevent="commit(row, column as EditableTableColumn<Row>)" @keydown.esc.prevent="cancel"
|
|
171
|
-
@blur="commit(row, column as EditableTableColumn<Row>)" />
|
|
172
|
-
</div>
|
|
173
|
-
<span v-else-if="canEdit(column as EditableTableColumn<Row>)" role="button" tabindex="0" class="t-editable-table-editable"
|
|
174
|
-
:class="{ 'text-truncate d-inline-block align-middle': (column as EditableTableColumn<Row>).truncate }"
|
|
175
|
-
:style="(column as EditableTableColumn<Row>).truncate ? { maxWidth: (column as EditableTableColumn<Row>).truncate } : undefined"
|
|
176
|
-
:title="`${text(row, column as EditableTableColumn<Row>)} (click to edit)`"
|
|
177
|
-
@keydown.enter.prevent="startEdit(row, column as EditableTableColumn<Row>)">
|
|
178
|
-
{{ text(row, column as EditableTableColumn<Row>) }}
|
|
179
|
-
</span>
|
|
180
|
-
<span v-else-if="(column as EditableTableColumn<Row>).truncate" class="text-truncate d-inline-block align-middle"
|
|
181
|
-
:style="{ maxWidth: (column as EditableTableColumn<Row>).truncate }" :title="text(row, column as EditableTableColumn<Row>)">
|
|
182
|
-
{{ text(row, column as EditableTableColumn<Row>) }}
|
|
183
|
-
</span>
|
|
184
|
-
<template v-else>{{ text(row, column as EditableTableColumn<Row>) }}</template>
|
|
185
|
-
</slot>
|
|
186
|
-
</template>
|
|
187
|
-
<template v-if="$slots.empty" #empty><slot name="empty" /></template>
|
|
188
|
-
</TTable>
|
|
189
|
-
|
|
190
|
-
<!-- The selection and pages; with selectable rows it's always there, so choosing rows moves nothing. -->
|
|
191
|
-
<template v-if="selectable || (pageSize && count > 0)" #footer>
|
|
192
|
-
<div class="d-flex flex-wrap align-items-center gap-2 t-editable-table-footer">
|
|
193
|
-
<template v-if="selectable && selected.length">
|
|
194
|
-
<span class="small fw-medium">{{ selected.length }} selected</span>
|
|
195
|
-
<TButton size="xs" color="neutral" variant="ghost" label="Clear" @click="selected = []" />
|
|
196
|
-
<slot name="bulk-actions" :selected="selected" />
|
|
197
|
-
</template>
|
|
198
|
-
<span v-else class="small text-secondary">{{ range || (selectable ? 'Select rows for more actions.' : '') }}</span>
|
|
199
|
-
<TPagination v-if="pageSize && count > pageSize" v-model:page="page" :total="count" :items-per-page="pageSize" size="sm" class="ms-auto" />
|
|
200
|
-
</div>
|
|
201
|
-
</template>
|
|
202
|
-
</TCard>
|
|
203
|
-
</template>
|
|
1
|
+
<script setup lang="ts" generic="Row extends Record<string, any>">
|
|
2
|
+
// An editable table: TTable with a toolbar (a title, search, and actions), pages, and per-column kinds
|
|
3
|
+
// (text, number, date, yes/no) that decide how cells are written and aligned. `selectable` rows
|
|
4
|
+
// show how many are chosen and the `bulk-actions` slot in the footer, so the table doesn't move.
|
|
5
|
+
// `truncate` cuts a column's long values to one line (the whole value on hover). When `editable`, a
|
|
6
|
+
// column marked `editable` is changed in place: click anywhere in the cell, then Enter (or leave
|
|
7
|
+
// it) to keep the change, Esc to drop it; each change emits `edit` for the caller to save. By
|
|
8
|
+
// default the table searches, sorts and pages
|
|
9
|
+
// the rows it has; `manual` leaves that to the caller (the rows are one page of `total`), who
|
|
10
|
+
// watches v-model:search, v-model:sort and v-model:page.
|
|
11
|
+
import { computed, nextTick, ref, watch } from 'vue'
|
|
12
|
+
import TButton from './TButton.vue'
|
|
13
|
+
import TCard from './TCard.vue'
|
|
14
|
+
import TCheckbox from './TCheckbox.vue'
|
|
15
|
+
import TInput from './TInput.vue'
|
|
16
|
+
import TPagination from './TPagination.vue'
|
|
17
|
+
import TTable from './TTable.vue'
|
|
18
|
+
import type { EditableTableColumn, TableEdit, TableColumn, TableSort } from '../types'
|
|
19
|
+
|
|
20
|
+
const props = withDefaults(defineProps<{
|
|
21
|
+
columns: EditableTableColumn<Row>[]
|
|
22
|
+
rows: Row[]
|
|
23
|
+
rowKey?: string | ((row: Row) => string | number)
|
|
24
|
+
title?: string
|
|
25
|
+
subtitle?: string
|
|
26
|
+
searchable?: boolean
|
|
27
|
+
// Rows per page; 0 shows them all.
|
|
28
|
+
pageSize?: number
|
|
29
|
+
selectable?: boolean
|
|
30
|
+
editable?: boolean
|
|
31
|
+
manual?: boolean
|
|
32
|
+
// The rows there are, for `manual` grids.
|
|
33
|
+
total?: number
|
|
34
|
+
loading?: boolean
|
|
35
|
+
empty?: string
|
|
36
|
+
striped?: boolean
|
|
37
|
+
// Rows can be clicked; each click emits `select`.
|
|
38
|
+
clickable?: boolean
|
|
39
|
+
}>(), { rowKey: 'id', searchable: true, pageSize: 25, empty: 'Nothing to show.' })
|
|
40
|
+
|
|
41
|
+
const emit = defineEmits<{ edit: [edit: TableEdit<Row>]; select: [row: Row] }>()
|
|
42
|
+
const search = defineModel<string>('search', { default: '' })
|
|
43
|
+
const sort = defineModel<TableSort | null>('sort', { default: null })
|
|
44
|
+
const page = defineModel<number>('page', { default: 1 })
|
|
45
|
+
const selected = defineModel<(string | number)[]>('selected', { default: () => [] })
|
|
46
|
+
|
|
47
|
+
const value = (row: Row, column: EditableTableColumn<Row>) => column.value ? column.value(row) : row[column.key]
|
|
48
|
+
const keyOf = (row: Row) => typeof props.rowKey === 'function' ? props.rowKey(row) : row[props.rowKey]
|
|
49
|
+
|
|
50
|
+
const numberFormat = new Intl.NumberFormat(undefined, { maximumFractionDigits: 2 })
|
|
51
|
+
const dateFormat = new Intl.DateTimeFormat(undefined, { dateStyle: 'medium' })
|
|
52
|
+
const dateTimeFormat = new Intl.DateTimeFormat(undefined, { dateStyle: 'medium', timeStyle: 'short' })
|
|
53
|
+
/** A cell as text: the column's format, else by its kind. */
|
|
54
|
+
function text(row: Row, column: EditableTableColumn<Row>): string {
|
|
55
|
+
const v = value(row, column)
|
|
56
|
+
if (column.format) return column.format(v, row)
|
|
57
|
+
if (v == null || v === '') return '—'
|
|
58
|
+
switch (column.type) {
|
|
59
|
+
case 'number': return typeof v === 'number' || (typeof v === 'string' && v.trim() !== '' && Number.isFinite(Number(v))) ? numberFormat.format(Number(v)) : String(v)
|
|
60
|
+
case 'boolean': return v ? 'Yes' : 'No'
|
|
61
|
+
case 'date': {
|
|
62
|
+
const d = v instanceof Date ? v : new Date(String(v))
|
|
63
|
+
if (Number.isNaN(d.getTime())) return String(v)
|
|
64
|
+
// A date alone (2026-11-01) has no time to show.
|
|
65
|
+
return typeof v === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(v) ? dateFormat.format(d) : dateTimeFormat.format(d)
|
|
66
|
+
}
|
|
67
|
+
default: return typeof v === 'boolean' ? (v ? 'Yes' : 'No') : String(v)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// TTable's columns: numbers to the right, sortable unless said otherwise.
|
|
72
|
+
const tableColumns = computed<TableColumn<Row>[]>(() => props.columns.map(c => ({
|
|
73
|
+
...c, align: c.align ?? (c.type === 'number' ? 'end' : undefined), sortable: c.sortable ?? true
|
|
74
|
+
})))
|
|
75
|
+
|
|
76
|
+
// ---- Search, sort and pages (unless manual) ----
|
|
77
|
+
const matching = computed(() => {
|
|
78
|
+
const term = search.value.trim().toLowerCase()
|
|
79
|
+
if (props.manual || !term) return props.rows
|
|
80
|
+
return props.rows.filter(row => props.columns.some(c => text(row, c).toLowerCase().includes(term)))
|
|
81
|
+
})
|
|
82
|
+
const sorted = computed(() => {
|
|
83
|
+
const by = sort.value
|
|
84
|
+
const column = by && props.columns.find(c => c.key === by.key)
|
|
85
|
+
if (props.manual || !by || !column) return matching.value
|
|
86
|
+
const factor = by.direction === 'asc' ? 1 : -1
|
|
87
|
+
return [...matching.value].sort((a, b) => {
|
|
88
|
+
const x = value(a, column) as any
|
|
89
|
+
const y = value(b, column) as any
|
|
90
|
+
if (x == null) return y == null ? 0 : 1
|
|
91
|
+
if (y == null) return -1
|
|
92
|
+
const numeric = column.type === 'number' || (typeof x === 'number' && typeof y === 'number')
|
|
93
|
+
return (numeric ? Number(x) - Number(y) : String(x).localeCompare(String(y), undefined, { numeric: true })) * factor
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
const count = computed(() => props.manual ? props.total ?? props.rows.length : sorted.value.length)
|
|
97
|
+
const shown = computed(() => {
|
|
98
|
+
if (props.manual || !props.pageSize) return sorted.value
|
|
99
|
+
const start = (page.value - 1) * props.pageSize
|
|
100
|
+
return sorted.value.slice(start, start + props.pageSize)
|
|
101
|
+
})
|
|
102
|
+
// A new search starts at the first page; a page past the end moves back.
|
|
103
|
+
watch(search, () => { page.value = 1 })
|
|
104
|
+
watch(count, total => {
|
|
105
|
+
const last = Math.max(1, Math.ceil(total / (props.pageSize || total || 1)))
|
|
106
|
+
if (page.value > last) page.value = last
|
|
107
|
+
})
|
|
108
|
+
const range = computed(() => {
|
|
109
|
+
if (!count.value) return ''
|
|
110
|
+
if (!props.pageSize) return `${count.value} row${count.value === 1 ? '' : 's'}`
|
|
111
|
+
const from = (page.value - 1) * props.pageSize + 1
|
|
112
|
+
return `${from}–${Math.min(from + props.pageSize - 1, count.value)} of ${count.value}`
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
// ---- Editing in place ----
|
|
116
|
+
const editing = ref<{ key: string | number; column: string } | null>(null)
|
|
117
|
+
const draft = ref<string | boolean>('')
|
|
118
|
+
const editor = ref<{ $el?: HTMLElement } | null>(null)
|
|
119
|
+
const canEdit = (column: EditableTableColumn<Row>) => !!(props.editable && column.editable)
|
|
120
|
+
|
|
121
|
+
async function startEdit(row: Row, column: EditableTableColumn<Row>) {
|
|
122
|
+
if (!canEdit(column)) return
|
|
123
|
+
const v = value(row, column)
|
|
124
|
+
draft.value = column.type === 'boolean' ? !!v : v == null ? '' : column.type === 'date' ? String(v).slice(0, 10) : String(v)
|
|
125
|
+
editing.value = { key: keyOf(row), column: column.key }
|
|
126
|
+
await nextTick()
|
|
127
|
+
editor.value?.$el?.querySelector?.('input')?.focus()
|
|
128
|
+
}
|
|
129
|
+
const isEditing = (row: Row, column: EditableTableColumn<Row>) => editing.value?.key === keyOf(row) && editing.value?.column === column.key
|
|
130
|
+
|
|
131
|
+
function commit(row: Row, column: EditableTableColumn<Row>) {
|
|
132
|
+
if (!isEditing(row, column)) return
|
|
133
|
+
editing.value = null
|
|
134
|
+
const before = value(row, column)
|
|
135
|
+
const raw = draft.value
|
|
136
|
+
const next = column.type === 'number' ? (raw === '' ? null : Number(raw)) : column.type === 'boolean' ? raw === true : raw === '' ? null : raw
|
|
137
|
+
if (column.type === 'number' && next !== null && !Number.isFinite(next)) return
|
|
138
|
+
if (next === before || (next == null && (before == null || before === ''))) return
|
|
139
|
+
emit('edit', { row, key: column.key, value: next })
|
|
140
|
+
}
|
|
141
|
+
function cancel() { editing.value = null }
|
|
142
|
+
|
|
143
|
+
const inputType = (column: EditableTableColumn<Row>) => column.type === 'number' ? 'number' : column.type === 'date' ? 'date' : 'text'
|
|
144
|
+
</script>
|
|
145
|
+
|
|
146
|
+
<template>
|
|
147
|
+
<TCard flush class="t-editable-table mb-0">
|
|
148
|
+
<template v-if="title || searchable || $slots.actions" #header>
|
|
149
|
+
<div class="d-flex flex-wrap align-items-center gap-2 w-100">
|
|
150
|
+
<div v-if="title" class="me-auto min-w-0">
|
|
151
|
+
<h3 class="card-title text-truncate">{{ title }}</h3>
|
|
152
|
+
<div v-if="subtitle" class="card-subtitle small text-secondary mb-0">{{ subtitle }}</div>
|
|
153
|
+
</div>
|
|
154
|
+
<TInput v-if="searchable" v-model="search" icon="search" size="sm" placeholder="Search…" aria-label="Search the rows"
|
|
155
|
+
:class="['t-editable-table-search', { 'ms-auto': !title }]" />
|
|
156
|
+
<slot name="actions" />
|
|
157
|
+
</div>
|
|
158
|
+
</template>
|
|
159
|
+
|
|
160
|
+
<TTable v-model:sort="sort" v-model:selected="selected" :columns="tableColumns" :rows="shown" :row-key="rowKey" :loading="loading"
|
|
161
|
+
:empty="search && rows.length ? 'No rows match the search.' : empty" card :striped="striped" :selectable="selectable" :clickable="clickable"
|
|
162
|
+
manual-sort :cell-clickable="(row, column) => canEdit(column as EditableTableColumn<Row>) && !isEditing(row, column as EditableTableColumn<Row>)"
|
|
163
|
+
@select="row => emit('select', row)" @cell-click="(row, column) => startEdit(row, column as EditableTableColumn<Row>)">
|
|
164
|
+
<template #cell="{ row, column }">
|
|
165
|
+
<slot :name="`${column.key}-cell`" :row="row" :value="value(row, column as EditableTableColumn<Row>)">
|
|
166
|
+
<div v-if="isEditing(row, column as EditableTableColumn<Row>)" class="t-editable-table-editor" @click.stop>
|
|
167
|
+
<TCheckbox v-if="(column as EditableTableColumn<Row>).type === 'boolean'" ref="editor" v-model="draft as boolean" class="mb-0"
|
|
168
|
+
:label="column.label ?? column.key" @update:model-value="commit(row, column as EditableTableColumn<Row>)" />
|
|
169
|
+
<TInput v-else ref="editor" v-model="draft as string" size="sm" :type="inputType(column as EditableTableColumn<Row>)" :aria-label="column.label ?? column.key"
|
|
170
|
+
@keydown.enter.prevent="commit(row, column as EditableTableColumn<Row>)" @keydown.esc.prevent="cancel"
|
|
171
|
+
@blur="commit(row, column as EditableTableColumn<Row>)" />
|
|
172
|
+
</div>
|
|
173
|
+
<span v-else-if="canEdit(column as EditableTableColumn<Row>)" role="button" tabindex="0" class="t-editable-table-editable"
|
|
174
|
+
:class="{ 'text-truncate d-inline-block align-middle': (column as EditableTableColumn<Row>).truncate }"
|
|
175
|
+
:style="(column as EditableTableColumn<Row>).truncate ? { maxWidth: (column as EditableTableColumn<Row>).truncate } : undefined"
|
|
176
|
+
:title="`${text(row, column as EditableTableColumn<Row>)} (click to edit)`"
|
|
177
|
+
@keydown.enter.prevent="startEdit(row, column as EditableTableColumn<Row>)">
|
|
178
|
+
{{ text(row, column as EditableTableColumn<Row>) }}
|
|
179
|
+
</span>
|
|
180
|
+
<span v-else-if="(column as EditableTableColumn<Row>).truncate" class="text-truncate d-inline-block align-middle"
|
|
181
|
+
:style="{ maxWidth: (column as EditableTableColumn<Row>).truncate }" :title="text(row, column as EditableTableColumn<Row>)">
|
|
182
|
+
{{ text(row, column as EditableTableColumn<Row>) }}
|
|
183
|
+
</span>
|
|
184
|
+
<template v-else>{{ text(row, column as EditableTableColumn<Row>) }}</template>
|
|
185
|
+
</slot>
|
|
186
|
+
</template>
|
|
187
|
+
<template v-if="$slots.empty" #empty><slot name="empty" /></template>
|
|
188
|
+
</TTable>
|
|
189
|
+
|
|
190
|
+
<!-- The selection and pages; with selectable rows it's always there, so choosing rows moves nothing. -->
|
|
191
|
+
<template v-if="selectable || (pageSize && count > 0)" #footer>
|
|
192
|
+
<div class="d-flex flex-wrap align-items-center gap-2 t-editable-table-footer">
|
|
193
|
+
<template v-if="selectable && selected.length">
|
|
194
|
+
<span class="small fw-medium">{{ selected.length }} selected</span>
|
|
195
|
+
<TButton size="xs" color="neutral" variant="ghost" label="Clear" @click="selected = []" />
|
|
196
|
+
<slot name="bulk-actions" :selected="selected" />
|
|
197
|
+
</template>
|
|
198
|
+
<span v-else class="small text-secondary">{{ range || (selectable ? 'Select rows for more actions.' : '') }}</span>
|
|
199
|
+
<TPagination v-if="pageSize && count > pageSize" v-model:page="page" :total="count" :items-per-page="pageSize" size="sm" class="ms-auto" />
|
|
200
|
+
</div>
|
|
201
|
+
</template>
|
|
202
|
+
</TCard>
|
|
203
|
+
</template>
|