@ternarytech-io/gears-ui 0.3.0 → 0.3.1
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/package.json +113 -113
- 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/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/index.ts +227 -227
- package/src/plugins/charts.ts +4 -4
- package/src/styles/gears.css +18 -12
|
@@ -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>
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// A theme's font, as tiles showing "Aa" in each typeface. `fonts` limits the choices; Comic is left
|
|
3
|
-
// out unless asked for.
|
|
4
|
-
import { computed } from 'vue'
|
|
5
|
-
import TRadioGroup from './TRadioGroup.vue'
|
|
6
|
-
import { themeFonts } from '../theme'
|
|
7
|
-
import type { ThemeFont } from '../types'
|
|
8
|
-
|
|
9
|
-
const props = withDefaults(defineProps<{ fonts?: ThemeFont[]; disabled?: boolean; legend?: string }>(),
|
|
10
|
-
{ fonts: () => ['sans-serif', 'serif', 'monospace'] })
|
|
11
|
-
|
|
12
|
-
const model = defineModel<ThemeFont | null | undefined>()
|
|
13
|
-
const items = computed(() => themeFonts.filter(f => props.fonts.includes(f.value)))
|
|
14
|
-
const family = (font: ThemeFont) => `var(--tblr-font-${font})`
|
|
15
|
-
</script>
|
|
16
|
-
|
|
17
|
-
<template>
|
|
18
|
-
<TRadioGroup v-model="model" :items="items" variant="tiles" :disabled="disabled" :legend="legend">
|
|
19
|
-
<template #preview="{ item }">
|
|
20
|
-
<span class="t-theme-font" :style="{ fontFamily: family(item.value as ThemeFont) }">Aa</span>
|
|
21
|
-
</template>
|
|
22
|
-
</TRadioGroup>
|
|
23
|
-
</template>
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// A theme's font, as tiles showing "Aa" in each typeface. `fonts` limits the choices; Comic is left
|
|
3
|
+
// out unless asked for.
|
|
4
|
+
import { computed } from 'vue'
|
|
5
|
+
import TRadioGroup from './TRadioGroup.vue'
|
|
6
|
+
import { themeFonts } from '../theme'
|
|
7
|
+
import type { ThemeFont } from '../types'
|
|
8
|
+
|
|
9
|
+
const props = withDefaults(defineProps<{ fonts?: ThemeFont[]; disabled?: boolean; legend?: string }>(),
|
|
10
|
+
{ fonts: () => ['sans-serif', 'serif', 'monospace'] })
|
|
11
|
+
|
|
12
|
+
const model = defineModel<ThemeFont | null | undefined>()
|
|
13
|
+
const items = computed(() => themeFonts.filter(f => props.fonts.includes(f.value)))
|
|
14
|
+
const family = (font: ThemeFont) => `var(--tblr-font-${font})`
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
<template>
|
|
18
|
+
<TRadioGroup v-model="model" :items="items" variant="tiles" :disabled="disabled" :legend="legend">
|
|
19
|
+
<template #preview="{ item }">
|
|
20
|
+
<span class="t-theme-font" :style="{ fontFamily: family(item.value as ThemeFont) }">Aa</span>
|
|
21
|
+
</template>
|
|
22
|
+
</TRadioGroup>
|
|
23
|
+
</template>
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// Where an application's navigation goes: down the left side or across the top. Each tile is a
|
|
3
|
-
// sketch of the page.
|
|
4
|
-
import TRadioGroup from './TRadioGroup.vue'
|
|
5
|
-
import { themeNavs } from '../theme'
|
|
6
|
-
import type { ThemeNav } from '../types'
|
|
7
|
-
|
|
8
|
-
defineProps<{ disabled?: boolean; legend?: string }>()
|
|
9
|
-
|
|
10
|
-
const model = defineModel<ThemeNav | null | undefined>()
|
|
11
|
-
</script>
|
|
12
|
-
|
|
13
|
-
<template>
|
|
14
|
-
<TRadioGroup v-model="model" :items="themeNavs" variant="tiles" tile-width="8rem" :disabled="disabled" :legend="legend">
|
|
15
|
-
<template #preview="{ item }">
|
|
16
|
-
<span :class="['t-sketch', item.value === 'top' ? 't-sketch-top' : 't-sketch-left']">
|
|
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
|
+
// Where an application's navigation goes: down the left side or across the top. Each tile is a
|
|
3
|
+
// sketch of the page.
|
|
4
|
+
import TRadioGroup from './TRadioGroup.vue'
|
|
5
|
+
import { themeNavs } from '../theme'
|
|
6
|
+
import type { ThemeNav } from '../types'
|
|
7
|
+
|
|
8
|
+
defineProps<{ disabled?: boolean; legend?: string }>()
|
|
9
|
+
|
|
10
|
+
const model = defineModel<ThemeNav | null | undefined>()
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<template>
|
|
14
|
+
<TRadioGroup v-model="model" :items="themeNavs" variant="tiles" tile-width="8rem" :disabled="disabled" :legend="legend">
|
|
15
|
+
<template #preview="{ item }">
|
|
16
|
+
<span :class="['t-sketch', item.value === 'top' ? 't-sketch-top' : 't-sketch-left']">
|
|
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,36 +1,36 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// Shows its content at a screen's width (`width`, in pixels), scaled down to fit: a preview of a
|
|
3
|
-
// whole page, such as an application's layout, in a smaller space. It fills its parent's height;
|
|
4
|
-
// the content scrolls inside it, and fixed or sticky parts stay inside the frame.
|
|
5
|
-
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
|
|
6
|
-
|
|
7
|
-
const props = withDefaults(defineProps<{ width?: number }>(), { width: 1440 })
|
|
8
|
-
|
|
9
|
-
const frame = ref<HTMLElement>()
|
|
10
|
-
const size = ref({ width: 0, height: 0 })
|
|
11
|
-
let observer: ResizeObserver | undefined
|
|
12
|
-
|
|
13
|
-
onMounted(() => {
|
|
14
|
-
observer = new ResizeObserver(([entry]) => { size.value = { width: entry.contentRect.width, height: entry.contentRect.height } })
|
|
15
|
-
observer.observe(frame.value!)
|
|
16
|
-
})
|
|
17
|
-
onBeforeUnmount(() => observer?.disconnect())
|
|
18
|
-
|
|
19
|
-
// Never larger than life.
|
|
20
|
-
const scale = computed(() => size.value.width ? Math.min(1, size.value.width / props.width) : 1)
|
|
21
|
-
const screen = computed(() => ({
|
|
22
|
-
width: `${props.width}px`,
|
|
23
|
-
height: `${size.value.height / scale.value}px`,
|
|
24
|
-
transform: `scale(${scale.value})`
|
|
25
|
-
}))
|
|
26
|
-
</script>
|
|
27
|
-
|
|
28
|
-
<template>
|
|
29
|
-
<div ref="frame" class="t-scaled-frame">
|
|
30
|
-
<div class="t-scaled-screen" :style="screen">
|
|
31
|
-
<div class="t-scaled-scroll">
|
|
32
|
-
<slot :scale="scale" />
|
|
33
|
-
</div>
|
|
34
|
-
</div>
|
|
35
|
-
</div>
|
|
36
|
-
</template>
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// Shows its content at a screen's width (`width`, in pixels), scaled down to fit: a preview of a
|
|
3
|
+
// whole page, such as an application's layout, in a smaller space. It fills its parent's height;
|
|
4
|
+
// the content scrolls inside it, and fixed or sticky parts stay inside the frame.
|
|
5
|
+
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
|
|
6
|
+
|
|
7
|
+
const props = withDefaults(defineProps<{ width?: number }>(), { width: 1440 })
|
|
8
|
+
|
|
9
|
+
const frame = ref<HTMLElement>()
|
|
10
|
+
const size = ref({ width: 0, height: 0 })
|
|
11
|
+
let observer: ResizeObserver | undefined
|
|
12
|
+
|
|
13
|
+
onMounted(() => {
|
|
14
|
+
observer = new ResizeObserver(([entry]) => { size.value = { width: entry.contentRect.width, height: entry.contentRect.height } })
|
|
15
|
+
observer.observe(frame.value!)
|
|
16
|
+
})
|
|
17
|
+
onBeforeUnmount(() => observer?.disconnect())
|
|
18
|
+
|
|
19
|
+
// Never larger than life.
|
|
20
|
+
const scale = computed(() => size.value.width ? Math.min(1, size.value.width / props.width) : 1)
|
|
21
|
+
const screen = computed(() => ({
|
|
22
|
+
width: `${props.width}px`,
|
|
23
|
+
height: `${size.value.height / scale.value}px`,
|
|
24
|
+
transform: `scale(${scale.value})`
|
|
25
|
+
}))
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<template>
|
|
29
|
+
<div ref="frame" class="t-scaled-frame">
|
|
30
|
+
<div class="t-scaled-screen" :style="screen">
|
|
31
|
+
<div class="t-scaled-scroll">
|
|
32
|
+
<slot :scale="scale" />
|
|
33
|
+
</div>
|
|
34
|
+
</div>
|
|
35
|
+
</div>
|
|
36
|
+
</template>
|