milk-lib 0.0.38 → 0.0.39

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.
@@ -20,6 +20,8 @@
20
20
  style,
21
21
  classNames='',
22
22
  autoFocus,
23
+ prohibitFraction = false,
24
+ prohibitNegative = false,
23
25
  ariaHasPopup,
24
26
  ariaExpanded,
25
27
  ariaControls,
@@ -41,6 +43,62 @@
41
43
  }
42
44
  })
43
45
 
46
+ const shouldRestrictNumberInput = () =>
47
+ type === 'number' && (prohibitFraction || prohibitNegative);
48
+
49
+ const sanitizeNumberInput = (rawValue: string) => {
50
+ if (!shouldRestrictNumberInput()) {
51
+ return rawValue;
52
+ }
53
+
54
+ let nextValue = rawValue;
55
+
56
+ if (prohibitFraction) {
57
+ nextValue = nextValue.split(/[.,]/)[0];
58
+ }
59
+
60
+ if (prohibitNegative) {
61
+ nextValue = nextValue.replace(/-/g, '');
62
+ } else {
63
+ nextValue = nextValue.replace(/(?!^)-/g, '');
64
+ }
65
+
66
+ nextValue = nextValue.replace(/(?!^-)[^\d]/g, '');
67
+
68
+ return nextValue;
69
+ };
70
+
71
+ const handleKeyDown = (event: KeyboardEvent) => {
72
+ if (shouldRestrictNumberInput() && !(event.ctrlKey || event.metaKey || event.altKey)) {
73
+ if (prohibitFraction && (event.key === '.' || event.key === ',')) {
74
+ event.preventDefault();
75
+ }
76
+
77
+ if (prohibitNegative && event.key === '-') {
78
+ event.preventDefault();
79
+ }
80
+ }
81
+
82
+ onKeyDown?.(event as KeyboardEvent & { currentTarget: EventTarget & HTMLInputElement });
83
+ };
84
+
85
+ const handleInput = (event: Event) => {
86
+ if (!shouldRestrictNumberInput()) {
87
+ return;
88
+ }
89
+
90
+ const target = event.target as HTMLInputElement | null;
91
+ if (!target) {
92
+ return;
93
+ }
94
+
95
+ const sanitizedValue = sanitizeNumberInput(target.value);
96
+ if (sanitizedValue !== target.value) {
97
+ target.value = sanitizedValue;
98
+ value = sanitizedValue;
99
+ }
100
+ };
101
+
44
102
  </script>
45
103
 
46
104
  <input
@@ -49,8 +107,9 @@
49
107
  class={`TextInput ${invalid ? 'invalid' : ''} ${classNames}`}
50
108
  onfocus={onFocus}
51
109
  onblur={onBlur}
52
- onkeydown={onKeyDown}
110
+ onkeydown={handleKeyDown}
53
111
  onkeyup={onKeyUp}
112
+ oninput={handleInput}
54
113
  onchange={onChange}
55
114
  onclick={onClick}
56
115
  {type}
@@ -22,6 +22,8 @@ export interface ITextInputProps {
22
22
  style?: string;
23
23
  classNames?: string;
24
24
  autoFocus?: boolean;
25
+ prohibitFraction?: boolean;
26
+ prohibitNegative?: boolean;
25
27
  ariaHasPopup?: "dialog" | "menu" | "listbox" | "tree" | "grid" | null | undefined;
26
28
  ariaExpanded?: boolean;
27
29
  ariaControls?: string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "milk-lib",
3
3
  "license": "MIT",
4
- "version": "0.0.38",
4
+ "version": "0.0.39",
5
5
  "scripts": {
6
6
  "dev": "vite dev",
7
7
  "build": "vite build && npm run prepack",