webcoreui 0.4.0 → 0.5.0

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.
Files changed (47) hide show
  1. package/README.md +230 -227
  2. package/astro.d.ts +6 -0
  3. package/astro.js +6 -0
  4. package/components/Avatar/Avatar.astro +9 -2
  5. package/components/Avatar/Avatar.svelte +3 -1
  6. package/components/Avatar/Avatar.tsx +4 -2
  7. package/components/Avatar/avatar.ts +1 -0
  8. package/components/Button/button.module.scss +6 -1
  9. package/components/Button/button.ts +2 -2
  10. package/components/Carousel/Carousel.astro +198 -0
  11. package/components/Carousel/Carousel.svelte +161 -0
  12. package/components/Carousel/Carousel.tsx +172 -0
  13. package/components/Carousel/carousel.module.scss +58 -0
  14. package/components/Carousel/carousel.ts +26 -0
  15. package/components/DataTable/DataTable.astro +332 -0
  16. package/components/DataTable/DataTable.svelte +272 -0
  17. package/components/DataTable/DataTable.tsx +287 -0
  18. package/components/DataTable/datatable.module.scss +102 -0
  19. package/components/DataTable/datatable.ts +41 -0
  20. package/components/Icon/map.ts +6 -0
  21. package/components/Input/input.module.scss +6 -0
  22. package/components/List/List.astro +1 -1
  23. package/components/List/List.svelte +1 -1
  24. package/components/List/List.tsx +1 -2
  25. package/components/Pagination/Pagination.astro +189 -0
  26. package/components/Pagination/Pagination.svelte +144 -0
  27. package/components/Pagination/Pagination.tsx +162 -0
  28. package/components/Pagination/pagination.module.scss +49 -0
  29. package/components/Pagination/pagination.ts +35 -0
  30. package/components/Select/Select.astro +8 -4
  31. package/components/Select/Select.svelte +15 -6
  32. package/components/Select/Select.tsx +15 -8
  33. package/components/Select/select.ts +7 -2
  34. package/components/Table/Table.svelte +1 -1
  35. package/components/Table/table.ts +1 -1
  36. package/icons/arrow-left.svg +3 -0
  37. package/icons/arrow-right.svg +3 -0
  38. package/icons/order.svg +3 -0
  39. package/icons.d.ts +3 -0
  40. package/icons.js +3 -0
  41. package/index.d.ts +6 -6
  42. package/package.json +11 -9
  43. package/react.d.ts +6 -0
  44. package/react.js +6 -0
  45. package/scss/resets.scss +27 -1
  46. package/svelte.d.ts +6 -0
  47. package/svelte.js +6 -0
