Skip to content

Commit 5f7e1f0

Browse files
committed
アップロード忘れ
1 parent 3f0702c commit 5f7e1f0

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

docs/public/demo/error-test.html

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<!doctype html>
2+
<html lang="ja">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>TextInputGuard</title>
6+
<link rel="stylesheet" href="./css/demo.css" />
7+
</head>
8+
<style>
9+
.error-bubble {
10+
display: none;
11+
margin-top: 6px;
12+
padding: 8px 12px;
13+
border: 1px solid var(--invalid-border);
14+
border-radius: 8px;
15+
background: var(--invalid-bg);
16+
color: var(--text);
17+
font-size: 14px;
18+
line-height: 1.4;
19+
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
20+
position: relative;
21+
max-width: 420px;
22+
}
23+
24+
.error-bubble.show {
25+
display: block;
26+
}
27+
28+
/* 吹き出しの三角 */
29+
.error-bubble::before {
30+
content: "";
31+
position: absolute;
32+
top: -8px;
33+
left: 16px;
34+
border-width: 0 8px 8px 8px;
35+
border-style: solid;
36+
border-color: transparent transparent var(--invalid-border) transparent;
37+
}
38+
39+
.error-bubble::after {
40+
content: "";
41+
position: absolute;
42+
top: -7px;
43+
left: 16px;
44+
border-width: 0 8px 8px 8px;
45+
border-style: solid;
46+
border-color: transparent transparent var(--invalid-bg) transparent;
47+
}
48+
</style>
49+
<body>
50+
<label>
51+
<input id="name" type="text" />
52+
<div id="name-errors" class="error-bubble" aria-live="polite"></div>
53+
</label>
54+
55+
<script type="module">
56+
import { attach, rules } from "./lib/text-input-guard.min.js";
57+
58+
const input = document.getElementById("name");
59+
const errorBox = document.getElementById("name-errors");
60+
61+
function formatErrorMessage(error) {
62+
switch (error.code) {
63+
case "length.max_overflow":
64+
return `文字数が多すぎます(最大 ${error.detail?.limit} 文字、現在 ${error.detail?.actual} 文字)`;
65+
case "bytes.max_overflow":
66+
return `文字容量が大きすぎます(最大 ${error.detail?.limit} バイト、現在 ${error.detail?.actual} バイト)`;
67+
default:
68+
return error.code;
69+
}
70+
}
71+
72+
function renderErrors(errors) {
73+
errorBox.innerHTML = "";
74+
75+
if (!errors || errors.length === 0) {
76+
errorBox.classList.remove("show");
77+
return;
78+
}
79+
80+
const ul = document.createElement("ul");
81+
ul.style.margin = "0";
82+
ul.style.paddingLeft = "1.2em";
83+
84+
for (const error of errors) {
85+
const li = document.createElement("li");
86+
li.textContent = formatErrorMessage(error);
87+
ul.appendChild(li);
88+
}
89+
90+
errorBox.appendChild(ul);
91+
errorBox.classList.add("show");
92+
}
93+
94+
attach(input, {
95+
rules: [
96+
rules.length({
97+
max: 5,
98+
mode: "error",
99+
unit: "grapheme"
100+
}),
101+
rules.bytes({
102+
max: 5,
103+
mode: "error",
104+
unit: "utf-8"
105+
})
106+
],
107+
onValidate(result) {
108+
renderErrors(result.errors);
109+
}
110+
});
111+
</script>
112+
</body>
113+
</html>

0 commit comments

Comments
 (0)