@spectric/ui 0.0.10 → 0.0.11

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.
@@ -1,5 +1,5 @@
1
1
 
2
- import { filterByColumn, tablecolumns, tabledata } from "./data";
2
+ import { filterByColumn, tablecolumns, tabledata, TestData } from "./data";
3
3
  import { ExampleBits } from "./Bits";
4
4
  import { FieldTypes, SpectricQuery } from "../../components/query_bar/QueryBar";
5
5
 
@@ -7,8 +7,9 @@ import { html, LitElement } from 'lit';
7
7
 
8
8
  import { customElement, property, query, state } from 'lit/decorators.js';
9
9
  import "./lorumipsum"
10
- import { PaginationChangeProps } from "../../components/pagination";
10
+ import { PaginationChangeProps, PaginationProps } from "../../components/pagination";
11
11
  import { FilterEvent } from "../../components/table/cell";
12
+ import { TableDataOptions, TableElement } from "../../components/table";
12
13
  type Props = {
13
14
  frameWidth: number
14
15
  }
@@ -23,10 +24,15 @@ export class SpectricStorybookExampleContent extends LitElement implements Props
23
24
  @query("spectric-query")
24
25
  query!: SpectricQuery
25
26
 
27
+ private dataSorter = TableElement.getDefaultDataSorterAndPaginatior<TestData>(tabledata)
26
28
  @state()
27
29
  dialogOpen: boolean = false;
