sveltacular 0.0.69 → 0.0.71

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.
@@ -7,10 +7,17 @@ export let size = "full";
7
7
  export let step = 1;
8
8
  export let min = 0;
9
9
  export let max = 1e6;
10
+ export let isCents = false;
11
+ let innerValue = isCents && value ? Math.round(value / 100) : value;
10
12
  $:
11
13
  decimals = allowCents ? 2 : 0;
14
+ const onChange = (e) => {
15
+ const dollars = e.detail;
16
+ if (dollars !== null)
17
+ value = isCents ? Math.round(dollars * 100) : dollars;
18
+ };
12
19
  </script>
13
20
 
14
- <NumberBox bind:value prefix={symbol} {decimals} {placeholder} {size} {min} {max} {step}
15
- ><slot /></NumberBox
16
- >
21
+ <NumberBox bind:value={innerValue} prefix={symbol} {decimals} {placeholder} {size} {min} {max} {step} on:change={onChange}>
22
+ <slot />
23
+ </NumberBox>
@@ -10,6 +10,7 @@ declare const __propDef: {
10
10
  step?: number | undefined;
11
11
  min?: number | undefined;
12
12
  max?: number | undefined;
13
+ isCents?: boolean | undefined;
13
14
  };
14
15
  events: {
15
16
  [evt: string]: CustomEvent<any>;
@@ -1,8 +1,10 @@
1
- <script>import { formatNumber, roundToDecimals } from "../../helpers/round-to-decimals.js";
1
+ <script>import { roundToDecimals } from "../../helpers/round-to-decimals.js";
2
2
  import { uniqueId } from "../../helpers/unique-id.js";
3
3
  import FormField from "../form-field.svelte";
4
4
  import FormLabel from "../form-label.svelte";
5
+ import { createEventDispatcher } from "svelte";
5
6
  const id = uniqueId();
7
+ const dipatch = createEventDispatcher();
6
8
  export let value = 0;
7
9
  export let placeholder = "";
8
10
  export let size = "full";
@@ -21,6 +23,27 @@ const valueChanged = () => {
21
23
  if (value > max)
22
24
  value = max;
23
25
  value = roundToDecimals(value, decimals);
26
+ dipatch("change", value);
27
+ };
28
+ const onInput = (e) => {
29
+ const input = e.target;
30
+ const newValue = parseFloat(input.value);
31
+ if (isNaN(newValue))
32
+ return;
33
+ value = newValue;
34
+ };
35
+ const onKeyPress = (e) => {
36
+ const isNumber = !isNaN(Number(e.key));
37
+ const isDecimal = e.key === ".";
38
+ const isAllowed = isNumber || isDecimal || ["Backspace", "Delete", "ArrowLeft", "ArrowRight", "Tab"].includes(e.key);
39
+ if (!isAllowed)
40
+ return e.preventDefault();
41
+ if (isDecimal && decimals === 0)
42
+ return e.preventDefault();
43
+ const newValue = `${value}${e.key}`;
44
+ const decimalPart = newValue.split(".")[1];
45
+ if (decimalPart && decimalPart.length > decimals)
46
+ return e.preventDefault();
24
47
  };
25
48
  </script>
26
49
 
@@ -42,6 +65,8 @@ const valueChanged = () => {
42
65
  {min}
43
66
  {max}
44
67
  on:change={valueChanged}
68
+ on:input={onInput}
69
+ on:keypress={onKeyPress}
45
70
  />
46
71
 
47
72
  {#if suffix}
@@ -14,6 +14,8 @@ declare const __propDef: {
14
14
  step?: number | undefined;
15
15
  };
16
16
  events: {
17
+ change: CustomEvent<number | null>;
18
+ } & {
17
19
  [evt: string]: CustomEvent<any>;
18
20
  };
19
21
  slots: {
@@ -28,12 +28,16 @@ const getColType = (col) => {
28
28
  return col.type;
29
29
  if (!rows?.length)
30
30
  return "string";
31
- return typeof rows[0][col.key];
31
+ const firstRow = rows[0];
32
+ if (!firstRow)
33
+ return "undefined";
34
+ if (col.key in firstRow)
35
+ return typeof firstRow[col.key];
32
36
  };
33
37
  const format = (row, key) => {
34
38
  const col = cols.find((col2) => col2.key === key);
35
39
  if (!col)
36
- return row[key];
40
+ return key in row ? String(row[key]) : "";
37
41
  if ((row[key] === null || row[key] === void 0) && col.nullText)
38
42
  return col.nullText;
39
43
  if (String(row[key]).trim() === "" && col.emptyText)
@@ -44,7 +48,7 @@ const format = (row, key) => {
44
48
  return formatDateTime(String(row[key])).substring(0, 10);
45
49
  if (col.type === "date-time")
46
50
  return formatDateTime(String(row[key]));
47
- return row[key];
51
+ return String(row[key]);
48
52
  };
49
53
  const calculateTotalPages = () => {
50
54
  if (!pagination || !rows)
@@ -1,19 +1,19 @@
1
1
  import { SvelteComponent } from "svelte";
2
- import type { DataCol, DataRow, PaginationProperties } from '../types/data.js';
2
+ import type { DataCol, JsonObject, PaginationProperties } from '../types/data.js';
3
3
  declare const __propDef: {
4
4
  props: {
5
5
  captionSide?: "top" | "bottom" | undefined;
6
6
  captionAlign?: "center" | "left" | "right" | undefined;
7
- rows?: DataRow[] | undefined;
7
+ rows?: JsonObject[] | undefined;
8
8
  cols: DataCol[];
9
9
  pagination?: PaginationProperties | undefined;
10
10
  actions?: {
11
11
  text: string;
12
- onClick: (row: DataRow) => unknown;
12
+ onClick: (row: JsonObject) => unknown;
13
13
  }[] | undefined;
14
14
  /**
15
15
  * Handle page change, which should return the new filtered/fetched rows.
16
- */ onPageChange?: ((pagination: PaginationProperties) => Promise<DataRow[]>) | null | undefined;
16
+ */ onPageChange?: ((pagination: PaginationProperties) => Promise<JsonObject[]>) | null | undefined;
17
17
  };
18
18
  events: {
19
19
  [evt: string]: CustomEvent<any>;
@@ -1,12 +1,17 @@
1
- export type DataRow = Record<string, unknown>;
2
- export type DataCol<T extends DataRow = DataRow> = {
1
+ export type JsonValue = string | number | boolean | null | {
2
+ [key: string]: JsonValue;
3
+ } | JsonValue[];
4
+ export type JsonObject = {
5
+ [key: string]: JsonValue;
6
+ };
7
+ export type DataCol = {
3
8
  key: string;
4
9
  label: string;
5
10
  type?: string;
6
11
  nullText?: string;
7
12
  emptyText?: string;
8
- format?: (row: T, key: keyof T) => string;
9
- link?: (row: T, key: keyof T) => string;
13
+ format?: (row: JsonObject, key: string) => string;
14
+ link?: (row: JsonObject, key: string) => string;
10
15
  hide?: boolean;
11
16
  width?: number | string;
12
17
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sveltacular",
3
- "version": "0.0.69",
3
+ "version": "0.0.71",
4
4
  "description": "A Svelte component library",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",