下拉控件迁入 Radix primitives:新建 components/primitives(Popup + CVA 尺子 + 共用 listbox 键盘 hook),Select/MultiSelect 全部改由 Radix Popover 接管浮层行为——面板宽度至少等于触发器、按最长选项展开到 440px 不再换行,两档触发器同字号同圆角,翻边/滚动/外点关闭交给 Radix;顺手修掉「点触发器自身关不掉面板」的 anchor 误判(Radix 把 handler 挂在真实被点元素上,原先的 currentTarget 比较恒真),并补上多选的首字母定位。表单公共 API 一字未改,页面 87 处调用零改动;旧的 use-popup/parts 手工定位已归档。
parent
34f8862cfe
commit
c5edd65d82
|
|
@ -8,8 +8,12 @@
|
|||
"name": "nex-animation-studio",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"@radix-ui/react-checkbox": "^1.4.1",
|
||||
"@radix-ui/react-dialog": "^1.3.1",
|
||||
"@radix-ui/react-popover": "^1.3.1",
|
||||
"@tailwindcss/vite": "^4.3.3",
|
||||
"@vitejs/plugin-react": "^5.2.0",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
"react": "^19.3.1",
|
||||
"react-dom": "^19.3.1",
|
||||
|
|
@ -306,6 +310,44 @@
|
|||
"node": ">=6.9.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@floating-ui/core": {
|
||||
"version": "1.8.0",
|
||||
"resolved": "https://registry.npmmirror.com/@floating-ui/core/-/core-1.8.0.tgz",
|
||||
"integrity": "sha512-0CIZ5itps/8x7BG8dEIhs53BvCUH2PCoogtakwRTut+Arm58sJooJ0AuZhLw2HJYIR5cMLNPBSS728sPho2khQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@floating-ui/utils": "^0.2.12"
|
||||
}
|
||||
},
|
||||
"node_modules/@floating-ui/dom": {
|
||||
"version": "1.8.0",
|
||||
"resolved": "https://registry.npmmirror.com/@floating-ui/dom/-/dom-1.8.0.tgz",
|
||||
"integrity": "sha512-yXSrzeHZBTZadLOlfyhCkJHNeLJnHRnRInwdZ40L7ZiaAtrBwoYlsDrX3v5zB1Utk7CLfzcOVnVVWoXEky7Ceg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@floating-ui/core": "^1.8.0",
|
||||
"@floating-ui/utils": "^0.2.12"
|
||||
}
|
||||
},
|
||||
"node_modules/@floating-ui/react-dom": {
|
||||
"version": "2.1.9",
|
||||
"resolved": "https://registry.npmmirror.com/@floating-ui/react-dom/-/react-dom-2.1.9.tgz",
|
||||
"integrity": "sha512-JDjEFGCpImxDCA7JJKviA0M9+RtmJdj0m/NVU5IMgBK+AmZouAQQ7/+2GLH0GXXY0YMw9oXPB8hKdbPYg5QLYg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@floating-ui/dom": "^1.8.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": ">=16.8.0",
|
||||
"react-dom": ">=16.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@floating-ui/utils": {
|
||||
"version": "0.2.12",
|
||||
"resolved": "https://registry.npmmirror.com/@floating-ui/utils/-/utils-0.2.12.tgz",
|
||||
"integrity": "sha512-HpCo8tmWzLVad5s2d19EhAz5zqrrQ6s69qd6moPMQvkOuSwDT1YgRfWSVuc4ennqrgv3OHppiOGMQ7oC13yIww==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@jridgewell/gen-mapping": {
|
||||
"version": "0.3.13",
|
||||
"resolved": "https://registry.npmmirror.com/@jridgewell/gen-mapping/-/gen-mapping-0.3.13.tgz",
|
||||
|
|
@ -360,6 +402,487 @@
|
|||
"url": "https://github.com/sponsors/oxc-project"
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/primitive": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/primitive/-/primitive-1.2.0.tgz",
|
||||
"integrity": "sha512-KABY+VMIRPJL+Vyzv/335FEhXXD/xDv8BMb1kl48/aXJZMH0LtHCTx5ga5RnxaEP3nZHtmTLf1P9vanmFX46bw==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@radix-ui/react-arrow": {
|
||||
"version": "1.1.17",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-arrow/-/react-arrow-1.1.17.tgz",
|
||||
"integrity": "sha512-67jjcVvxjXCCStnTW3Z+M7jmaFMuMR7M2JgpDzH4w7JSRrikmzhjfYgP/09ECb8n7whnsgh1ifJ+Ie96qEj+pQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/react-primitive": "2.2.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"@types/react-dom": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
|
||||
"react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
},
|
||||
"@types/react-dom": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-checkbox": {
|
||||
"version": "1.4.1",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-checkbox/-/react-checkbox-1.4.1.tgz",
|
||||
"integrity": "sha512-KWtwhh+98ML3seZ9vO9yRY2OwaQG90dCiDAxjpSNY2AaZ3Hik3B0dsk6m0ILywo/no3A9/SozadyPMNweVT8yw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/primitive": "1.2.0",
|
||||
"@radix-ui/react-compose-refs": "1.1.5",
|
||||
"@radix-ui/react-context": "1.3.0",
|
||||
"@radix-ui/react-presence": "1.1.11",
|
||||
"@radix-ui/react-primitive": "2.2.0",
|
||||
"@radix-ui/react-use-controllable-state": "1.3.0",
|
||||
"@radix-ui/react-use-size": "1.1.5"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"@types/react-dom": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
|
||||
"react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
},
|
||||
"@types/react-dom": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-compose-refs": {
|
||||
"version": "1.1.5",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-compose-refs/-/react-compose-refs-1.1.5.tgz",
|
||||
"integrity": "sha512-+48PbAAbq3didjJxa+OaWY2ZwgAKsNiRGyeHKszblZMQ+kcpd9pAaT11cMkGEie0vsOi3QdeTE6d5Fe3Gn61kA==",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-context": {
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-context/-/react-context-1.3.0.tgz",
|
||||
"integrity": "sha512-v5AYEocMISkbJ+1fTXzZpiFv7NRr3D2WiYbLIKf+FkmJ8VkP6d503eV2n34MFussHXY1pcoq3yfJiQH77dc4Mg==",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-dialog": {
|
||||
"version": "1.3.1",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-dialog/-/react-dialog-1.3.1.tgz",
|
||||
"integrity": "sha512-+0E4qvVTwjAWra9sEhJ+9BTsvHE00zkUuNw1VGh7S+Gc13dfYk+S1PVQdchWDj+XYHhAai7Z7IpzXVDCGsDplg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/primitive": "1.2.0",
|
||||
"@radix-ui/react-compose-refs": "1.1.5",
|
||||
"@radix-ui/react-context": "1.3.0",
|
||||
"@radix-ui/react-dismissable-layer": "1.2.1",
|
||||
"@radix-ui/react-focus-guards": "1.2.0",
|
||||
"@radix-ui/react-focus-scope": "1.3.1",
|
||||
"@radix-ui/react-id": "1.1.4",
|
||||
"@radix-ui/react-portal": "1.2.0",
|
||||
"@radix-ui/react-presence": "1.1.11",
|
||||
"@radix-ui/react-primitive": "2.2.0",
|
||||
"@radix-ui/react-slot": "1.5.0",
|
||||
"@radix-ui/react-use-controllable-state": "1.3.0",
|
||||
"@radix-ui/react-use-layout-effect": "1.1.4",
|
||||
"aria-hidden": "^1.2.4",
|
||||
"react-remove-scroll": "^2.7.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"@types/react-dom": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
|
||||
"react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
},
|
||||
"@types/react-dom": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-dismissable-layer": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-dismissable-layer/-/react-dismissable-layer-1.2.1.tgz",
|
||||
"integrity": "sha512-KIfDcAsx+oVylFZ4xryVKusJUrLLhTQmfnJW4kwk4Km5myvhHCrj04hnWWUC166+xD4DHdH8tQGylwYX1h6+Pg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/primitive": "1.2.0",
|
||||
"@radix-ui/react-compose-refs": "1.1.5",
|
||||
"@radix-ui/react-primitive": "2.2.0",
|
||||
"@radix-ui/react-use-callback-ref": "1.1.5",
|
||||
"@radix-ui/react-use-effect-event": "0.0.5"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"@types/react-dom": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
|
||||
"react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
},
|
||||
"@types/react-dom": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-focus-guards": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-focus-guards/-/react-focus-guards-1.2.0.tgz",
|
||||
"integrity": "sha512-MPm5R0iRGUDrDnPNxmLBkv+X7pN4XKzt+MbWJEuJQsH3NFH4omrbpXpucyYCCKTSt+na/ZxGhEWQrWTZ0285cA==",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-focus-scope": {
|
||||
"version": "1.3.1",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-focus-scope/-/react-focus-scope-1.3.1.tgz",
|
||||
"integrity": "sha512-ugeMhbXt4y5S2T1GnIqbWDKEN962PDwnX1fN0eKOOgf5f63Dh2mn8F1FUySlgW0iF6GiVsRF32DzuDIWTUyQJg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/react-compose-refs": "1.1.5",
|
||||
"@radix-ui/react-primitive": "2.2.0",
|
||||
"@radix-ui/react-use-callback-ref": "1.1.5"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"@types/react-dom": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
|
||||
"react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
},
|
||||
"@types/react-dom": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-id": {
|
||||
"version": "1.1.4",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-id/-/react-id-1.1.4.tgz",
|
||||
"integrity": "sha512-TMQp2llA+RYn7JcjnrMnz7wN4pcVttPZnRZo52PLQsoLVKzNlVwUeHmfePgTgRluXFvlD3GD5g5MOVVTJCO0qA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/react-use-layout-effect": "1.1.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-popover": {
|
||||
"version": "1.3.1",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-popover/-/react-popover-1.3.1.tgz",
|
||||
"integrity": "sha512-ZNxvXRx5W2Wos4fXCXb5lpdqN0Z5chf+mStOUOTr4ik7d+62MRvoe9rzCA/eGNg36lxXqSrKEKmwuZEcUrIstw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/primitive": "1.2.0",
|
||||
"@radix-ui/react-compose-refs": "1.1.5",
|
||||
"@radix-ui/react-context": "1.3.0",
|
||||
"@radix-ui/react-dismissable-layer": "1.2.1",
|
||||
"@radix-ui/react-focus-guards": "1.2.0",
|
||||
"@radix-ui/react-focus-scope": "1.3.1",
|
||||
"@radix-ui/react-id": "1.1.4",
|
||||
"@radix-ui/react-popper": "1.4.0",
|
||||
"@radix-ui/react-portal": "1.2.0",
|
||||
"@radix-ui/react-presence": "1.1.11",
|
||||
"@radix-ui/react-primitive": "2.2.0",
|
||||
"@radix-ui/react-slot": "1.5.0",
|
||||
"@radix-ui/react-use-controllable-state": "1.3.0",
|
||||
"@radix-ui/react-use-layout-effect": "1.1.4",
|
||||
"aria-hidden": "^1.2.4",
|
||||
"react-remove-scroll": "^2.7.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"@types/react-dom": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
|
||||
"react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
},
|
||||
"@types/react-dom": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-popper": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-popper/-/react-popper-1.4.0.tgz",
|
||||
"integrity": "sha512-yCWRvLeGv9Pz3+efwHqGPp70mZ56r3kfIgQVF56Oj2aAa4uP/Tb+IE59wNcc+758rctqbCKx4LbK8II/r7Bn0Q==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@floating-ui/react-dom": "^2.0.0",
|
||||
"@radix-ui/react-arrow": "1.1.17",
|
||||
"@radix-ui/react-compose-refs": "1.1.5",
|
||||
"@radix-ui/react-context": "1.3.0",
|
||||
"@radix-ui/react-primitive": "2.2.0",
|
||||
"@radix-ui/react-use-callback-ref": "1.1.5",
|
||||
"@radix-ui/react-use-layout-effect": "1.1.4",
|
||||
"@radix-ui/react-use-rect": "1.1.4",
|
||||
"@radix-ui/react-use-size": "1.1.5",
|
||||
"@radix-ui/rect": "1.1.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"@types/react-dom": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
|
||||
"react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
},
|
||||
"@types/react-dom": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-portal": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-portal/-/react-portal-1.2.0.tgz",
|
||||
"integrity": "sha512-StX7xXnaRU41VvFtMm9zlhVGz8XF6GmWWTXftkQaY+tqVkbs/R9UmAp5mLzHHapk5hM9RMZQvO/3usyIP/g42g==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/react-primitive": "2.2.0",
|
||||
"@radix-ui/react-use-layout-effect": "1.1.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"@types/react-dom": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
|
||||
"react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
},
|
||||
"@types/react-dom": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-presence": {
|
||||
"version": "1.1.11",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-presence/-/react-presence-1.1.11.tgz",
|
||||
"integrity": "sha512-Z7/atlkab36Z8hm+V8zYc79r0W+tiOB1o0tun1By94JyadkekLQmtEAnBSP9XKXBNQsStoStS4+gX10JpS5akA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/react-use-layout-effect": "1.1.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"@types/react-dom": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
|
||||
"react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
},
|
||||
"@types/react-dom": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-primitive": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-primitive/-/react-primitive-2.2.0.tgz",
|
||||
"integrity": "sha512-HCTSPMNqv55Qk0IX4geJbpyXazmqulcimwuyjxTkKi29v+QZbG2Zho9BnbBq4YS2PF+KlNb2CXoLz66uUmblkg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/react-slot": "1.5.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"@types/react-dom": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
|
||||
"react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
},
|
||||
"@types/react-dom": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-slot": {
|
||||
"version": "1.5.0",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-slot/-/react-slot-1.5.0.tgz",
|
||||
"integrity": "sha512-tyYd9ddPXAysAbdbD9eYxgwpCSv6f3PncqVSNKQFSR5lcJVLz3cb/pANDoYrLiHBpXv7skortyUgGUWsYuPPFQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/react-compose-refs": "1.1.5"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-use-callback-ref": {
|
||||
"version": "1.1.5",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-use-callback-ref/-/react-use-callback-ref-1.1.5.tgz",
|
||||
"integrity": "sha512-Y5GBf4NKeADoXI1geiMlMuMoURT3k9MizzzKhGb/m55H94aGigZ01PIpkvsO6lML13BA6XYAXSJ6gza7vTn5nw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/react-use-layout-effect": "1.1.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-use-controllable-state": {
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-use-controllable-state/-/react-use-controllable-state-1.3.0.tgz",
|
||||
"integrity": "sha512-TAcBI7bw2ljYCbv4+zuTSd6zICu8pRt6SDN2vJ/Z06K2MXh6I9WYFe/A9ovlFDtR5HVHGm5iDxDoABm/++dbKg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/primitive": "1.2.0",
|
||||
"@radix-ui/react-use-effect-event": "0.0.5",
|
||||
"@radix-ui/react-use-layout-effect": "1.1.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-use-effect-event": {
|
||||
"version": "0.0.5",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-use-effect-event/-/react-use-effect-event-0.0.5.tgz",
|
||||
"integrity": "sha512-7cshFL8HGS/7HEiHH+9kL9HBwp2sa9yX18Knwek6KYWmXwM7pegMgta2AXMQKI+rq3JnfSj9x8wYqFMTdG1Jgg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/react-use-layout-effect": "1.1.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-use-layout-effect": {
|
||||
"version": "1.1.4",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-use-layout-effect/-/react-use-layout-effect-1.1.4.tgz",
|
||||
"integrity": "sha512-K20DkRkUwDnxEYMBPcg3Y6voLkEy5p5QQmszZgLngKKiC7dzBR/aEuK3w1qlx2JWDUNH6FluahYdgR3BP+QbYw==",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-use-rect": {
|
||||
"version": "1.1.4",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-use-rect/-/react-use-rect-1.1.4.tgz",
|
||||
"integrity": "sha512-cSOCh6JlkmfjLyNcLiu2nB4v+nm+dkZ+Q5KHWk/soo4U7ZLiEQFKHK9/YmtBHjfCEaU43IBKQOc4/uJmCaiCTQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/rect": "1.1.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/react-use-size": {
|
||||
"version": "1.1.5",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/react-use-size/-/react-use-size-1.1.5.tgz",
|
||||
"integrity": "sha512-V1McDQkMGzgivwqdu7u96APs7oi0Jvj9LTzgq3WWrDZqxWdAqU9yZnZQqk5OizGFR8badUQwZsrlao7MOlWYOA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@radix-ui/react-use-layout-effect": "1.1.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@radix-ui/rect": {
|
||||
"version": "1.1.3",
|
||||
"resolved": "https://registry.npmmirror.com/@radix-ui/rect/-/rect-1.1.3.tgz",
|
||||
"integrity": "sha512-JtyZR+mqgBibTo8xea3B6ZRmzZiM/YeVBtUkas6zMuXjAlfIFIW2FgqeM9eLyvEaYX66vr6DJMK+4U6LV0KhNw==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@rolldown/binding-android-arm-eabi": {
|
||||
"version": "1.2.13",
|
||||
"resolved": "https://registry.npmmirror.com/@rolldown/binding-android-arm-eabi/-/binding-android-arm-eabi-1.2.13.tgz",
|
||||
|
|
@ -1167,7 +1690,7 @@
|
|||
"version": "19.3.0",
|
||||
"resolved": "https://registry.npmmirror.com/@types/react-dom/-/react-dom-19.3.0.tgz",
|
||||
"integrity": "sha512-ZI7bU42mZXXKHn/qNLEw2IrbiINU7X5+vfgdixBHkCNpYWXjKgfQ/P+uyGb5CjOLB9UcnTeg3rylQtV2hym44Q==",
|
||||
"dev": true,
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"@types/react": "^19.3.0"
|
||||
|
|
@ -1199,6 +1722,18 @@
|
|||
"integrity": "sha512-eybk3TjzzzV97Dlj5c+XrBFW57eTNhzod66y9HrBlzJ6NsCrWCp/2kaPS3K9wJmurBC0Tdw4yPjXKZqlznim3Q==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/aria-hidden": {
|
||||
"version": "1.2.6",
|
||||
"resolved": "https://registry.npmmirror.com/aria-hidden/-/aria-hidden-1.2.6.tgz",
|
||||
"integrity": "sha512-ik3ZgC9dY/lYVVM++OISsaYDeg1tb0VtP5uL3ouh1koGOaUMDPpbFIei4JkFimWUFPn90sbMNMXQAIVOlnYKJA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/baseline-browser-mapping": {
|
||||
"version": "2.11.34",
|
||||
"resolved": "https://registry.npmmirror.com/baseline-browser-mapping/-/baseline-browser-mapping-2.11.34.tgz",
|
||||
|
|
@ -1264,6 +1799,18 @@
|
|||
],
|
||||
"license": "CC-BY-4.0"
|
||||
},
|
||||
"node_modules/class-variance-authority": {
|
||||
"version": "0.7.1",
|
||||
"resolved": "https://registry.npmmirror.com/class-variance-authority/-/class-variance-authority-0.7.1.tgz",
|
||||
"integrity": "sha512-Ka+9Trutv7G8M6WT6SeiRWz792K5qEqIGEGzXKhAE6xOWAY6pPH8U+9IY3oCMv6kqTmLsv7Xh/2w2RigkePMsg==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"clsx": "^2.1.1"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://polar.sh/cva"
|
||||
}
|
||||
},
|
||||
"node_modules/clsx": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmmirror.com/clsx/-/clsx-2.1.1.tgz",
|
||||
|
|
@ -1325,6 +1872,12 @@
|
|||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/detect-node-es": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmmirror.com/detect-node-es/-/detect-node-es-1.1.0.tgz",
|
||||
"integrity": "sha512-ypdmJU/TbBby2Dxibuv7ZLW3Bs1QEmM7nHjEANfohJLvE0XVujisn1qPJcZxg+qDucsr+bP6fLD1rPS3AhJ7EQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/electron-to-chromium": {
|
||||
"version": "1.5.472",
|
||||
"resolved": "https://registry.npmmirror.com/electron-to-chromium/-/electron-to-chromium-1.5.472.tgz",
|
||||
|
|
@ -1393,6 +1946,15 @@
|
|||
"node": ">=6.9.0"
|
||||
}
|
||||
},
|
||||
"node_modules/get-nonce": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmmirror.com/get-nonce/-/get-nonce-1.0.1.tgz",
|
||||
"integrity": "sha512-FJhYRoDaiatfEkUK8HKlicmu/3SGFD51q3itKDGoSTysQJBnfOcxU5GxnhE1E6soB76MbT0MBtnKJuXyAx+96Q==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/graceful-fs": {
|
||||
"version": "4.2.11",
|
||||
"resolved": "https://registry.npmmirror.com/graceful-fs/-/graceful-fs-4.2.11.tgz",
|
||||
|
|
@ -1814,6 +2376,53 @@
|
|||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/react-remove-scroll": {
|
||||
"version": "2.7.2",
|
||||
"resolved": "https://registry.npmmirror.com/react-remove-scroll/-/react-remove-scroll-2.7.2.tgz",
|
||||
"integrity": "sha512-Iqb9NjCCTt6Hf+vOdNIZGdTiH1QSqr27H/Ek9sv/a97gfueI/5h1s3yRi1nngzMUaOOToin5dI1dXKdXiF+u0Q==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"react-remove-scroll-bar": "^2.3.7",
|
||||
"react-style-singleton": "^2.2.3",
|
||||
"tslib": "^2.1.0",
|
||||
"use-callback-ref": "^1.3.3",
|
||||
"use-sidecar": "^1.1.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/react-remove-scroll-bar": {
|
||||
"version": "2.3.8",
|
||||
"resolved": "https://registry.npmmirror.com/react-remove-scroll-bar/-/react-remove-scroll-bar-2.3.8.tgz",
|
||||
"integrity": "sha512-9r+yi9+mgU33AKcj6IbT9oRCO78WriSj6t/cF8DWBZJ9aOGPOTEDvdUDz1FwKim7QXWwmHqtdHnRJfhAxEG46Q==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"react-style-singleton": "^2.2.2",
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/react-router": {
|
||||
"version": "7.18.5",
|
||||
"resolved": "https://registry.npmmirror.com/react-router/-/react-router-7.18.5.tgz",
|
||||
|
|
@ -1852,6 +2461,28 @@
|
|||
"react-dom": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/react-style-singleton": {
|
||||
"version": "2.2.3",
|
||||
"resolved": "https://registry.npmmirror.com/react-style-singleton/-/react-style-singleton-2.2.3.tgz",
|
||||
"integrity": "sha512-b6jSvxvVnyptAiLjbkWLE/lOnR4lfTtDAl+eUC7RZy+QQWc6wRzIV2CE6xBuMmDxc2qIihtDCZD5NPOFl7fRBQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"get-nonce": "^1.0.0",
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/rolldown": {
|
||||
"version": "1.2.13",
|
||||
"resolved": "https://registry.npmmirror.com/rolldown/-/rolldown-1.2.13.tgz",
|
||||
|
|
@ -1951,6 +2582,12 @@
|
|||
"url": "https://github.com/sponsors/SuperchupuDev"
|
||||
}
|
||||
},
|
||||
"node_modules/tslib": {
|
||||
"version": "2.9.1",
|
||||
"resolved": "https://registry.npmmirror.com/tslib/-/tslib-2.9.1.tgz",
|
||||
"integrity": "sha512-a4Ux9VKuIMHsOS7ENbEq6hK2ZGYIT27Rtl0E4eD9KqwxhKiQeMZE/9xb3IdYwWaSOyIBQLExhQgXDTYoiCxnWA==",
|
||||
"license": "0BSD"
|
||||
},
|
||||
"node_modules/typescript": {
|
||||
"version": "5.6.3",
|
||||
"resolved": "https://registry.npmmirror.com/typescript/-/typescript-5.6.3.tgz",
|
||||
|
|
@ -1995,6 +2632,49 @@
|
|||
"browserslist": ">= 4.21.0"
|
||||
}
|
||||
},
|
||||
"node_modules/use-callback-ref": {
|
||||
"version": "1.3.3",
|
||||
"resolved": "https://registry.npmmirror.com/use-callback-ref/-/use-callback-ref-1.3.3.tgz",
|
||||
"integrity": "sha512-jQL3lRnocaFtu3V00JToYz/4QkNWswxijDaCVNZRiRTO3HQDLsdu1ZtmIUvV4yPp+rvWm5j0y0TG/S61cuijTg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/use-sidecar": {
|
||||
"version": "1.1.3",
|
||||
"resolved": "https://registry.npmmirror.com/use-sidecar/-/use-sidecar-1.1.3.tgz",
|
||||
"integrity": "sha512-Fedw0aZvkhynoPYlA5WXrMCAMm+nSWdZt6lzJQ7Ok8S6Q+VsHmHpRWndVRJ8Be0ZbkfPc5LRYH+5XrzXcEeLRQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"detect-node-es": "^1.1.0",
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "*",
|
||||
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^19.0.0-rc"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/vite": {
|
||||
"version": "8.3.4",
|
||||
"resolved": "https://registry.npmmirror.com/vite/-/vite-8.3.4.tgz",
|
||||
|
|
|
|||
|
|
@ -10,8 +10,12 @@
|
|||
"preview": "vite preview --host 0.0.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@radix-ui/react-checkbox": "^1.4.1",
|
||||
"@radix-ui/react-dialog": "^1.3.1",
|
||||
"@radix-ui/react-popover": "^1.3.1",
|
||||
"@tailwindcss/vite": "^4.3.3",
|
||||
"@vitejs/plugin-react": "^5.2.0",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
"react": "^19.3.1",
|
||||
"react-dom": "^19.3.1",
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { CARET_PATH, caretCls } from './parts'
|
||||
import { CARET_PATH, caretCls } from '../primitives'
|
||||
|
||||
/** 统一的下拉箭头(Select / MultiSelect 共用一份几何) */
|
||||
export function Caret({ open, className }: { open: boolean; className?: string }) {
|
||||
|
|
|
|||
|
|
@ -1,14 +1,16 @@
|
|||
import { useEffect, useId, useMemo, useRef, useState } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import { cn } from '../../lib/cn'
|
||||
import {
|
||||
Popup, barLayer, chipButtonLayer, countBadgeLayer, emptyLayer, itemCls, listLayer,
|
||||
panelInputLayer, tickBoxLayer, triggerCls, useListbox
|
||||
} from '../primitives'
|
||||
import { sameValue, summaryOf, toOptions, type OptionLike } from './options'
|
||||
import { contentWidth, usePopup } from './use-popup'
|
||||
import { filterBarCls, itemCls, listCls, noneCls, popCls, triggerCls } from './parts'
|
||||
import { Caret } from './Caret'
|
||||
|
||||
/**
|
||||
* 全站统一「多选下拉」(替代原生 <select multiple> 的系统列表外观)。
|
||||
* 与 Select 同一套视觉语言:复选列表 + 全选/清空 + 过滤框。
|
||||
* 与 Select 共用 primitives:同一个 Popup、同一份键盘实现、同一套皮肤,
|
||||
* 差别只在「选了不关闭」和行首那个勾选框。
|
||||
* 回显超过 30 字退化成「首项 等 N 项」,否则触发器会被撑出整行。
|
||||
*/
|
||||
export type MultiSelectProps = {
|
||||
|
|
@ -30,12 +32,12 @@ export function MultiSelect({
|
|||
const picked = useMemo(() => (Array.isArray(value) ? value : []), [value])
|
||||
const opts = useMemo(() => toOptions(options), [options])
|
||||
const uid = useId()
|
||||
const cw = useMemo(() => contentWidth(opts.map((o) => o.label), 62), [opts])
|
||||
const { open, setOpen, triggerRef, popRef, box } = usePopup({ minW: 190, maxW: 440, listMinH: 210, maxHCap: 320, contentW: cw, disabled })
|
||||
const listRef = useRef<HTMLDivElement | null>(null)
|
||||
const listId = `${uid}-list`
|
||||
const boxRef = useRef<HTMLSpanElement | null>(null)
|
||||
const triggerRef = useRef<HTMLButtonElement | null>(null)
|
||||
const searchRef = useRef<HTMLInputElement | null>(null)
|
||||
const [open, setOpen] = useState(false)
|
||||
const [query, setQuery] = useState('')
|
||||
const [active, setActive] = useState(-1)
|
||||
|
||||
const isSelected = (v: any) => picked.some((p) => sameValue(p, v))
|
||||
const labels = opts.filter((o) => isSelected(o.value)).map((o) => o.label)
|
||||
|
|
@ -49,16 +51,48 @@ export function MultiSelect({
|
|||
const selectableInView = view.filter((i) => !opts[i].disabled).length
|
||||
const searchable = searchThreshold > 0 && opts.length > searchThreshold
|
||||
|
||||
const scrollActive = () => listRef.current?.querySelector<HTMLElement>('[data-act="1"]')?.scrollIntoView({ block: 'nearest' })
|
||||
useEffect(() => { if (open) queueMicrotask(scrollActive) }, [open, active])
|
||||
const lb = useListbox({
|
||||
view,
|
||||
isDisabled: (i) => !!opts[i]?.disabled,
|
||||
labelOf: (i) => opts[i]?.label ?? '',
|
||||
open,
|
||||
onPick: toggleIndex,
|
||||
onOpen: openList,
|
||||
onClose: closeList,
|
||||
initialActive: () => {
|
||||
const hit = view.findIndex((i) => isSelected(opts[i].value))
|
||||
return hit >= 0 ? hit : (view.find((i) => !opts[i].disabled) ?? -1)
|
||||
}
|
||||
})
|
||||
|
||||
// 只盯 query:opts/view 每次渲染都是新引用,列进依赖会把高亮反复重置回第一行
|
||||
useEffect(() => {
|
||||
if (query) setActive(view.find((i) => !opts[i].disabled) ?? -1)
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
if (query) lb.setActive(view.find((i) => !opts[i]?.disabled) ?? -1)
|
||||
}, [query])
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
;(searchable ? searchRef : triggerRef).current?.focus({ preventScroll: true })
|
||||
}, [open, searchable])
|
||||
|
||||
function openList() {
|
||||
if (disabled) return
|
||||
setQuery('')
|
||||
setOpen(true)
|
||||
lb.prime()
|
||||
}
|
||||
|
||||
/** refocus=false:外点到页面上的别的控件时,别把焦点抢回来,否则点不动下一个字段 */
|
||||
function closeList(refocus = true) {
|
||||
setQuery('')
|
||||
setOpen(false)
|
||||
if (refocus) triggerRef.current?.focus({ preventScroll: true })
|
||||
}
|
||||
|
||||
function commit(next: any[]) {
|
||||
onChange(next)
|
||||
if (open) queueMicrotask(scrollActive)
|
||||
// 勾完一项后行高可能变化(长标签换行),等这一轮 DOM 落地再校准可见区
|
||||
if (open) queueMicrotask(() => lb.scrollActive())
|
||||
}
|
||||
|
||||
function toggleIndex(i: number) {
|
||||
|
|
@ -78,108 +112,60 @@ export function MultiSelect({
|
|||
}
|
||||
}
|
||||
|
||||
function toggle(force?: boolean) {
|
||||
if (disabled) return
|
||||
const next = force ?? !open
|
||||
if (!next) { setQuery(''); setOpen(false); triggerRef.current?.focus({ preventScroll: true }); return }
|
||||
setQuery('')
|
||||
const first = view.findIndex((i) => isSelected(opts[i].value))
|
||||
setActive(first >= 0 ? first : view.find((i) => !opts[i].disabled) ?? -1)
|
||||
setOpen(true)
|
||||
queueMicrotask(() => { if (searchable) searchRef.current?.focus({ preventScroll: true }); else triggerRef.current?.focus({ preventScroll: true }) })
|
||||
}
|
||||
|
||||
function move(key: 'ArrowDown' | 'ArrowUp' | 'Home' | 'End') {
|
||||
if (!view.length) return
|
||||
let pos = view.indexOf(active)
|
||||
const step = (delta: number) => {
|
||||
for (let n = 0; n < view.length; n++) {
|
||||
pos = (pos + delta + view.length) % view.length
|
||||
if (!opts[view[pos]].disabled) return
|
||||
}
|
||||
}
|
||||
if (key === 'ArrowDown') step(1)
|
||||
else if (key === 'ArrowUp') step(-1)
|
||||
else if (key === 'Home') pos = 0
|
||||
else pos = view.length - 1
|
||||
setActive(view[pos] ?? -1)
|
||||
}
|
||||
|
||||
function onKeydown(e: React.KeyboardEvent) {
|
||||
if ((e.target as HTMLElement | null)?.tagName === 'INPUT') return
|
||||
if (e.key === 'Escape') { if (open) { e.preventDefault(); e.stopPropagation(); toggle(false) } return }
|
||||
if (e.key === 'Tab') { setOpen(false); return }
|
||||
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
||||
e.preventDefault()
|
||||
if (!open) { toggle(true); return }
|
||||
move(e.key)
|
||||
return
|
||||
}
|
||||
if (e.key === 'Enter' || e.key === ' ') {
|
||||
e.preventDefault()
|
||||
if (open) { if (active >= 0) toggleIndex(active) } else toggle(true)
|
||||
}
|
||||
}
|
||||
|
||||
function onSearchKeydown(e: React.KeyboardEvent) {
|
||||
if (['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(e.key)) { e.preventDefault(); e.stopPropagation(); move(e.key as any) }
|
||||
else if (e.key === 'Enter') { e.preventDefault(); e.stopPropagation(); if (active >= 0) toggleIndex(active) }
|
||||
else if (e.key === 'Escape') { e.stopPropagation(); toggle(false) }
|
||||
}
|
||||
const optId = (i: number) => `${uid}-o${i}`
|
||||
|
||||
return (
|
||||
<span className={cn('relative inline-flex w-full min-w-0 align-middle', open && 'z-[401]', className)}>
|
||||
<button ref={triggerRef} type="button" disabled={disabled} aria-label={ariaLabel || undefined}
|
||||
aria-haspopup="listbox" aria-expanded={open}
|
||||
aria-activedescendant={open && active >= 0 ? `${uid}-o${active}` : undefined}
|
||||
className={triggerCls({ open, size, disabled })}
|
||||
onClick={() => toggle()} onKeyDown={onKeydown}>
|
||||
{!!count && (
|
||||
<span className="flex-none min-w-[17px] h-[17px] px-[5px] rounded-pill grid place-items-center bg-accent-fill text-on-accent text-tiny font-bold leading-none">{count}</span>
|
||||
<Popup open={open} onOpenChange={(next) => (next ? openList() : closeList(false))}
|
||||
anchorRef={boxRef} label={ariaLabel || undefined} onKeyDown={lb.onKeyDown}
|
||||
anchor={
|
||||
<span ref={boxRef} className={cn('relative inline-flex w-full min-w-0 align-middle', open && 'z-[401]', className)}>
|
||||
<button ref={triggerRef} type="button" role="combobox" disabled={disabled}
|
||||
aria-label={ariaLabel || undefined}
|
||||
aria-haspopup="listbox" aria-expanded={open}
|
||||
aria-controls={open ? listId : undefined}
|
||||
aria-activedescendant={open && lb.active >= 0 ? optId(lb.active) : undefined}
|
||||
className={triggerCls({ size, open })}
|
||||
onClick={() => (open ? closeList() : openList())} onKeyDown={lb.onKeyDown}>
|
||||
{!!count && <span className={countBadgeLayer}>{count}</span>}
|
||||
<span className={cn('flex-1 min-w-0 truncate whitespace-nowrap', !shown && 'text-faint')}>{shown || placeholder}</span>
|
||||
<Caret open={open} />
|
||||
</button>
|
||||
</span>
|
||||
}>
|
||||
<div className={barLayer}>
|
||||
{searchable && (
|
||||
<input ref={searchRef} type="search" value={query} placeholder="过滤选项…"
|
||||
aria-label={`${ariaLabel || '选项'}:过滤`}
|
||||
className={panelInputLayer}
|
||||
onChange={(e) => setQuery(e.target.value)} onKeyDown={lb.onInputKeyDown} />
|
||||
)}
|
||||
<span className={cn('flex-1 min-w-0 truncate whitespace-nowrap', !shown && 'text-faint')}>{shown || placeholder}</span>
|
||||
<Caret open={open} />
|
||||
</button>
|
||||
{open && box && createPortal(
|
||||
<div ref={popRef} role="listbox" aria-multiselectable aria-label={ariaLabel || undefined}
|
||||
className={popCls}
|
||||
style={{ left: box.left, top: box.top, width: box.width, maxHeight: box.maxH, transform: box.drop ? undefined : 'translateY(-100%)' }}
|
||||
onMouseDown={(e) => e.stopPropagation()}
|
||||
onKeyDown={onKeydown}>
|
||||
<div className={filterBarCls}>
|
||||
{searchable && (
|
||||
<input ref={searchRef} type="search" value={query} placeholder="过滤选项…"
|
||||
aria-label={`${ariaLabel || '选项'}:过滤`}
|
||||
className="flex-1 min-w-0 h-auto py-[5px] px-2 text-caption rounded-[6px] border border-line-strong bg-field text-ink outline-none focus:border-accent-deep [&::-webkit-search-cancel-button]:appearance-none"
|
||||
onChange={(e) => setQuery(e.target.value)} onKeyDown={onSearchKeydown} />
|
||||
)}
|
||||
<span className="flex-none text-tiny text-faint">{count ? `已选 ${count} 项` : '未选择'}</span>
|
||||
<button type="button" className="flex-none p-[4px_7px] rounded-[6px] text-tiny border border-line-strong bg-hover text-ink-2 disabled:opacity-40 disabled:cursor-not-allowed hover:not-disabled:border-line-bright hover:not-disabled:text-ink"
|
||||
disabled={!selectableInView} onClick={() => setInView(true)}>全选</button>
|
||||
<button type="button" className="flex-none p-[4px_7px] rounded-[6px] text-tiny border border-line-strong bg-hover text-ink-2 disabled:opacity-40 disabled:cursor-not-allowed hover:not-disabled:border-line-bright hover:not-disabled:text-ink"
|
||||
disabled={!count} onClick={() => setInView(false)}>清空</button>
|
||||
</div>
|
||||
<div ref={listRef} className={listCls}>
|
||||
{view.map((i) => {
|
||||
const o = opts[i]
|
||||
const on = isSelected(o.value)
|
||||
return (
|
||||
<div key={`${o.value}-${i}`} id={`${uid}-o${i}`} role="option" aria-selected={on}
|
||||
data-act={i === active ? '1' : '0'}
|
||||
className={itemCls({ on, act: i === active, off: o.disabled, multi: true })}
|
||||
onMouseEnter={() => setActive(i)}
|
||||
onClick={() => toggleIndex(i)}>
|
||||
<span className={cn('flex-none w-[14px] h-[14px] mt-px rounded-[4px] grid place-items-center border text-on-accent',
|
||||
on ? 'bg-accent-fill border-accent-fill' : 'border-line-bright')}>
|
||||
{on && <svg viewBox="0 0 12 12" className="w-[11px] h-[11px]"><path d="M2.6 6.3 4.9 8.6 9.4 3.7" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" /></svg>}
|
||||
</span>
|
||||
<span className="min-w-0 break-anywhere">{o.label}</span>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
{!view.length && <div className={noneCls}>{opts.length ? '没有匹配的选项' : '无可选项'}</div>}
|
||||
</div>
|
||||
</div>, document.body)}
|
||||
</span>
|
||||
<span className="flex-none text-tiny text-faint">{count ? `已选 ${count} 项` : '未选择'}</span>
|
||||
<button type="button" className={chipButtonLayer}
|
||||
disabled={!selectableInView} onClick={() => setInView(true)}>全选</button>
|
||||
<button type="button" className={chipButtonLayer}
|
||||
disabled={!count} onClick={() => setInView(false)}>清空</button>
|
||||
</div>
|
||||
<div ref={lb.listRef} id={listId} role="listbox" aria-multiselectable aria-label={ariaLabel || undefined} className={listLayer}>
|
||||
{view.map((i) => {
|
||||
const o = opts[i]
|
||||
const on = isSelected(o.value)
|
||||
return (
|
||||
<div key={`${o.value}-${i}`} id={optId(i)} role="option" aria-selected={on}
|
||||
data-active={i === lb.active ? '1' : undefined}
|
||||
data-on={on ? '1' : undefined}
|
||||
data-off={o.disabled ? '1' : undefined}
|
||||
className={itemCls({ mode: 'multi' })}
|
||||
onMouseEnter={() => { if (!o.disabled) lb.setActive(i) }}
|
||||
onClick={() => toggleIndex(i)}>
|
||||
<span className={cn(tickBoxLayer, on ? 'bg-accent-fill border-accent-fill' : 'border-line-bright')} aria-hidden>
|
||||
{on && <svg viewBox="0 0 12 12" className="w-[11px] h-[11px]"><path d="M2.6 6.3 4.9 8.6 9.4 3.7" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" /></svg>}
|
||||
</span>
|
||||
<span className="min-w-0 break-anywhere">{o.label}</span>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
{!view.length && <div className={emptyLayer}>{opts.length ? '没有匹配的选项' : '无可选项'}</div>}
|
||||
</div>
|
||||
</Popup>
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
import { useEffect, useId, useMemo, useRef, useState } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import { cn } from '../../lib/cn'
|
||||
import { Popup, barLayer, emptyLayer, itemCls, listLayer, panelInputLayer, tickSlotLayer, triggerCls, useListbox } from '../primitives'
|
||||
import { sameValue, summaryOf, toOptions, type OptionLike, type SelectOption } from './options'
|
||||
import { contentWidth, usePopup } from './use-popup'
|
||||
import { filterBarCls, filterInputCls, itemCls, listCls, noneCls, popCls, tickCls, triggerCls } from './parts'
|
||||
import { Caret } from './Caret'
|
||||
|
||||
/**
|
||||
* 全站统一下拉控件(替代原生 <select> 的系统外观)。
|
||||
*
|
||||
* 可见部分是自绘触发器 + 挂到 body 的弹层:抽屉、表格、滚动容器都不会裁切它,
|
||||
* 深浅主题一致。键盘(↑↓ Home End Enter Esc 首字母定位)与 ARIA 齐备;
|
||||
* 选项超过阈值自动出现过滤框,否则长列表只能靠滚动条盲摸。
|
||||
* 弹层宽度按最长选项量出来,既不裁字也不会比触发器还窄。
|
||||
* 分工:浮层行为在 primitives/Popup(Radix Popover:portal / 跟随锚点 / 翻边 / 外点关闭 / Esc),
|
||||
* 键盘语义在 primitives/use-listbox,皮肤在 primitives/layers,这里只管「选中谁、怎么选」。
|
||||
* 页面侧 87 处调用的 props 一个都没动。
|
||||
*
|
||||
* 选项超过阈值自动出现过滤框,否则长列表只能靠滚动条盲摸;
|
||||
* 面板宽度取「至少和触发器一样宽」,不再固定 168px 压住右边的字段标题。
|
||||
*/
|
||||
export type SelectProps = {
|
||||
value: any
|
||||
|
|
@ -38,15 +38,12 @@ export function Select({
|
|||
}: SelectProps) {
|
||||
const opts = useMemo(() => toOptions(options), [options])
|
||||
const uid = useId()
|
||||
const { open, setOpen, triggerRef, popRef, box } = usePopup({
|
||||
minW: 168, maxW: 420, listMinH: 190, maxHCap: 300,
|
||||
contentW: useMemo(() => contentWidth(opts.map((o) => o.label), 44), [opts]),
|
||||
disabled
|
||||
})
|
||||
const listRef = useRef<HTMLDivElement | null>(null)
|
||||
const listId = `${uid}-list`
|
||||
const boxRef = useRef<HTMLSpanElement | null>(null)
|
||||
const triggerRef = useRef<HTMLButtonElement | null>(null)
|
||||
const searchRef = useRef<HTMLInputElement | null>(null)
|
||||
const [open, setOpen] = useState(false)
|
||||
const [query, setQuery] = useState('')
|
||||
const [active, setActive] = useState(-1)
|
||||
|
||||
const selIndex = useMemo(() => opts.findIndex((o) => sameValue(o.value, value)), [opts, value])
|
||||
const shown = selIndex >= 0 ? opts[selIndex].label : ''
|
||||
|
|
@ -57,134 +54,102 @@ export function Select({
|
|||
}, [opts, query])
|
||||
const searchable = searchThreshold > 0 && opts.length > searchThreshold
|
||||
|
||||
const scrollActive = () => {
|
||||
listRef.current?.querySelector<HTMLElement>('[data-act="1"]')?.scrollIntoView({ block: 'nearest' })
|
||||
}
|
||||
useEffect(() => { if (open) queueMicrotask(scrollActive) }, [open, active])
|
||||
const lb = useListbox({
|
||||
view,
|
||||
isDisabled: (i) => !!opts[i]?.disabled,
|
||||
labelOf: (i) => opts[i]?.label ?? '',
|
||||
open,
|
||||
onPick: chooseIndex,
|
||||
onOpen: openList,
|
||||
onClose: closeList,
|
||||
initialActive: () => (selIndex >= 0 ? selIndex : opts.findIndex((o) => !o.disabled))
|
||||
})
|
||||
|
||||
// 只盯 query:opts/view 每次渲染都是新引用,列进依赖会把高亮反复重置回第一行
|
||||
useEffect(() => {
|
||||
if (query) setActive(view.find((i) => !opts[i].disabled) ?? -1)
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
if (query) lb.setActive(view.find((i) => !opts[i]?.disabled) ?? -1)
|
||||
}, [query])
|
||||
|
||||
function toggle(force?: boolean) {
|
||||
// 面板出现后把焦点交给过滤框(没有过滤框就留在触发器上,键盘不断)
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
;(searchable ? searchRef : triggerRef).current?.focus({ preventScroll: true })
|
||||
}, [open, searchable])
|
||||
|
||||
function openList() {
|
||||
if (disabled) return
|
||||
const next = force ?? !open
|
||||
if (!next) { setQuery(''); setOpen(false); triggerRef.current?.focus({ preventScroll: true }); return }
|
||||
setQuery('')
|
||||
setActive(selIndex >= 0 ? selIndex : opts.findIndex((o) => !o.disabled))
|
||||
setOpen(true)
|
||||
queueMicrotask(() => { if (searchable) searchRef.current?.focus({ preventScroll: true }); else triggerRef.current?.focus({ preventScroll: true }) })
|
||||
lb.prime()
|
||||
}
|
||||
|
||||
function choose(i: number) {
|
||||
/** refocus=false:外点到页面上的别的控件时,别把焦点抢回来,否则点不动下一个字段 */
|
||||
function closeList(refocus = true) {
|
||||
setQuery('')
|
||||
setOpen(false)
|
||||
if (refocus) triggerRef.current?.focus({ preventScroll: true })
|
||||
}
|
||||
|
||||
function chooseIndex(i: number) {
|
||||
const o = opts[i]
|
||||
if (!o || o.disabled) return
|
||||
onChange(o.value)
|
||||
setOpen(false)
|
||||
setQuery('')
|
||||
setOpen(false)
|
||||
triggerRef.current?.focus({ preventScroll: true })
|
||||
}
|
||||
|
||||
function move(delta: number) {
|
||||
if (!opts.length) return
|
||||
if (!open) { toggle(true); return }
|
||||
if (!view.length) return
|
||||
let pos = Math.max(0, view.indexOf(active))
|
||||
for (let n = 0; n < view.length; n++) {
|
||||
pos = (pos + delta + view.length) % view.length
|
||||
if (!opts[view[pos]].disabled) break
|
||||
}
|
||||
setActive(view[pos])
|
||||
}
|
||||
|
||||
let typeBuf = ''
|
||||
let typeTimer = 0
|
||||
function typeAhead(ch: string) {
|
||||
typeBuf += ch.toLowerCase()
|
||||
window.clearTimeout(typeTimer)
|
||||
typeTimer = window.setTimeout(() => { typeBuf = '' }, 420)
|
||||
const hit = view.find((i) => opts[i].label.toLowerCase().startsWith(typeBuf))
|
||||
if (hit !== undefined) setActive(hit)
|
||||
}
|
||||
|
||||
function onKeydown(e: React.KeyboardEvent) {
|
||||
if ((e.target as HTMLElement | null)?.tagName === 'INPUT') return
|
||||
if (e.key === 'Escape') { if (open) { e.preventDefault(); e.stopPropagation(); setOpen(false) } return }
|
||||
if (e.key === 'Tab') { setOpen(false); return }
|
||||
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { e.preventDefault(); move(e.key === 'ArrowDown' ? 1 : -1); return }
|
||||
if (e.key === 'Home' || e.key === 'End') {
|
||||
if (!open) return
|
||||
e.preventDefault()
|
||||
const order = e.key === 'Home' ? view : [...view].reverse()
|
||||
setActive(order.find((i) => !opts[i].disabled) ?? view[0] ?? -1)
|
||||
return
|
||||
}
|
||||
if (e.key === 'Enter' || e.key === ' ') {
|
||||
e.preventDefault()
|
||||
if (open) choose(active)
|
||||
else toggle(true)
|
||||
return
|
||||
}
|
||||
if (e.key.length === 1 && !e.metaKey && !e.ctrlKey && !e.altKey) {
|
||||
if (!open) { toggle(true); return }
|
||||
typeAhead(e.key)
|
||||
}
|
||||
}
|
||||
|
||||
function onSearchKeydown(e: React.KeyboardEvent) {
|
||||
// 过滤框自己消化这些键,否则冒泡到弹层会二次执行(方向键走两格、回车来回切换)
|
||||
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { e.preventDefault(); e.stopPropagation(); move(e.key === 'ArrowDown' ? 1 : -1) }
|
||||
else if (e.key === 'Home' || e.key === 'End') { e.stopPropagation() }
|
||||
else if (e.key === 'Enter') {
|
||||
if (active >= 0) { e.preventDefault(); e.stopPropagation(); choose(active) }
|
||||
} else if (e.key === 'Escape') { e.stopPropagation(); toggle(false) }
|
||||
}
|
||||
const optId = (i: number) => `${uid}-o${i}`
|
||||
|
||||
return (
|
||||
<span className={cn('relative inline-flex w-full min-w-0 align-middle', open && 'z-[401]', className)}>
|
||||
<button ref={triggerRef} type="button" id={id} disabled={disabled} aria-label={ariaLabel || undefined}
|
||||
title={title} aria-haspopup="listbox" aria-expanded={open} data-name={name}
|
||||
aria-activedescendant={open && active >= 0 ? `${uid}-o${active}` : undefined}
|
||||
className={triggerCls({ open, size, disabled })}
|
||||
onClick={() => toggle()} onKeyDown={onKeydown}>
|
||||
<span className={cn('flex-1 min-w-0 truncate whitespace-nowrap', !shown && 'text-faint')}>{shown || placeholder}</span>
|
||||
<Caret open={open} />
|
||||
</button>
|
||||
{open && box && createPortal(
|
||||
<div ref={popRef} role="listbox" aria-label={ariaLabel || undefined}
|
||||
className={popCls}
|
||||
style={{ left: box.left, top: box.top, width: box.width, maxHeight: box.maxH, transform: box.drop ? undefined : 'translateY(-100%)' }}
|
||||
onMouseDown={(e) => e.stopPropagation()}
|
||||
onKeyDown={onKeydown}>
|
||||
{searchable && (
|
||||
<div className={filterBarCls}>
|
||||
<input ref={searchRef} type="search" value={query} className={filterInputCls} placeholder="过滤选项…"
|
||||
aria-label={`${ariaLabel || '选项'}:过滤`}
|
||||
onChange={(e) => setQuery(e.target.value)} onKeyDown={onSearchKeydown} />
|
||||
<span className="flex-none text-tiny text-faint whitespace-nowrap">
|
||||
{view.length === opts.length ? `${opts.length} 项` : `${view.length} / ${opts.length} 项`}
|
||||
</span>
|
||||
<Popup open={open} onOpenChange={(next) => (next ? openList() : closeList(false))}
|
||||
anchorRef={boxRef} label={ariaLabel || undefined} onKeyDown={lb.onKeyDown}
|
||||
anchor={
|
||||
<span ref={boxRef} className={cn('relative inline-flex w-full min-w-0 align-middle', open && 'z-[401]', className)}>
|
||||
<button ref={triggerRef} type="button" id={id} role="combobox" disabled={disabled}
|
||||
aria-label={ariaLabel || undefined} title={title}
|
||||
aria-haspopup="listbox" aria-expanded={open}
|
||||
aria-controls={open ? listId : undefined}
|
||||
aria-activedescendant={open && lb.active >= 0 ? optId(lb.active) : undefined}
|
||||
data-name={name}
|
||||
className={triggerCls({ size, open })}
|
||||
onClick={() => (open ? closeList() : openList())} onKeyDown={lb.onKeyDown}>
|
||||
<span className={cn('flex-1 min-w-0 truncate whitespace-nowrap', !shown && 'text-faint')}>{shown || placeholder}</span>
|
||||
<Caret open={open} />
|
||||
</button>
|
||||
</span>
|
||||
}>
|
||||
{searchable && (
|
||||
<div className={barLayer}>
|
||||
<input ref={searchRef} type="search" value={query} className={panelInputLayer} placeholder="过滤选项…"
|
||||
aria-label={`${ariaLabel || '选项'}:过滤`}
|
||||
onChange={(e) => setQuery(e.target.value)} onKeyDown={lb.onInputKeyDown} />
|
||||
<span className="flex-none text-tiny text-faint whitespace-nowrap">
|
||||
{view.length === opts.length ? `${opts.length} 项` : `${view.length} / ${opts.length} 项`}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
<div ref={lb.listRef} id={listId} role="listbox" aria-label={ariaLabel || undefined} className={listLayer}>
|
||||
{view.map((i) => {
|
||||
const o: SelectOption = opts[i]
|
||||
const on = sameValue(o.value, value)
|
||||
return (
|
||||
<div key={`${o.value}-${i}`} id={optId(i)} role="option" aria-selected={on}
|
||||
data-active={i === lb.active ? '1' : undefined}
|
||||
data-on={on ? '1' : undefined}
|
||||
data-off={o.disabled ? '1' : undefined}
|
||||
className={itemCls({ mode: 'single' })}
|
||||
onMouseEnter={() => { if (!o.disabled) lb.setActive(i) }}
|
||||
onClick={() => chooseIndex(i)}>
|
||||
<span className={tickSlotLayer} style={{ opacity: on ? 1 : 0 }} aria-hidden>✓</span>
|
||||
<span className="min-w-0 break-anywhere">{o.label}</span>
|
||||
</div>
|
||||
)}
|
||||
<div ref={listRef} className={listCls}>
|
||||
{view.map((i) => {
|
||||
const o: SelectOption = opts[i]
|
||||
return (
|
||||
<div key={`${o.value}-${i}`} id={`${uid}-o${i}`} role="option" aria-selected={sameValue(o.value, value)}
|
||||
data-act={i === active ? '1' : '0'}
|
||||
className={itemCls({ on: sameValue(o.value, value), act: i === active, off: o.disabled })}
|
||||
onMouseEnter={() => setActive(i)}
|
||||
onClick={() => choose(i)}>
|
||||
<span className={tickCls} style={{ opacity: sameValue(o.value, value) ? 1 : 0 }} aria-hidden>✓</span>
|
||||
<span className="min-w-0 break-anywhere">{o.label}</span>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
{!opts.length && <div className={noneCls}>无可选项</div>}
|
||||
{!!opts.length && !view.length && <div className={noneCls}>没有匹配的选项</div>}
|
||||
</div>
|
||||
</div>, document.body)}
|
||||
</span>
|
||||
)
|
||||
})}
|
||||
{!opts.length && <div className={emptyLayer}>无可选项</div>}
|
||||
{!!opts.length && !view.length && <div className={emptyLayer}>没有匹配的选项</div>}
|
||||
</div>
|
||||
</Popup>
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,46 +0,0 @@
|
|||
import { cn } from '../../lib/cn'
|
||||
|
||||
/**
|
||||
* Select / MultiSelect 共用的皮肤。
|
||||
*
|
||||
* 两个控件的形状几乎一样(触发器 + body 弹层 + 选项行),旧站因此复制了
|
||||
* 一整份 scoped 样式,改一处必须改两处;这里把类名收成常量。
|
||||
* 高度仍然只来自 --ctl-h(容器决定主尺寸还是紧凑尺寸),组件内部不写死 px。
|
||||
*/
|
||||
export const triggerCls = (o: { open: boolean; size: 'sm' | 'md'; disabled?: boolean; className?: string }) => cn(
|
||||
'appearance-none flex items-center gap-2 w-full min-w-0 h-(--ctl-h) px-[10px] rounded-control text-left',
|
||||
'border border-line-strong bg-field text-ink text-body leading-[1.35] outline-none',
|
||||
'transition-[border-color,background,box-shadow] duration-150 disabled:cursor-not-allowed',
|
||||
'hover:not-disabled:border-line-bright hover:not-disabled:bg-hover',
|
||||
o.size === 'sm' && 'px-2 text-tiny rounded-[6px]',
|
||||
o.disabled && 'opacity-50',
|
||||
o.open && 'border-accent-deep shadow-[0_0_0_3px_var(--c-accent-tint)]',
|
||||
o.className
|
||||
)
|
||||
|
||||
export const popCls = 'fixed z-[400] flex flex-col overflow-hidden border border-line-strong rounded-[10px] bg-panel shadow-pop animate-fade-in'
|
||||
|
||||
export const listCls = 'flex-1 min-h-0 overflow-auto scrollbar-slim p-[5px]'
|
||||
|
||||
export const itemCls = (o: { on?: boolean; act?: boolean; off?: boolean; multi?: boolean }) => cn(
|
||||
'flex items-start gap-[7px] p-[7px_8px] rounded-[7px] text-body leading-[1.45] text-ink-2 cursor-pointer',
|
||||
o.act && 'bg-hover text-ink',
|
||||
o.on && (o.multi ? 'text-ink' : 'text-accent-bright bg-accent-tint'),
|
||||
o.on && o.act && !o.multi && 'bg-accent-tint-2',
|
||||
o.off && 'opacity-40 cursor-not-allowed'
|
||||
)
|
||||
|
||||
export const filterBarCls = 'flex-none flex items-center gap-[7px] p-[7px_8px] border-b border-line bg-raise'
|
||||
|
||||
export const filterInputCls = 'flex-1 min-w-0 h-auto py-[5px] px-2 text-caption rounded-[6px] border border-line-strong bg-field text-ink focus:border-accent-deep'
|
||||
|
||||
export const noneCls = 'p-[10px] text-center text-caption text-faint'
|
||||
|
||||
/** 下拉箭头:展开时翻转并点亮,用户永远知道面板是开还是关 */
|
||||
export function caretCls(open: boolean) {
|
||||
return cn('flex-none w-[15px] h-[15px] text-muted transition-transform duration-150', open && 'rotate-180 text-accent-bright')
|
||||
}
|
||||
|
||||
export const CARET_PATH = 'M5.5 7.5 10 12l4.5-4.5'
|
||||
|
||||
export const tickCls = 'flex-none w-3 shrink-0 mt-px text-[11px] text-accent-bright'
|
||||
|
|
@ -1,109 +0,0 @@
|
|||
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'
|
||||
|
||||
/**
|
||||
* 弹层定位:触发器 + 挂到 body 的下拉面板(Select / MultiSelect 共用)。
|
||||
*
|
||||
* 为什么要算这么多:抽屉、滚动容器、表格 overflow:hidden 都会把就地渲染的弹层裁掉;
|
||||
* 贴底时不翻边就露出一两条选项。位置只在打开时算一次 + 跟随 scroll/resize 重算,
|
||||
* 真实高度渲染完再按 rect 纠正(必要时翻边/贴边)。
|
||||
*/
|
||||
export type PopupBox = { left: number; top: number; width: number; drop: boolean; maxH: number }
|
||||
|
||||
let measureCtx: CanvasRenderingContext2D | null = null
|
||||
/** 弹层宽度:既不窄于触发器,也要放得下最长的选项 */
|
||||
export function contentWidth(texts: string[], pad: number): number {
|
||||
if (typeof document === 'undefined') return 0
|
||||
if (!measureCtx) measureCtx = document.createElement('canvas').getContext('2d')
|
||||
if (!measureCtx) return 0
|
||||
measureCtx.font = '12px "DM Sans", "Noto Sans SC", system-ui, sans-serif'
|
||||
let max = 0
|
||||
for (const t of texts) max = Math.max(max, measureCtx.measureText(t).width)
|
||||
return Math.ceil(max) + pad
|
||||
}
|
||||
|
||||
export function usePopup(opts: {
|
||||
minW: number
|
||||
maxW: number
|
||||
contentW: number
|
||||
/** 低于这个剩余空间就往上翻边 */
|
||||
listMinH: number
|
||||
maxHCap: number
|
||||
disabled?: boolean
|
||||
}) {
|
||||
const { minW, maxW, contentW: cw, listMinH, maxHCap, disabled } = opts
|
||||
const triggerRef = useRef<HTMLButtonElement | null>(null)
|
||||
const popRef = useRef<HTMLDivElement | null>(null)
|
||||
const [open, setOpen] = useState(false)
|
||||
const [box, setBox] = useState<PopupBox | null>(null)
|
||||
|
||||
const place = useCallback(() => {
|
||||
const el = triggerRef.current
|
||||
if (!el) return
|
||||
const r = el.getBoundingClientRect()
|
||||
const avail = Math.max(160, window.innerWidth - 24)
|
||||
const width = Math.min(Math.max(Math.max(r.width, cw), Math.min(minW, avail)), maxW, avail)
|
||||
const below = window.innerHeight - r.bottom - 14
|
||||
const above = r.top - 14
|
||||
const drop = below >= listMinH || below >= above
|
||||
setBox({
|
||||
left: Math.min(Math.max(8, r.left), Math.max(8, window.innerWidth - width - 8)),
|
||||
top: drop ? r.bottom + 6 : r.top - 6,
|
||||
width,
|
||||
drop,
|
||||
maxH: Math.max(listMinH, Math.min(maxHCap, drop ? below : above))
|
||||
})
|
||||
}, [cw, minW, maxW, listMinH, maxHCap])
|
||||
|
||||
/** 渲染完成后按真实高度纠正:只有这里知道面板到底有多高 */
|
||||
const clamp = useCallback(() => {
|
||||
const el = popRef.current
|
||||
const trig = triggerRef.current?.getBoundingClientRect()
|
||||
if (!el || !trig || !box) return
|
||||
const rect = el.getBoundingClientRect()
|
||||
const h = rect.height
|
||||
let top = box.top
|
||||
if (rect.bottom > window.innerHeight - 8 || rect.top < 8) {
|
||||
let drop = box.drop
|
||||
if (drop && window.innerHeight - (trig.bottom + 6) < h && trig.top - 14 > h) { top = trig.top - 6; drop = false }
|
||||
else if (!drop && rect.top < 8) { top = trig.bottom + 6; drop = true }
|
||||
if (top + h > window.innerHeight - 8) top = window.innerHeight - 8 - h
|
||||
if (top < 8) top = 8
|
||||
setBox({ ...box, top, drop })
|
||||
}
|
||||
}, [box])
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (!open) return
|
||||
place()
|
||||
}, [open, place])
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (!open) return
|
||||
clamp()
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [open, box?.maxH, box?.width, box?.left])
|
||||
|
||||
useEffect(() => {
|
||||
if (disabled && open) setOpen(false)
|
||||
}, [disabled, open])
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
const onPointerDown = (e: PointerEvent) => {
|
||||
const t = e.target as Node
|
||||
if (triggerRef.current?.contains(t) || popRef.current?.contains(t)) return
|
||||
setOpen(false)
|
||||
}
|
||||
const reposition = () => place()
|
||||
document.addEventListener('pointerdown', onPointerDown, true)
|
||||
window.addEventListener('resize', reposition)
|
||||
window.addEventListener('scroll', reposition, true)
|
||||
return () => {
|
||||
document.removeEventListener('pointerdown', onPointerDown, true)
|
||||
window.removeEventListener('resize', reposition)
|
||||
window.removeEventListener('scroll', reposition, true)
|
||||
}
|
||||
}, [open, place])
|
||||
|
||||
return { open, setOpen, triggerRef, popRef, box }
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
import * as RadixPopover from '@radix-ui/react-popover'
|
||||
import type { KeyboardEvent as ReactKeyboardEvent, ReactNode, RefObject } from 'react'
|
||||
import { cn } from '../../lib/cn'
|
||||
import { contentLayer } from './layers'
|
||||
|
||||
/**
|
||||
* 下拉面板的宿主:Radix Popover 负责 portal、跟随锚点、视口避让(放不下就翻边)、
|
||||
* 外点关闭、Esc、焦点管理;皮肤在 layers.ts,行为参数由调用点决定。
|
||||
*
|
||||
* 用 Anchor 而不是 Trigger:开合与键盘继续由 Select / MultiSelect 自己管
|
||||
* (两者共用 useListbox),Trigger 会替我们吞掉 Enter/Space/方向键。
|
||||
*
|
||||
* 代价是 Radix 的「点在触发器上不要当成外点」只认 Trigger 注册的 ref,
|
||||
* 用 Anchor 时 anchorRef 得我们自己传进来(见下面的 onInteractOutside)。
|
||||
*
|
||||
* 取代的是旧 forms/use-popup.ts:那份要自己量 left/top/width/maxH、
|
||||
* 渲染完再按 rect 二次纠正、手写 pointerdown/scroll/resize 监听,
|
||||
* 全站十几个 overflow 容器 + 抽屉照样能把面板裁掉。
|
||||
*/
|
||||
export function Popup({ open, onOpenChange, anchor, anchorRef, children, className, label, onKeyDown }: {
|
||||
open: boolean
|
||||
onOpenChange: (next: boolean) => void
|
||||
/** 触发器本身(必须能接 ref),面板贴着它摆 */
|
||||
anchor: ReactNode
|
||||
/** 指向 anchor 那个节点:外点/焦点离开时用来判断「其实点的是自己」 */
|
||||
anchorRef?: RefObject<HTMLElement | null>
|
||||
children: ReactNode
|
||||
className?: string
|
||||
/** 读屏用:面板是干什么的(Radix 给 Content 的 role 是 dialog) */
|
||||
label?: string
|
||||
onKeyDown?: (e: ReactKeyboardEvent) => void
|
||||
}) {
|
||||
// Radix 把 outside 事件派发在真正被点到的那个元素上,原始事件挂在 detail 里
|
||||
const hitAnchor = (target: EventTarget | null) => {
|
||||
const host = anchorRef?.current
|
||||
return !!host && !!target && host.contains(target as Node)
|
||||
}
|
||||
return (
|
||||
<RadixPopover.Root open={open} onOpenChange={onOpenChange} modal={false}>
|
||||
<RadixPopover.Anchor asChild>{anchor}</RadixPopover.Anchor>
|
||||
<RadixPopover.Portal>
|
||||
<RadixPopover.Content
|
||||
aria-label={label}
|
||||
// 左对齐锚点、下沿 6px;离视口边留 8px,放不下由 Radix 翻到上方
|
||||
side="bottom" align="start" sideOffset={6} collisionPadding={8}
|
||||
// 焦点留在触发器上,键盘与首字母定位才不会一开就断;关闭时由调用点把焦点还回去
|
||||
onOpenAutoFocus={(e) => e.preventDefault()}
|
||||
onCloseAutoFocus={(e) => e.preventDefault()}
|
||||
onInteractOutside={(e) => { if (hitAnchor(e.detail?.originalEvent?.target ?? null)) e.preventDefault() }}
|
||||
onFocusOutside={(e) => { if (hitAnchor(e.detail?.originalEvent?.target ?? null)) e.preventDefault() }}
|
||||
// 面板挂在 body 上,但 React 的事件仍沿组件树冒泡:不拦住的话,
|
||||
// 点一下选项会连带触发外层那一行 / 那张卡片的 click、mousedown
|
||||
onMouseDown={(e) => e.stopPropagation()}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
className={cn(contentLayer, className)}
|
||||
onKeyDown={onKeyDown}>
|
||||
{children}
|
||||
</RadixPopover.Content>
|
||||
</RadixPopover.Portal>
|
||||
</RadixPopover.Root>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
/**
|
||||
* primitives:Radix 行为 + 本站令牌皮肤。
|
||||
*
|
||||
* 分工(docs/UI规范.md §0.5):Radix 管「浮层怎么活」(定位/翻边/焦点/键盘/关闭),
|
||||
* layers.ts 管「长什么样」(尺寸、字号、圆角、配色全部来自 tokens.css),
|
||||
* ui/ 与 forms/ 管「业务语义」(字段、面板、状态标签…)。
|
||||
* 想换行为库,只动这一层。
|
||||
*/
|
||||
export * from './layers'
|
||||
export * from './use-listbox'
|
||||
export * from './Popup'
|
||||
|
|
@ -0,0 +1,123 @@
|
|||
import { cva, type VariantProps } from 'class-variance-authority'
|
||||
import { cn } from '../../lib/cn'
|
||||
|
||||
/**
|
||||
* primitives 的皮肤层:Radix 只负责行为(portal / 定位 / 翻边 / 视口避让 / 外点关闭 /
|
||||
* Esc / 焦点),DOM 和类名仍然归我们,`styles/tokens.css` 依旧是唯一的尺子来源。
|
||||
*
|
||||
* 这里是 Select / MultiSelect(以及后续 Tooltip、Menu)共用的一套档位,
|
||||
* 取代原先散在 forms/parts.ts 里的手写条件类名。
|
||||
*
|
||||
* 两条改过的规矩(都是量出来的问题):
|
||||
* 1. 触发器字号两档统一 text-body。旧实现 sm 档写死 `text-tiny`(10px) + `rounded-[6px]`,
|
||||
* 同一条筛选条里旁边的 input 是 12px / 7px 圆角,一眼就能看出两种控件。
|
||||
* 2. 弹层宽度「不小于触发器」而不是固定 168px。旧实现 82px 的触发器顶出 168px 的面板,
|
||||
* 多出来的 86px 直接压住右边的字段标题。
|
||||
*/
|
||||
|
||||
/** 触发器:高度只来自 --ctl-h(容器决定档位),组件内部不写死 px */
|
||||
export const triggerLayer = cva([
|
||||
'appearance-none flex items-center gap-2 w-full min-w-0 text-left outline-none',
|
||||
'h-(--ctl-h) rounded-control border border-line-strong bg-field text-ink text-body leading-[1.35]',
|
||||
'transition-[border-color,background,box-shadow] duration-150',
|
||||
'hover:not-disabled:border-line-bright hover:not-disabled:bg-hover',
|
||||
'disabled:cursor-not-allowed disabled:opacity-50',
|
||||
'focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent'
|
||||
].join(' '), {
|
||||
variants: {
|
||||
size: { sm: 'px-2', md: 'px-[10px]' },
|
||||
open: { true: 'border-accent-deep shadow-[0_0_0_3px_var(--c-accent-tint)]' }
|
||||
},
|
||||
defaultVariants: { size: 'md', open: false }
|
||||
})
|
||||
|
||||
/**
|
||||
* 弹层容器。Radix 在 Content 上给出两个实测变量:
|
||||
* --radix-popover-trigger-width(锚点宽度)、--radix-popover-content-available-{width,height}(避让后的可用空间),
|
||||
* 直接消费它们,就不需要旧 use-popup.ts 里手算 left/top/maxH 与二次纠正。
|
||||
*/
|
||||
export const contentLayer = [
|
||||
'fixed z-[400] flex flex-col overflow-hidden',
|
||||
'rounded-[10px] border border-line-strong bg-panel shadow-pop',
|
||||
// 宽度三段式:至少和触发器一样宽(面板不会缩成一条)、按最长选项自然展开
|
||||
// (长标签不该被挤成两行)、超过 440 或视口放不下才换行
|
||||
'min-w-(--radix-popover-trigger-width) w-max',
|
||||
'max-w-[min(440px,calc(var(--radix-popover-content-available-width)-16px))]',
|
||||
'max-h-[min(320px,var(--radix-popover-content-available-height))]',
|
||||
'data-[state=open]:animate-fade-in'
|
||||
].join(' ')
|
||||
|
||||
/** 选项滚动区 */
|
||||
export const listLayer = 'flex-1 min-h-0 overflow-auto scrollbar-slim p-[5px]'
|
||||
|
||||
/**
|
||||
* 选项行:状态写在 DOM 属性上(data-active / data-on / data-off),类名只描述属性。
|
||||
*
|
||||
* 「选中」和「键盘高亮」同时命中时谁赢不能靠 CSS 出场顺序赌:
|
||||
* 叠加态用两个属性选择器(data-[on=1]:data-[active=1]:…),特指度天然更高。
|
||||
*/
|
||||
export const itemLayer = cva([
|
||||
'flex items-start gap-[7px] p-[7px_8px] rounded-[7px] cursor-pointer',
|
||||
'text-body leading-[1.45] text-ink-2',
|
||||
'data-[active=1]:bg-hover data-[active=1]:text-ink',
|
||||
'data-[off=1]:cursor-not-allowed data-[off=1]:opacity-40'
|
||||
].join(' '), {
|
||||
variants: {
|
||||
mode: {
|
||||
/** 单选:选中项点亮主色,且点亮色压过键盘高亮 */
|
||||
single: 'data-[on=1]:bg-accent-tint data-[on=1]:text-accent-bright '
|
||||
+ 'data-[on=1]:data-[active=1]:bg-accent-tint-2 data-[on=1]:data-[active=1]:text-accent-bright',
|
||||
/** 多选:勾选框自己说话,文字不换色,只保留键盘高亮底色 */
|
||||
multi: 'data-[on=1]:text-ink data-[on=1]:data-[active=1]:text-ink'
|
||||
}
|
||||
},
|
||||
defaultVariants: { mode: 'single' }
|
||||
})
|
||||
|
||||
/** 面板顶部那条(过滤框 + 计数 + 批量按钮) */
|
||||
export const barLayer = 'flex-none flex items-center gap-[7px] p-[7px_8px] border-b border-line bg-raise'
|
||||
|
||||
/** 面板里的小输入框:它不是字段,不吃 --ctl-h,只吃同一套圆角和描边 */
|
||||
export const panelInputLayer = [
|
||||
'flex-1 min-w-0 h-auto py-[5px] px-2 text-caption rounded-[6px]',
|
||||
'border border-line-strong bg-field text-ink outline-none focus:border-accent-deep',
|
||||
'[&::-webkit-search-cancel-button]:appearance-none'
|
||||
].join(' ')
|
||||
|
||||
export const emptyLayer = 'p-[10px] text-center text-caption text-faint'
|
||||
|
||||
/** 面板里的批量动作小按钮(全选 / 清空) */
|
||||
export const chipButtonLayer = [
|
||||
'flex-none p-[4px_7px] rounded-[6px] text-tiny border border-line-strong bg-hover text-ink-2',
|
||||
'disabled:opacity-40 disabled:cursor-not-allowed',
|
||||
'hover:not-disabled:border-line-bright hover:not-disabled:text-ink'
|
||||
].join(' ')
|
||||
|
||||
/** 多选触发器上的计数角标 */
|
||||
export const countBadgeLayer = [
|
||||
'flex-none min-w-[17px] h-[17px] px-[5px] rounded-pill grid place-items-center',
|
||||
'bg-accent-fill text-on-accent text-tiny font-bold leading-none'
|
||||
].join(' ')
|
||||
|
||||
/** 多选行的勾选框(画在行里,不是表单控件) */
|
||||
export const tickBoxLayer = 'flex-none w-[14px] h-[14px] mt-px rounded-[4px] grid place-items-center border text-on-accent'
|
||||
/** 单选行的对勾槽位:没选中也占位,否则文字会左右跳 */
|
||||
export const tickSlotLayer = 'flex-none w-3 shrink-0 mt-px text-[11px] text-accent-bright'
|
||||
|
||||
/** 下拉箭头:展开时翻转并点亮,用户永远知道面板是开还是关 */
|
||||
export const caretLayer = 'flex-none w-[15px] h-[15px] text-muted transition-transform duration-150'
|
||||
export const CARET_PATH = 'M5.5 7.5 10 12l4.5-4.5'
|
||||
|
||||
/** 保持函数式写法,调用点少改一层 */
|
||||
export function triggerCls(o: VariantProps<typeof triggerLayer> & { className?: string } = {}) {
|
||||
const { className, ...variants } = o
|
||||
return cn(triggerLayer(variants), className)
|
||||
}
|
||||
/** 选项行的类名:只有形态(单选/多选)参与拼类名,开关状态是 DOM 属性不是类 */
|
||||
export function itemCls(o: VariantProps<typeof itemLayer> & { className?: string } = {}) {
|
||||
const { className, ...variants } = o
|
||||
return cn(itemLayer(variants), className)
|
||||
}
|
||||
export function caretCls(open: boolean) {
|
||||
return cn(caretLayer, open && 'rotate-180 text-accent-bright')
|
||||
}
|
||||
|
|
@ -0,0 +1,135 @@
|
|||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import type { KeyboardEvent as ReactKeyboardEvent } from 'react'
|
||||
|
||||
/**
|
||||
* 列表键盘导航:Select(单选)与 MultiSelect(多选)共用的一份实现。
|
||||
*
|
||||
* 原来两个控件各自写了 60 行几乎一样的 ↑↓ Home End Enter Esc + 首字母定位,
|
||||
* 改一处忘一处(例如多选漏掉首字母定位,单选漏掉 Home/End)。
|
||||
* 定位/关闭交给 Radix,这里只管「高亮哪一项、按键是什么意思」。
|
||||
*/
|
||||
export type ListboxSpec = {
|
||||
/** 过滤后可见的选项在完整列表里的下标(保持接口给的顺序,不做模糊重排) */
|
||||
view: number[]
|
||||
isDisabled: (i: number) => boolean
|
||||
labelOf: (i: number) => string
|
||||
open: boolean
|
||||
onPick: (i: number) => void
|
||||
onOpen: () => void
|
||||
onClose: () => void
|
||||
/** 展开时高亮谁:单选回显当前值,多选回显第一个已选 */
|
||||
initialActive: () => number
|
||||
}
|
||||
|
||||
export function useListbox(spec: ListboxSpec) {
|
||||
// spec 每次渲染都是新对象:放进 ref,回调保持恒定身份,省掉一串 useCallback 依赖
|
||||
const ref = useRef(spec)
|
||||
ref.current = spec
|
||||
const listRef = useRef<HTMLDivElement | null>(null)
|
||||
const type = useRef({ text: '', timer: 0 })
|
||||
const [active, setActive] = useState(-1)
|
||||
// 高亮项也要读最新值:下面这些回调故意保持恒定身份,否则会锁死在旧闭包里
|
||||
const activeRef = useRef(active)
|
||||
activeRef.current = active
|
||||
|
||||
const firstEnabled = useCallback(() => {
|
||||
const { view, isDisabled } = ref.current
|
||||
return view.find((i) => !isDisabled(i)) ?? -1
|
||||
}, [])
|
||||
|
||||
/** 展开时把高亮挪到初值(没有可选就是 -1,让 Enter 什么也不做) */
|
||||
const prime = useCallback(() => {
|
||||
const s = ref.current
|
||||
const want = s.initialActive()
|
||||
setActive(s.view.includes(want) ? want : firstEnabled())
|
||||
}, [firstEnabled])
|
||||
|
||||
/** 高亮项滚进可视区:键盘走三屏之后看不见光标,和没做键盘一个体验 */
|
||||
const scrollActive = useCallback(() => {
|
||||
listRef.current?.querySelector<HTMLElement>('[data-active="1"]')?.scrollIntoView({ block: 'nearest' })
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (spec.open) queueMicrotask(scrollActive)
|
||||
}, [spec.open, active, scrollActive])
|
||||
|
||||
useEffect(() => () => window.clearTimeout(type.current.timer), [])
|
||||
|
||||
const step = useCallback((delta: number) => {
|
||||
const { view, isDisabled } = ref.current
|
||||
if (!view.length) return
|
||||
let pos = Math.max(0, view.indexOf(activeRef.current))
|
||||
for (let n = 0; n < view.length; n++) {
|
||||
pos = (pos + delta + view.length) % view.length
|
||||
if (!isDisabled(view[pos])) break
|
||||
}
|
||||
setActive(view[pos])
|
||||
}, [])
|
||||
|
||||
const jump = useCallback((edge: 'first' | 'last') => {
|
||||
const { view, isDisabled } = ref.current
|
||||
const order = edge === 'first' ? view : [...view].reverse()
|
||||
setActive(order.find((i) => !isDisabled(i)) ?? -1)
|
||||
}, [])
|
||||
|
||||
/** 首字母定位:连续敲字在 420ms 内累加,能直接敲「qwen」跳到那一项 */
|
||||
const typeAhead = useCallback((ch: string) => {
|
||||
const { view, labelOf, isDisabled } = ref.current
|
||||
const buf = type.current
|
||||
buf.text += ch.toLowerCase()
|
||||
window.clearTimeout(buf.timer)
|
||||
buf.timer = window.setTimeout(() => { buf.text = '' }, 420)
|
||||
const hit = view.find((i) => !isDisabled(i) && labelOf(i).toLowerCase().startsWith(buf.text))
|
||||
if (hit !== undefined) setActive(hit)
|
||||
}, [])
|
||||
|
||||
/** 触发器与面板共用:面板里的按键会冒泡到这里,统一消化,避免二次执行 */
|
||||
const onKeyDown = useCallback((e: ReactKeyboardEvent) => {
|
||||
if ((e.target as HTMLElement | null)?.tagName === 'INPUT') return
|
||||
const s = ref.current
|
||||
if (e.key === 'Escape') {
|
||||
if (s.open) { e.preventDefault(); e.stopPropagation(); s.onClose() }
|
||||
return
|
||||
}
|
||||
if (e.key === 'Tab') { if (s.open) s.onClose(); return }
|
||||
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
||||
e.preventDefault()
|
||||
if (!s.open) s.onOpen()
|
||||
else step(e.key === 'ArrowDown' ? 1 : -1)
|
||||
return
|
||||
}
|
||||
if (e.key === 'Home' || e.key === 'End') {
|
||||
if (!s.open) return
|
||||
e.preventDefault()
|
||||
jump(e.key === 'Home' ? 'first' : 'last')
|
||||
return
|
||||
}
|
||||
if (e.key === 'Enter' || e.key === ' ') {
|
||||
e.preventDefault()
|
||||
if (!s.open) s.onOpen()
|
||||
else if (activeRef.current >= 0) s.onPick(activeRef.current)
|
||||
return
|
||||
}
|
||||
if (e.key.length === 1 && !e.metaKey && !e.ctrlKey && !e.altKey) {
|
||||
if (!s.open) s.onOpen()
|
||||
else typeAhead(e.key)
|
||||
}
|
||||
}, [step, jump, typeAhead])
|
||||
|
||||
/** 过滤框自己吃掉的键:不拦的话方向键一次走两格、回车来回切换 */
|
||||
const onInputKeyDown = useCallback((e: ReactKeyboardEvent) => {
|
||||
const s = ref.current
|
||||
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
||||
e.preventDefault(); e.stopPropagation(); step(e.key === 'ArrowDown' ? 1 : -1)
|
||||
} else if (e.key === 'Home' || e.key === 'End') {
|
||||
e.preventDefault(); e.stopPropagation(); jump(e.key === 'Home' ? 'first' : 'last')
|
||||
} else if (e.key === 'Enter') {
|
||||
if (activeRef.current >= 0) { e.preventDefault(); e.stopPropagation(); s.onPick(activeRef.current) }
|
||||
else e.stopPropagation()
|
||||
} else if (e.key === 'Escape') {
|
||||
e.stopPropagation(); s.onClose()
|
||||
}
|
||||
}, [step, jump])
|
||||
|
||||
return { listRef, active, setActive, firstEnabled, prime, scrollActive, step, jump, onKeyDown, onInputKeyDown }
|
||||
}
|
||||
Loading…
Reference in New Issue