milk-lib 0.0.37 → 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.
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import { getContext } from "svelte";
|
|
2
|
+
import { getContext, onMount } from "svelte";
|
|
3
3
|
import type {IAccordionContentProps, IAccordionItemContext} from "./Accordion.types";
|
|
4
4
|
|
|
5
5
|
let { children }: IAccordionContentProps = $props();
|
|
6
6
|
const { isOpen } = getContext<IAccordionItemContext>('accordion-item-context');
|
|
7
7
|
let accordionContent: HTMLDivElement | null = $state(null);
|
|
8
|
-
let height = $
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
let height = $state(0);
|
|
9
|
+
|
|
10
|
+
const recalcHeight = () => {
|
|
11
|
+
height = accordionContent?.getBoundingClientRect?.().height ?? 0;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
$effect(() => {
|
|
15
|
+
recalcHeight();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
onMount(() => {
|
|
19
|
+
window.addEventListener('resize', recalcHeight);
|
|
20
|
+
return () => window.removeEventListener('resize', recalcHeight);
|
|
21
|
+
});
|
|
13
22
|
|
|
14
23
|
</script>
|
|
15
24
|
|
|
@@ -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={
|
|
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;
|