@yourdash/uikit 1.0.8 → 1.0.9
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,84 +1,82 @@
|
|
1
|
-
/*
|
2
|
-
* Copyright ©2024 Ewsgit<https://ewsgit.uk> and YourDash<https://yourdash.ewsgit.uk> contributors.
|
3
|
-
* YourDash is licensed under the MIT License. (https://mit.ewsgit.uk)
|
4
|
-
*/
|
5
|
-
|
6
|
-
import clippy from "@yourdash/shared/web/helpers/clippy.ts";
|
7
|
-
import Icon from "../icon/icon.tsx";
|
8
|
-
import { UKIcon } from "../icon/iconDictionary.ts";
|
9
|
-
import styles from "./textInput.module.scss";
|
10
|
-
import React, { useEffect, useRef, useState } from "react";
|
11
|
-
|
12
|
-
// TODO: maybe remove onEnter for onSubmit
|
13
|
-
|
14
|
-
const TextInputComponent: React.
|
15
|
-
onChange?: (value: string) => void;
|
16
|
-
onSubmit?: (value: string) => void;
|
17
|
-
placeholder: string;
|
18
|
-
icon?: UKIcon;
|
19
|
-
onEnter?: (value: string) => void;
|
20
|
-
defaultValue?: string;
|
21
|
-
value?: string;
|
22
|
-
accessibleName: string;
|
23
|
-
className?: string;
|
24
|
-
type?: string;
|
25
|
-
}> = (props
|
26
|
-
const ref = useRef<HTMLInputElement>(null);
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
e.
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
export default TextInput;
|
1
|
+
/*
|
2
|
+
* Copyright ©2024 Ewsgit<https://ewsgit.uk> and YourDash<https://yourdash.ewsgit.uk> contributors.
|
3
|
+
* YourDash is licensed under the MIT License. (https://mit.ewsgit.uk)
|
4
|
+
*/
|
5
|
+
|
6
|
+
import clippy from "@yourdash/shared/web/helpers/clippy.ts";
|
7
|
+
import Icon from "../icon/icon.tsx";
|
8
|
+
import { UKIcon } from "../icon/iconDictionary.ts";
|
9
|
+
import styles from "./textInput.module.scss";
|
10
|
+
import React, { useEffect, useRef, useState } from "react";
|
11
|
+
|
12
|
+
// TODO: maybe remove onEnter for onSubmit
|
13
|
+
|
14
|
+
const TextInputComponent: React.FC<{
|
15
|
+
onChange?: (value: string) => void;
|
16
|
+
onSubmit?: (value: string) => void;
|
17
|
+
placeholder: string;
|
18
|
+
icon?: UKIcon;
|
19
|
+
onEnter?: (value: string) => void;
|
20
|
+
defaultValue?: string;
|
21
|
+
value?: string;
|
22
|
+
accessibleName: string;
|
23
|
+
className?: string;
|
24
|
+
type?: string;
|
25
|
+
}> = (props) => {
|
26
|
+
const ref = useRef<HTMLInputElement>(null);
|
27
|
+
|
28
|
+
const [value, setValue] = useState(props.defaultValue || "");
|
29
|
+
|
30
|
+
useEffect(() => {
|
31
|
+
if (props.value) setValue(props.value);
|
32
|
+
}, [props.value]);
|
33
|
+
|
34
|
+
useEffect(() => {
|
35
|
+
if (!ref.current) return;
|
36
|
+
|
37
|
+
ref.current.value = value;
|
38
|
+
}, [value]);
|
39
|
+
|
40
|
+
useEffect(() => {
|
41
|
+
setTimeout(() => {
|
42
|
+
if (ref.current?.value !== props.defaultValue) {
|
43
|
+
ref.current?.onkeyup?.({ currentTarget: ref.current } as unknown as KeyboardEvent);
|
44
|
+
ref.current?.onchange?.({ currentTarget: ref.current } as unknown as Event);
|
45
|
+
}
|
46
|
+
}, 200);
|
47
|
+
}, []);
|
48
|
+
|
49
|
+
return (
|
50
|
+
<div className={clippy(styles.component, props.className)}>
|
51
|
+
{props.icon && (
|
52
|
+
<Icon
|
53
|
+
className={styles.icon}
|
54
|
+
icon={props.icon}
|
55
|
+
/>
|
56
|
+
)}
|
57
|
+
<input
|
58
|
+
ref={ref}
|
59
|
+
type={props.type || "text"}
|
60
|
+
aria-label={props.accessibleName}
|
61
|
+
className={clippy(styles.input, !props.icon && styles.noIcon)}
|
62
|
+
placeholder={props.placeholder}
|
63
|
+
onKeyUp={(e) => {
|
64
|
+
setValue(e.currentTarget.value);
|
65
|
+
props.onChange?.(e.currentTarget.value);
|
66
|
+
}}
|
67
|
+
onChange={(e) => props.onSubmit?.(e.currentTarget.value)}
|
68
|
+
onKeyDown={(e) => {
|
69
|
+
if (e.key === "Enter") {
|
70
|
+
e.preventDefault();
|
71
|
+
|
72
|
+
props.onEnter?.(e.currentTarget.value);
|
73
|
+
}
|
74
|
+
}}
|
75
|
+
/>
|
76
|
+
</div>
|
77
|
+
);
|
78
|
+
};
|
79
|
+
|
80
|
+
const TextInput = TextInputComponent;
|
81
|
+
|
82
|
+
export default TextInput;
|