@@ -0,0 +1,287 @@
1
+ import React, { useState } from 'react'
2
+ import type { HeadingObject, ReactDataTableProps } from './datatable'
3
+
4
+ import Button from '../Button/Button.tsx'
5
+ import ConditionalWrapper from '../ConditionalWrapper/ConditionalWrapper.tsx'
6
+ import Input from '../Input/Input.tsx'
7
+ import Pagination from '../Pagination/Pagination.tsx'
8
+ import Select from '../Select/Select.tsx'
9
+
10
+ import { classNames } from '../../utils/classNames'
11
+ import { debounce } from '../../utils/debounce'
12
+
13
+ import checkIcon from '../../icons/check.svg?raw'
14
+ import orderIcon from '../../icons/order.svg?raw'
15
+ import searchIcon from '../../icons/search.svg?raw'
16
+
17
+ import styles from './datatable.module.scss'
18
+
19
+ import type { ListEventType } from '../List/list'
20
+
21
+ // eslint-disable-next-line complexity
22
+ const DataTable = ({
23
+ headings,
24
+ filterPlaceholder = 'Filter entries',
25
+ showFilterIcon,
26
+ noResultsLabel = 'No results.',
27
+ itemsPerPage,
28
+ subText,
29
+ columnToggleLabel = 'Columns',
30
+ pagination,
31
+ data,
32
+ hover,
33
+ striped,
34
+ offsetStripe,
35
+ compact,
36
+ maxHeight,
37
+ className,
38
+ id,
39
+ onFilter,
40
+ children
41
+ }: ReactDataTableProps) => {
42
+ const [filteredData, setFilteredData] = useState<any>(data)
43
+ const [toggledData, setToggledData] = useState(filteredData)
44
+ const [filteredHeadings, setFilteredHeadings] = useState<any>(headings)
45
+ const [page, setPage] = useState(1)
46
+ const [hasActiveFilter, setHasActiveFilter] = useState(false)
47
+ const [sortOrder, setSortOrder] = useState(1)
48
+
49
+ const classes = classNames([
50
+ styles.table,
51
+ hover && styles.hover,
52
+ striped && styles[`striped-${striped}s`],
53
+ offsetStripe && styles.offset,
54
+ compact && styles.compact,
55
+ maxHeight && styles.scroll
56
+ ])
57
+
58
+ const footerClasses = classNames([
59
+ styles.footer,
60
+ subText && styles.between
61
+ ])
62
+
63
+ const styleVariables = {
64
+ ...(maxHeight && { 'max-height': maxHeight })
65
+ } as React.CSSProperties
66
+
67
+ const showColumnToggle = headings?.some(heading => {
68
+ return typeof heading === 'string' ? false : heading.toggleable
69
+ })
70
+
71
+ const columnToggleItems = [{
72
+ items: headings?.length ? headings
73
+ .filter(heading => typeof heading !== 'string' && heading.toggleable)
74
+ .map(heading => ({
75
+ icon: checkIcon,
76
+ name: (heading as HeadingObject).name,
77
+ value: String(headings.findIndex(h => {
78
+ return (h as HeadingObject).name === (heading as HeadingObject).name
79
+ }))
80
+ })) : []
81
+ }]
82
+
83
+ const columnFilterIndexes = headings?.map(heading => (heading as HeadingObject).filterable)
84
+ .map((heading, index) => heading ? index : null)
85
+ .filter(heading => heading !== null) || []
86
+
87
+ const hasPagination = data?.length && itemsPerPage
88
+ ? data.length > itemsPerPage
89
+ : false
90
+
91
+ const filter = debounce((event: Event) => {
92
+ const target = event.target as HTMLInputElement
93
+
94
+ setHasActiveFilter(!!target.value)
95
+
96
+ setFilteredData(toggledData?.filter((row: string[]) => {
97
+ const rowValue = row.filter((_, index) => columnFilterIndexes.includes(index))
98
+ .join('')
99
+ .toLowerCase()
100
+
101
+ return rowValue.includes(target.value.toLowerCase())
102
+ }))
103
+
104
+ onFilter?.({
105
+ results: filteredData,
106
+ numberOfResults: filteredData.length
107
+ })
108
+ }, 400)
109
+
110
+ const toggleColumns = (event: ListEventType) => {
111
+ const columnToggleListElement = Array.from(event.list.children)
112
+ .find(child => (child as HTMLLIElement).dataset.name === event.name) as HTMLLIElement
113
+ const svgIcon = columnToggleListElement.children[0] as HTMLElement
114
+ let mappedData
115
+
116
+ svgIcon.style.opacity = svgIcon.style.opacity === '0'
117
+ ? '1'
118
+ : '0'
119
+
120
+ if (svgIcon.style.opacity === '0') {
121
+ mappedData = (hasActiveFilter ? data : filteredData)?.map((row: string[]) => {
122
+ return row.map((column, index) => index === Number(event.value) ? null : column)
123
+ })
124
+
125
+ setFilteredData(mappedData)
126
+
127
+ setFilteredHeadings(filteredHeadings.map((heading: HeadingObject | string) => {
128
+ return ((heading as HeadingObject)?.name || heading) === event.name ? null : heading
129
+ }))
130
+ } else {
131
+ mappedData = (hasActiveFilter ? data : filteredData)?.map((row: string[], x: number) => {
132
+ return row.map((column, y) => y === Number(event.value) ? data?.[x][y] : column)
133
+ })
134
+
135
+ setFilteredData(mappedData)
136
+
137
+ setFilteredHeadings(filteredHeadings.map((heading: HeadingObject | string, index: number) => {
138
+ return ((headings?.[index] as HeadingObject)?.name || headings?.[index]) === event.name
139
+ ? headings?.[index]
140
+ : heading
141
+ }))
142
+ }
143
+
144
+ setToggledData(mappedData)
145
+ }
146
+
147
+ const sort = (index: number) => {
148
+ const sortedData = filteredData.sort((a: string[], b: string[]) => {
149
+ let aValue: string | number = a[index]
150
+ let bValue: string | number = b[index]
151
+
152
+ if (!isNaN(aValue as any)) {
153
+ aValue = Number(aValue)
154
+ }
155
+
156
+ if (!isNaN(bValue as any)) {
157
+ bValue = Number(bValue)
158
+ }
159
+
160
+ return aValue > bValue
161
+ ? sortOrder * -1
162
+ : sortOrder
163
+ })
164
+
165
+ setFilteredData(sortedData)
166
+ setSortOrder(sortOrder === 1 ? -1 : 1)
167
+ }
168
+
169
+ const isNextPage = (index: number) => {
170
+ if (hasPagination && itemsPerPage && !hasActiveFilter) {
171
+ const currentPage = Math.ceil((index + 1) / itemsPerPage)
172
+
173
+ return currentPage !== page ? 'true' : undefined
174
+ }
175
+
176
+ if (hasActiveFilter && itemsPerPage) {
177
+ return index >= itemsPerPage ? 'true' : undefined
178
+ }
179
+
180
+ return undefined
181
+ }
182
+
183
+ return (
184
+ <section className={className} id={id}>
185
+ {(!!columnFilterIndexes?.length || showColumnToggle) && (
186
+ <div className={styles.filters}>
187
+ {!!columnFilterIndexes?.length && (
188
+ <Input
189
+ type="search"
190
+ placeholder={filterPlaceholder}
191
+ onInput={filter}
192
+ >
193
+ {showFilterIcon && (
194
+ <span dangerouslySetInnerHTML={{ __html: searchIcon }} />
195
+ )}
196
+ </Input>
197
+ )}
198
+ {showColumnToggle && (
199
+ <Select
200
+ name={`data-table-${id || crypto.randomUUID()}`}
201
+ itemGroups={columnToggleItems}
202
+ position="bottom-end"
203
+ value={columnToggleLabel}
204
+ onChange={toggleColumns}
205
+ updateValue={false}
206
+ />
207
+ )}
208
+ </div>
209
+ )}
210
+
211
+ <div className={classes} style={styleVariables}>
212
+ <table>
213
+ {!!filteredHeadings?.length && (
214
+ <thead>
215
+ <tr>
216
+ {filteredHeadings?.map((heading: HeadingObject | string, index: number) => {
217
+ if (!heading) {
218
+ return null
219
+ }
220
+
221
+ return (
222
+ <th key={index}>
223
+ <ConditionalWrapper
224
+ condition={!!(heading as HeadingObject).sortable}
225
+ wrapper={children => (
226
+ <Button theme="flat" slot="wrapper" onClick={() => sort(index)}>
227
+ {children}
228
+ <span dangerouslySetInnerHTML={{ __html: orderIcon }} />
229
+ </Button>
230
+ )}
231
+ >
232
+ {(heading as HeadingObject).name || heading as string}
233
+ </ConditionalWrapper>
234
+ </th>
235
+ )
236
+ })}
237
+ </tr>
238
+ </thead>
239
+ )}
240
+
241
+ <tbody>
242
+ {filteredData?.map((row: string[], rowIndex: number) => (
243
+ <tr key={rowIndex} data-hidden={isNextPage(rowIndex)}>
244
+ {row.filter(Boolean).map((column, columnIndex) => (
245
+ <td
246
+ key={columnIndex}
247
+ dangerouslySetInnerHTML={{ __html: column }}
248
+ />
249
+ ))}
250
+ </tr>
251
+ ))}
252
+ {children}
253
+ </tbody>
254
+ {!filteredData?.length && (
255
+ <tfoot>
256
+ <tr>
257
+ <td
258
+ colSpan={data?.[0].length}
259
+ className={styles['no-results']}
260
+ >
261
+ {noResultsLabel}
262
+ </td>
263
+ </tr>
264
+ </tfoot>
265
+ )}
266
+ </table>
267
+ </div>
268
+ {(subText || hasPagination) && (
269
+ <div className={footerClasses}>
270
+ {subText && (
271
+ <span className={styles.subtext}>{subText}</span>
272
+ )}
273
+ {(hasPagination && itemsPerPage && !hasActiveFilter) && (
274
+ <Pagination
275
+ {...pagination}
276
+ totalPages={Math.ceil((data?.length || 0) / itemsPerPage)}
277
+ currentPage={page}
278
+ onChange={event => setPage(event.page)}
279
+ />
280
+ )}
281
+ </div>
282
+ )}
283
+ </section>
284
+ )
285
+ }
286
+
287
+ export default DataTable
@@ -0,0 +1,102 @@
1
+ @import '../../scss/config.scss';
2
+
3
+ .filters {
4
+ @include layout(flex, xs);
5
+ @include spacing(mb-sm);
6
+ }
7
+
8
+ .table {
9
+ @include visibility(auto);
10
+
11
+ table {
12
+ @include size('w100%');
13
+ @include typography(left);
14
+
15
+ border-collapse: collapse;
16
+ }
17
+
18
+ thead,
19
+ thead button {
20
+ @include typography(bold);
21
+ }
22
+
23
+ thead button {
24
+ @include spacing(p-xxs);
25
+
26
+ svg {
27
+ @include size(15px);
28
+ pointer-events: none;
29
+ }
30
+ }
31
+
32
+ th,
33
+ td {
34
+ @include spacing(py-xs, px-sm);
35
+
36
+ &.no-results {
37
+ @include typography(center);
38
+ }
39
+ }
40
+
41
+ thead,
42
+ tr {
43
+ @include border(bottom, primary-50);
44
+
45
+ &:last-child {
46
+ @include border(bottom, 0);
47
+ }
48
+ }
49
+
50
+ [data-hidden] {
51
+ @include visibility(none);
52
+ }
53
+
54
+ &.hover tr:hover,
55
+ &.striped-rows tbody tr:nth-child(odd),
56
+ &.striped-rows.offset tbody tr:nth-child(even),
57
+ &.striped-columns td:nth-child(odd),
58
+ &.striped-columns.offset td:nth-child(even),
59
+ &.hover.striped-rows.offset tbody tr:nth-child(odd):hover {
60
+ @include background(primary-60);
61
+ }
62
+
63
+ &.striped-rows tr,
64
+ &.striped-columns tr,
65
+ &.striped-columns thead {
66
+ @include border(bottom, 0);
67
+ }
68
+
69
+ &.striped-rows.offset tbody tr:nth-child(odd),
70
+ &.striped-columns.offset td:nth-child(odd) {
71
+ @include background(transparent);
72
+ }
73
+
74
+ &.compact {
75
+ th, td {
76
+ @include spacing(py-xxs, px-sm);
77
+ }
78
+ }
79
+
80
+ &.scroll {
81
+ @include spacing(pr-sm);
82
+
83
+ thead {
84
+ @include position(sticky, t0);
85
+ @include background(primary-70);
86
+ box-shadow: 0 .5px 0 var(--w-color-primary-50);
87
+ }
88
+ }
89
+ }
90
+
91
+ .footer {
92
+ @include layout(flex, h-end, v-center, xs);
93
+ @include spacing(mt-sm);
94
+
95
+ &.between {
96
+ @include layout(h-between);
97
+ }
98
+
99
+ .subtext {
100
+ @include typography(md, primary-20);
101
+ }
102
+ }
@@ -0,0 +1,41 @@
1
+ import type { PaginationProps } from '../Pagination/pagination'
2
+
3
+ export type DataTableEventType = {
4
+ results: string[][]
5
+ numberOfResults: number
6
+ }
7
+
8
+ export type HeadingObject = {
9
+ name: string
10
+ sortable?: boolean
11
+ toggleable?: boolean
12
+ filterable?: boolean
13
+ }
14
+
15
+ export type DataTableProps = {
16
+ headings?: (HeadingObject | string)[]
17
+ filterPlaceholder?: string
18
+ showFilterIcon?: boolean
19
+ noResultsLabel?: string
20
+ itemsPerPage?: number | null
21
+ subText?: string
22
+ columnToggleLabel?: string
23
+ pagination?: PaginationProps
24
+ data: string[][]
25
+ hover?: boolean
26
+ striped?: 'column' | 'row' | null
27
+ offsetStripe?: boolean
28
+ compact?: boolean
29
+ maxHeight?: string
30
+ className?: string
31
+ id?: string
32
+ }
33
+
34
+ export type SvelteDataTableProps = {
35
+ onFilter?: (event: DataTableEventType) => void
36
+ } & DataTableProps
37
+
38
+ export type ReactDataTableProps = {
39
+ onFilter?: (event: DataTableEventType) => void
40
+ children?: React.ReactNode
41
+ } & DataTableProps
@@ -1,11 +1,14 @@
1
1
  import Alert from '../../icons/alert.svg?raw'
