sveltacular 0.0.70 → 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: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sveltacular",
3
- "version": "0.0.70",
3
+ "version": "0.0.71",
4
4
  "description": "A Svelte component library",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",