28
- tableData = tabledata.slice(0, 3)
29
- pagination = {
30
+ get tableData() {
31
+ return this.dataSorter(this)
32
+ }
33
+
34
+ columns = tablecolumns
35
+ pagination: PaginationProps = {
30
36
  page: 1,
31
37
  pageSize: 3,
32
38
  size: "xsmall",
@@ -35,17 +41,17 @@ export class SpectricStorybookExampleContent extends LitElement implements Props
35
41
  _handleFilter = (e: CustomEvent<FilterEvent<any>>) => {
36
42
  let include = e.detail.include ? "" : "not "
37
43
  if (e.detail.column.key && e.detail.value) {
38
- if (this.query.value !== "") {
44
+ if (this.query.value.trim() !== "") {
39
45
  this.query.value = `${this.query.value} and ${include}${e.detail.column.key}: '${e.detail.value}'`
40
46
  } else {
41
47
  this.query.value = `${include}${e.detail.column.key}: '${e.detail.value}'`
42
48
  }
43
49
  }
44
50
  }
45
- _handlePaginationChange = (e: CustomEvent<{ pagination: PaginationChangeProps }>) => {
46
- let { pagination } = e.detail
51
+ _handlePaginationChange = (e: CustomEvent<TableDataOptions<TestData>>) => {
52
+ let { pagination, columns } = e.detail
47
53
  this.pagination = { ...this.pagination, ...pagination }
48
- this.tableData = tabledata.slice((pagination.page - 1) * pagination.pageSize, (pagination.page) * pagination.pageSize)
54
+ this.columns = columns
49
55
  this.requestUpdate()
50
56
  }
51
57
  render() {
@@ -89,6 +95,7 @@ export class SpectricStorybookExampleContent extends LitElement implements Props
89
95
 
90
96
  </spectric-query>
91
97
  <spectric-table
98
+ style="height:200px;"
92
99
  .data=${this.tableData}
93
100
  .columns=${tablecolumns}
94
101
  @filter=${this._handleFilter}
@@ -33,21 +33,32 @@ export const filterByColumn = async (field, text) => {
33
33
  return values.filter(v => v.includes(text))
34
34
  }
35
35
 
36
- type TestData = {
36
+ export type TestData = {
37
37
  name: string
38
38
  company: string
39
39
  contact: string
40
40
  location: {
41
41
  country: string,
42
42
  state: string
43
- }
43
+ },
44
+ years: number
44
45
  }
45
46
  export const tabledata: TestData[] = [
46
- { name: "Sean", company: "Spectric Labs", "contact": "123-4567", location: { "country": "US", state: "VA" } },
47
- { name: "Kipp", company: "Spectric Labs", "contact": "123-4567", location: { "country": "UK", state: "N/A" } },
48
- { name: "Adam", company: "Spectric Labs", "contact": "123-4567", location: { "country": "US", state: "VA" } },
49
- { name: "Chris", company: "Spectric Labs", "contact": "123-4567", location: { "country": "US", state: "VA" } },
50
- { name: "Michael", company: "Spectric Labs", "contact": "123-4567", location: { "country": "US", state: "VA" } },
51
- { name: "Matt", company: "Spectric Labs", "contact": "123-4567", location: { "country": "US", state: "VA" } },
52
- { name: "Grant", company: "Spectric Labs", "contact": "123-4567", location: { "country": "US", state: "VA" } },]
53
- export const tablecolumns: ColumnSettings<TestData>[] = [{ "title": "Company", key: "company" }, { "title": "Name", key: "name" }, { "title": "Contact", key: "contact" }, { "title": "Country", key: "location.country", filterable: true }]
47
+ { name: "Sean", company: "Spectric Labs", "contact": "123-4567", location: { "country": "US", state: "VA" }, years: 11 },
48
+ { name: "Kipp", company: "Spectric Labs", "contact": "123-4567", location: { "country": "UK", state: "N/A" }, years: 5 },
49
+ { name: "Adam", company: "Spectric Labs", "contact": "123-4567", location: { "country": "US", state: "VA" }, years: 19 },
50
+ { name: "Chris", company: "Spectric Labs", "contact": "123-4567", location: { "country": "US", state: "VA" }, years: 27 },
51
+ { name: "Michael", company: "Spectric Labs", "contact": "123-4567", location: { "country": "US", state: "VA" }, years: 3 },
52
+ { name: "Matt", company: "Spectric Labs", "contact": "123-4567", location: { "country": "US", state: "VA" }, years: 9 },
53
+ { name: "Matt", company: "Spectric Labs", "contact": "865-6343", location: { "country": "UK", state: "VA" }, years: 5 },
54
+ { name: "Matt", company: "Spectric Labs", "contact": "253-6795", location: { "country": "UK", state: "VA" }, years: 15 },
55
+ { name: "Matt", company: "Spectric Labs (Intern)", "contact": "253-6795", location: { "country": "US", state: "CO" }, years: 1 },
56
+ { name: "Matt", company: "Spectric Labs", "contact": "912-1230", location: { "country": "UK", state: "VA" }, years: 24 },
57
+ { name: "Grant", company: "Spectric Labs", "contact": "123-4567", location: { "country": "US", state: "VA" }, years: 100 },]
58
+ export const tablecolumns: ColumnSettings<TestData>[] = [
59
+ { "title": "Company", key: "company" },
60
+ { "title": "Name", key: "name", sortable: true },
61
+ { "title": "Contact", key: "contact" },
62
+ { "title": "Country", key: "location.country", filterable: true },
63
+ { "title": "Years Employed", key: "years", sortable: true }
64
+ ]
@@ -1,15 +1,16 @@
1
1
  import type { Meta, StoryObj } from "@storybook/web-components";
2
2
 
3
- import { ColumnSettings, PaginationChangeProps, type TableProps as Props } from "../components/";
3
+ import { ColumnSettings, PaginationChangeProps, TableElement, TableSelectOptions, TableSortDirection, TableSortOption, type TableProps as Props } from "../components/";
4
4
  import { html } from "lit";
5
5
  import '../components';
6
6
  import { ifDefined } from "lit/directives/if-defined.js";
7
7
  import { useArgs } from "@storybook/client-api";
8
- import { FilterEvent } from "../components/table/cell";
8
+ import { FilterEvent, rowGetValue } from "../components/table/cell";
9
9
  import { tablecolumns, tabledata } from "./fixtures/data";
10
10
  // More on how to set up stories at: https://storybook.js.org/docs/writing-stories
11
- const data = tabledata
11
+ const data = JSON.parse(JSON.stringify(tabledata))
12
12
  const columns = tablecolumns
13
+ const getData = TableElement.getDefaultDataSorterAndPaginatior<typeof tabledata[0]>(data)
13
14
  const meta = {
14
15
  title: "UI/Table",
15
16
  tags: ["autodocs"],
@@ -19,22 +20,29 @@ const meta = {
19
20
 
20
21
  return html`
21
22
  <spectric-table
23
+ style="max-height: 150px;"
22
24
  .pagination=${args.pagination}
23
25
  .columns=${args.columns}
24
- .data=${!args.pagination ? data : data.slice((args.pagination.page - 1) * args.pagination.pageSize, (args.pagination.page) * args.pagination.pageSize)}
26
+ .data=${getData(args)}
25
27
  select=${ifDefined(args.select)}
28
+ sort=${ifDefined(args.sort)}
26
29
  @filter=${(e: CustomEvent<FilterEvent<any>>) => {
27
30
  alert(`filter ${e.detail.include ? "for" : "out"} event value ${e.detail.value}`)
28
31
  }}
29
32
  @change=${(e: CustomEvent<PaginationChangeProps>) => {
30
33
  console.log(e)
31
34
  updateArgs({ ...e.detail });
35
+ if (e.target && e.target instanceof TableElement) {
36
+ e.target.data = getData({ ...args, ...e.detail })
37
+ }
32
38
  }}
33
39
  >
34
40
  </spectric-table>
35
41
  `;
36
42
  },
37
43
  argTypes: {
44
+ select: { control: { type: "select" }, options: Object.values(TableSelectOptions) },
45
+ sort: { control: { type: "select" }, options: Object.values(TableSortOption) }
38
46
  },
39
47
  args: {
40
48
  pagination: {
@@ -43,7 +51,8 @@ const meta = {
43
51
  size: "xsmall",
44
52
  totalItems: data.length,
45
53
  },
46
- columns: columns
54
+ columns: columns,
55
+ select: TableSelectOptions.none
47
56
  },
48
57
  } satisfies Meta<Props<typeof tabledata[0]>>;
49
58
 
@@ -79,4 +88,15 @@ export const SingleSelect: Story = {
79
88
  args: {
80
89
  select: "single"
81
90
  },
91
+ };
92
+
93
+
94
+ /**
95
+ * The spectric table by default is set to single column sorting, but you can enable multicolumn sorting with the sort="multi" attribute
96
+ */
97
+ export const MultiColumnSort: Story = {
98
+ args: {
99
+ sort: "multi",
100
+ pagination: { pageSize: 20, page: 1 }
101
+ },
82
102
  };
@@ -0,0 +1,12 @@
1
+ export function once(func: Function) {
2
+ let hasBeenCalled = false;
3
+ let result: any;
4
+ return function (...args: any[]) {
5
+ if (!hasBeenCalled) {
6
+ hasBeenCalled = true;
7
+ //@ts-ignore
8
+ result = func.apply(this, args);
9
+ }
10
+ return result;
11
+ };
12
+ }
@@ -7,7 +7,7 @@ import { AsyncDirective, directive } from 'lit/async-directive.js';
7
7
  /**
8
8
  * Usage:
9
9
  * import { html, render } from 'lit';
10
- * import { spreadProps } from '@open-wc/lit-helpers';
10
+ * import { spreadProps } from '@spectric/ui';
11
11
  *
12
12
  * render(
13
13
  * html`
@@ -44,10 +44,10 @@ export class SpreadPropsDirective extends AsyncDirective {
44
44
 
45
45
  apply(data: any) {
46
46
  if (!data) return;
47
- const { prevData, element } = this;
47
+ const { element } = this;
48
48
  for (const key in data) {
49
49
  const value = data[key];
50
- if (value === prevData[key]) {
50
+ if ((element as any)[key] === value) {
51
51
  continue;
52
52
  }
53
53
  // @ts-ignore