milk-lib 0.0.38 → 0.0.40

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