@isoftdata/svelte-table 2.11.0 → 2.11.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/dist/Td.svelte CHANGED
@@ -1,171 +1,171 @@
1
- <script
2
- lang="ts"
3
- generics="R extends UuidRowProps = any"
4
- >
5
- import type { ColumnInfo, RowProperties, UuidRowProps } from './'
6
- import type { ColumnInfoRunicStore } from './column-info-store.svelte'
7
- import type { HTMLTdAttributes, ClassValue } from 'svelte/elements'
8
- import type { Writable } from 'svelte/store'
9
- import { getContext, onMount, type Snippet } from 'svelte'
10
-
11
- // Get the columnInfo store from the Table component
12
- const columnInfo = getContext<ColumnInfoRunicStore<UuidRowProps>>('columnInfo')
13
- const columnResizingEnabled = getContext<Writable<boolean>>('columnResizingEnabled')
14
- const bs5 = getContext<boolean>('bs5')
15
-
16
- interface Props extends Omit<HTMLTdAttributes, 'align'> {
17
- class?: ClassValue
18
- property: ColumnInfo<R>['property']
19
- /** Direction to align the column's contents
20
- *
21
- * `start` and `end` are only available in Bootstrap 5+
22
- *
23
- * `left` and `right`should only be used while you transition to Boostrap 5+
24
- */
25
- align?: 'start' | 'left' | 'center' | 'right' | 'end'
26
- /** If enabled, pressing enter while focused in this Td will focus the next non-disabled input/select/textarea in the column. Holding shift will go up instead of down.
27
- *
28
- * Can also be a callback that is fired when the enter key is used to move between rows.
29
- */
30
- enterGoesDown?: boolean | ((ctx: { event: KeyboardEvent; tr: HTMLTableRowElement }) => void)
31
- /** If enabled, keypress (enter only) and click events on the Td's contents will have `stopPropagation`/`preventDefault` called on them. */
32
- stopPropagation?: boolean
33
- tagName?: 'td' | 'th'
34
- children?: Snippet
35
- }
36
-
37
- let {
38
- //
39
- class: classNames = '',
40
- property,
41
- align = $bindable(columnInfo.current[property]?.align),
42
- enterGoesDown = false,
43
- stopPropagation = false,
44
- tagName = 'td',
45
- children,
46
- ...rest
47
- }: Props = $props()
48
-
49
- const thisColumnInfo = $derived(
50
- columnInfo.current[property] as (typeof columnInfo.current)[typeof property] | undefined,
51
- )
52
- const alignClass = $derived.by(() => {
53
- let alignment = align || thisColumnInfo?.align || (thisColumnInfo?.numeric ? 'right' : undefined)
54
- if (bs5 && alignment === 'right') {
55
- alignment = 'end'
56
- } else if (bs5 && alignment === 'left') {
57
- alignment = 'start'
58
- }
59
- return alignment ? (`text-${alignment}` as const) : ''
60
- })
61
- const visible = $derived(thisColumnInfo?.visible ?? true)
62
-
63
- /** Handle `stopPropagation` and `enterGoesDown` props */
64
- function onkeypress(event: KeyboardEvent) {
65
- if (event.key === 'Enter') {
66
- event.stopPropagation()
67
- event.preventDefault()
68
- }
69
-
70
- if (event.key === 'Enter' && enterGoesDown) {
71
- const target = event.target
72
- if (!(target instanceof HTMLElement)) {
73
- return
74
- }
75
- const tr = target?.closest('tr') ?? null
76
- const td = target?.closest('td') ?? null
77
- const tdIndex = td ? Array.from(tr?.children ?? []).indexOf(td) : -1
78
- const allTrs = Array.from(tr?.parentElement?.children ?? [])
79
-
80
- // if they're holding shift, reverse the tr list
81
- if (event.shiftKey) {
82
- allTrs.reverse()
83
- }
84
- // Find the next Td in the column with a non-disabled input, select, or textarea
85
- const lastTrIndex = tr ? allTrs.indexOf(tr) : -1
86
- const newTrIndex = allTrs.findIndex(
87
- (tr, index) =>
88
- index > lastTrIndex &&
89
- tr.children[tdIndex]?.querySelector('input:enabled, select:enabled, textarea:enabled') &&
90
- tr instanceof HTMLTableRowElement &&
91
- tr.offsetParent !== null,
92
- )
93
- const theTd = allTrs[newTrIndex]?.children[tdIndex] ?? null
94
- theTd?.querySelector<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>('input,select,textarea')?.focus()
95
- if (typeof enterGoesDown === 'function') {
96
- enterGoesDown({
97
- tr: allTrs[newTrIndex] as HTMLTableRowElement,
98
- event,
99
- })
100
- }
101
- }
102
- }
103
-
104
- onMount(() => {
105
- if (!(property in columnInfo.current)) {
106
- console.warn(
107
- `Column "${property}" does not exist in its parent Table component's list of columns.\r\n\r\nPlease check that this Td's "property" prop matches the "property" prop of one of the columns in the parent Table component.`,
108
- )
109
- }
110
- })
111
- </script>
112
-
113
- {#if visible}
114
- <svelte:element
115
- this={tagName}
116
- class={[
117
- classNames,
118
- alignClass,
119
- {
120
- pinned: thisColumnInfo?.pinned,
121
- },
122
- ]}
123
- {...rest}
124
- style:font-variant-numeric={thisColumnInfo?.numeric ? 'tabular-nums' : undefined}
125
- style:overflow={$columnResizingEnabled ? 'hidden' : undefined}
126
- style:text-overflow={$columnResizingEnabled ? 'ellipsis' : undefined}
127
- style:--pinned-column-offset={thisColumnInfo?.pinned ? columnInfo.getPinnedColumnOffset(property) : undefined}
128
- >
129
- <!--
130
- Don't listen to events that the user hasn't turned on the features for.
131
- Also, put them on a span so they can still click in the td to click the row but not the td's child content
132
- -->
133
- {#if stopPropagation || enterGoesDown}
134
- <!-- svelte-ignore a11y_no_static_element_interactions -->
135
- <span
136
- onclick={stopPropagation ? e => e.stopPropagation() : undefined}
137
- {onkeypress}
138
- >
139
- {@render children?.()}
140
- </span>
141
- {:else}
142
- {@render children?.()}
143
- {/if}
144
- </svelte:element>
145
- {/if}
146
-
147
- <style>
148
- td.text-right :global(input),
149
- td.text-end :global(input) {
150
- text-align: right;
151
- }
152
- td.text-right :global(select),
153
- td.text-end :global(select) {
154
- text-align: right;
155
- }
156
- td.text-right :global(textarea),
157
- td.text-end :global(textarea) {
158
- text-align: right;
159
- }
160
-
161
- /* BS5 uses floats for checkbox alignment, which overrides the text align classes, so undo that here. */
162
- td:where(.text-start, .text-center, .text-end) :global(.form-check-input) {
163
- float: unset;
164
- }
165
-
166
- .pinned {
167
- position: sticky;
168
- z-index: 10 !important;
169
- left: var(--pinned-column-offset, -1);
170
- }
171
- </style>
1
+ <script
2
+ lang="ts"
3
+ generics="R extends UuidRowProps = any"
4
+ >
5
+ import type { ColumnInfo, RowProperties, UuidRowProps } from './'
6
+ import type { ColumnInfoRunicStore } from './column-info-store.svelte'
7
+ import type { HTMLTdAttributes, ClassValue } from 'svelte/elements'
8
+ import type { Writable } from 'svelte/store'
9
+ import { getContext, onMount, type Snippet } from 'svelte'
10
+
11
+ // Get the columnInfo store from the Table component
12
+ const columnInfo = getContext<ColumnInfoRunicStore<UuidRowProps>>('columnInfo')
13
+ const columnResizingEnabled = getContext<Writable<boolean>>('columnResizingEnabled')
14
+ const bs5 = getContext<boolean>('bs5')
15
+
16
+ interface Props extends Omit<HTMLTdAttributes, 'align'> {
17
+ class?: ClassValue
18
+ property: ColumnInfo<R>['property']
19
+ /** Direction to align the column's contents
20
+ *
21
+ * `start` and `end` are only available in Bootstrap 5+
22
+ *
23
+ * `left` and `right`should only be used while you transition to Boostrap 5+
24
+ */
25
+ align?: 'start' | 'left' | 'center' | 'right' | 'end'
26
+ /** If enabled, pressing enter while focused in this Td will focus the next non-disabled input/select/textarea in the column. Holding shift will go up instead of down.
27
+ *
28
+ * Can also be a callback that is fired when the enter key is used to move between rows.
29
+ */
30
+ enterGoesDown?: boolean | ((ctx: { event: KeyboardEvent; tr: HTMLTableRowElement }) => void)
31
+ /** If enabled, keypress (enter only) and click events on the Td's contents will have `stopPropagation`/`preventDefault` called on them. */
32
+ stopPropagation?: boolean
33
+ tagName?: 'td' | 'th'
34
+ children?: Snippet
35
+ }
36
+
37
+ let {
38
+ //
39
+ class: classNames = '',
40
+ property,
41
+ align = $bindable(columnInfo.current[property]?.align),
42
+ enterGoesDown = false,
43
+ stopPropagation = false,
44
+ tagName = 'td',
45
+ children,
46
+ ...rest
47
+ }: Props = $props()
48
+
49
+ const thisColumnInfo = $derived(
50
+ columnInfo.current[property] as (typeof columnInfo.current)[typeof property] | undefined,
51
+ )
52
+ const alignClass = $derived.by(() => {
53
+ let alignment = align || thisColumnInfo?.align || (thisColumnInfo?.numeric ? 'right' : undefined)
54
+ if (bs5 && alignment === 'right') {
55
+ alignment = 'end'
56
+ } else if (bs5 && alignment === 'left') {
57
+ alignment = 'start'
58
+ }
59
+ return alignment ? (`text-${alignment}` as const) : ''
60
+ })
61
+ const visible = $derived(thisColumnInfo?.visible ?? true)
62
+
63
+ /** Handle `stopPropagation` and `enterGoesDown` props */
64
+ function onkeypress(event: KeyboardEvent) {
65
+ if (event.key === 'Enter') {
66
+ event.stopPropagation()
67
+ event.preventDefault()
68
+ }
69
+
70
+ if (event.key === 'Enter' && enterGoesDown) {
71
+ const target = event.target
72
+ if (!(target instanceof HTMLElement)) {
73
+ return
74
+ }
75
+ const tr = target?.closest('tr') ?? null
76
+ const td = target?.closest('td') ?? null
77
+ const tdIndex = td ? Array.from(tr?.children ?? []).indexOf(td) : -1
78
+ const allTrs = Array.from(tr?.parentElement?.children ?? [])
79
+
80
+ // if they're holding shift, reverse the tr list
81
+ if (event.shiftKey) {
82
+ allTrs.reverse()
83
+ }
84
+ // Find the next Td in the column with a non-disabled input, select, or textarea
85
+ const lastTrIndex = tr ? allTrs.indexOf(tr) : -1
86
+ const newTrIndex = allTrs.findIndex(
87
+ (tr, index) =>
88
+ index > lastTrIndex &&
89
+ tr.children[tdIndex]?.querySelector('input:enabled, select:enabled, textarea:enabled') &&
90
+ tr instanceof HTMLTableRowElement &&
91
+ tr.offsetParent !== null,
92
+ )
93
+ const theTd = allTrs[newTrIndex]?.children[tdIndex] ?? null
94
+ theTd?.querySelector<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>('input,select,textarea')?.focus()
95
+ if (typeof enterGoesDown === 'function') {
96
+ enterGoesDown({
97
+ tr: allTrs[newTrIndex] as HTMLTableRowElement,
98
+ event,
99
+ })
100
+ }
101
+ }
102
+ }
103
+
104
+ onMount(() => {
105
+ if (!(property in columnInfo.current)) {
106
+ console.warn(
107
+ `Column "${property}" does not exist in its parent Table component's list of columns.\r\n\r\nPlease check that this Td's "property" prop matches the "property" prop of one of the columns in the parent Table component.`,
108
+ )
109
+ }
110
+ })
111
+ </script>
112
+
113
+ {#if visible}
114
+ <svelte:element
115
+ this={tagName}
116
+ class={[
117
+ classNames,
118
+ alignClass,
119
+ {
120
+ pinned: thisColumnInfo?.pinned,
121
+ },
122
+ ]}
123
+ {...rest}
124
+ style:font-variant-numeric={thisColumnInfo?.numeric ? 'tabular-nums' : undefined}
125
+ style:overflow={$columnResizingEnabled ? 'hidden' : undefined}
126
+ style:text-overflow={$columnResizingEnabled ? 'ellipsis' : undefined}
127
+ style:--pinned-column-offset={thisColumnInfo?.pinned ? columnInfo.getPinnedColumnOffset(property) : undefined}
128
+ >
129
+ <!--
130
+ Don't listen to events that the user hasn't turned on the features for.
131
+ Also, put them on a span so they can still click in the td to click the row but not the td's child content
132
+ -->
133
+ {#if stopPropagation || enterGoesDown}
134
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
135
+ <span
136
+ onclick={stopPropagation ? e => e.stopPropagation() : undefined}
137
+ {onkeypress}
138
+ >
139
+ {@render children?.()}
140
+ </span>
141
+ {:else}
142
+ {@render children?.()}
143
+ {/if}
144
+ </svelte:element>
145
+ {/if}
146
+
147
+ <style>
148
+ td.text-right :global(input),
149
+ td.text-end :global(input) {
150
+ text-align: right;
151
+ }
152
+ td.text-right :global(select),
153
+ td.text-end :global(select) {
154
+ text-align: right;
155
+ }
156
+ td.text-right :global(textarea),
157
+ td.text-end :global(textarea) {
158
+ text-align: right;
159
+ }
160
+
161
+ /* BS5 uses floats for checkbox alignment, which overrides the text align classes, so undo that here. */
162
+ td:where(.text-start, .text-center, .text-end) :global(.form-check-input) {
163
+ float: unset;
164
+ }
165
+
166
+ .pinned {
167
+ position: sticky;
168
+ z-index: 10 !important;
169
+ left: var(--pinned-column-offset, -1);
170
+ }
171
+ </style>