generated from xhofe/solid-lib
-
Notifications
You must be signed in to change notification settings - Fork 355
/
ModalInput.tsx
90 lines (90 loc) · 2.24 KB
/
ModalInput.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import {
Modal,
ModalOverlay,
ModalContent,
ModalHeader,
ModalBody,
ModalFooter,
Button,
Input,
Textarea,
FormHelperText,
} from "@hope-ui/solid";
import { createSignal, Show } from "solid-js";
import { useT } from "~/hooks";
import { notify } from "~/utils";
export type ModalInputProps = {
opened: boolean;
onClose: () => void;
title: string;
onSubmit?: (text: string) => void;
type?: string;
defaultValue?: string;
loading?: boolean;
tips?: string;
};
export const ModalInput = (props: ModalInputProps) => {
const [value, setValue] = createSignal(props.defaultValue ?? "");
const t = useT();
const submit = () => {
if (!value()) {
notify.warning(t("global.empty_input"));
return;
}
props.onSubmit?.(value());
setValue("");
};
return (
<Modal
blockScrollOnMount={false}
opened={props.opened}
onClose={props.onClose}
initialFocus="#modal-input"
>
<ModalOverlay />
<ModalContent>
{/* <ModalCloseButton /> */}
<ModalHeader>{t(props.title)}</ModalHeader>
<ModalBody>
<Show
when={props.type === "text"}
fallback={
<Input
id="modal-input"
type={props.type}
value={value()}
onInput={(e) => {
setValue(e.currentTarget.value);
}}
onKeyDown={(e) => {
if (e.key === "Enter") {
submit();
}
}}
/>
}
>
<Textarea
id="modal-input"
value={value()}
onInput={(e) => {
setValue(e.currentTarget.value);
}}
/>
</Show>
<Show when={props.tips}>
<FormHelperText>{props.tips}</FormHelperText>
</Show>
</ModalBody>
<ModalFooter display="flex" gap="$2">
<Button onClick={props.onClose} colorScheme="neutral">
{t("global.cancel")}
</Button>
<Button loading={props.loading} onClick={() => submit()}>
{t("global.ok")}
</Button>
</ModalFooter>
</ModalContent>
</Modal>
);
};