2
2
  import ArrowDown from '../../icons/arrow-down.svg?raw'
3
+ import ArrowLeft from '../../icons/arrow-left.svg?raw'
4
+ import ArrowRight from '../../icons/arrow-right.svg?raw'
3
5
  import Check from '../../icons/check.svg?raw'
4
6
  import CircleCheck from '../../icons/circle-check.svg?raw'
5
7
  import Close from '../../icons/close.svg?raw'
6
8
  import Github from '../../icons/github.svg?raw'
7
9
  import Info from '../../icons/info.svg?raw'
8
10
  import Moon from '../../icons/moon.svg?raw'
11
+ import Order from '../../icons/order.svg?raw'
9
12
  import Search from '../../icons/search.svg?raw'
10
13
  import Sun from '../../icons/sun.svg?raw'
11
14
  import Warning from '../../icons/warning.svg?raw'
@@ -13,12 +16,15 @@ import Warning from '../../icons/warning.svg?raw'
13
16
  const iconMap = {
14
17
  'alert': Alert,
15
18
  'arrow-down': ArrowDown,
19
+ 'arrow-left': ArrowLeft,
20
+ 'arrow-right': ArrowRight,
16
21
  'check': Check,
17
22
  'circle-check': CircleCheck,
18
23
  'close': Close,
19
24
  'github': Github,
20
25
  'info': Info,
21
26
  'moon': Moon,
27
+ 'order': Order,
22
28
  'search': Search,
23
29
  'sun': Sun,
24
30
  'warning': Warning
@@ -52,6 +52,7 @@
52
52
 
53
53
  .input-label {
54
54
  @include layout(flex, column);
55
+ @include size('w100%');
55
56
 
56
57
  .label {
57
58
  @include typography(primary-20);
@@ -66,6 +67,11 @@
66
67
  padding-left: 40px;
67
68
  }
68
69
 
70
+ span {
71
+ @include position(absolute);
72
+ @include size(18px);
73
+ }
74
+
69
75
  svg {
70
76
  @include position(absolute, l10px);
71
77
  @include size(18px);
@@ -76,7 +76,7 @@ const wrapperClasses = [
76
76
  <ConditionalWrapper condition={!!(item.icon && item.subText)}>
77
77
  <div slot="wrapper">children</div>
78
78
  {item.icon && <Fragment set:html={item.icon} />}
79
- {item.name}
79
+ <div>{item.name}</div>
80
80
  </ConditionalWrapper>
81
81
  {item.subText && <span>{item.subText}</span>}
82
82
  </ConditionalWrapper>
@@ -130,7 +130,7 @@
130
130
  {#if item.icon}
131
131
  {@html item.icon}
132
132
  {/if}
133
- {item.name}
133
+ <div>{item.name}</div>
134
134
  </ConditionalWrapper>
135
135
  {#if item.subText}
136
136
  <span>{item.subText}</span>
@@ -97,7 +97,6 @@ const List = ({
97
97
  {showSearchBarIcon && (
98
98
  <span
99
99
  dangerouslySetInnerHTML={{ __html: searchIcon }}
100
- style={{ height: '18px', position: 'absolute' }}
101
100
  />
102
101
  )}
103
102
  </Input>
@@ -148,7 +147,7 @@ const List = ({
148
147
  style={{ height: '18px' }}
149
148
  />
150
149
  )}
151
- {item.name}
150
+ <div>{item.name}</div>
152
151
  </ConditionalWrapper>
153
152
  {item.subText && <span>{item.subText}</span>}
154
153
  </ConditionalWrapper>