From 9aad76bf44ad7f42b0557cec5cdc2eacd844491b Mon Sep 17 00:00:00 2001 From: JSR Date: Wed, 18 Mar 2026 19:05:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=B9=9D=E5=AE=AB=E6=A0=BC?= =?UTF-8?q?=E6=8B=BC=E9=9F=B3=E6=8C=89=E9=94=AE=E5=A4=9A=E5=AD=97=E6=AF=8D?= =?UTF-8?q?=E7=8C=9C=E6=B5=8B=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Home.tsx | 74 +++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/src/components/Home.tsx b/src/components/Home.tsx index be569c5..03f3c6e 100644 --- a/src/components/Home.tsx +++ b/src/components/Home.tsx @@ -24,6 +24,35 @@ interface reason { type SortType = "alphabet" | "surname" | "score" +const T9_KEY_MAP: Record = { + a: "2", + b: "2", + c: "2", + d: "3", + e: "3", + f: "3", + g: "4", + h: "4", + i: "4", + j: "5", + k: "5", + l: "5", + m: "6", + n: "6", + o: "6", + p: "7", + q: "7", + r: "7", + s: "7", + t: "8", + u: "8", + v: "8", + w: "9", + x: "9", + y: "9", + z: "9", +} + export const Home: React.FC<{ canEdit: boolean }> = ({ canEdit }) => { const { t } = useTranslation() const [students, setStudents] = useState([]) @@ -106,17 +135,36 @@ export const Home: React.FC<{ canEdit: boolean }> = ({ canEdit }) => { }, []) const pinyinKeyRows = [ - ["ABC", "DEF", "GHI"], - ["JKL", "MNO", "PQRS"], - ["TUV", "WXYZ", "⌫"], + [ + { digit: "2", letters: "ABC" }, + { digit: "3", letters: "DEF" }, + { digit: "4", letters: "GHI" }, + ], + [ + { digit: "5", letters: "JKL" }, + { digit: "6", letters: "MNO" }, + { digit: "7", letters: "PQRS" }, + ], + [ + { digit: "8", letters: "TUV" }, + { digit: "9", letters: "WXYZ" }, + { digit: "⌫", letters: "" }, + ], ] - const handlePinyinKeyPress = (value: string) => { - if (value === "⌫") { + const toT9Digits = (text: string) => + text + .toLowerCase() + .split("") + .map((ch) => T9_KEY_MAP[ch] || "") + .join("") + + const handlePinyinKeyPress = (digit: string) => { + if (digit === "⌫") { setSearchKeyword((prev) => prev.slice(0, -1)) return } - setSearchKeyword((prev) => `${prev}${value.charAt(0).toLowerCase()}`) + setSearchKeyword((prev) => `${prev}${digit}`) } const getDisplayText = (name: string) => { @@ -135,6 +183,12 @@ export const Home: React.FC<{ canEdit: boolean }> = ({ canEdit }) => { if (pyLower.includes(q0)) return true const q1 = q0.replace(/\s+/g, "") + const isT9Query = /^[2-9]+$/.test(q1) + if (isT9Query) { + const pyDigits = toT9Digits(pyLower.replace(/[^a-z]/g, "")) + if (pyDigits.includes(q1)) return true + } + if ( q1 && (nameLower.replace(/\s+/g, "").includes(q1) || pyLower.replace(/\s+/g, "").includes(q1)) @@ -765,14 +819,14 @@ export const Home: React.FC<{ canEdit: boolean }> = ({ canEdit }) => { key={`pinyin-row-${rowIndex}`} style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: "6px" }} > - {row.map((keyValue) => ( + {row.map((keyItem) => ( ))}