mirror of
https://github.com/docmost/docmost.git
synced 2026-05-19 16:04:17 +08:00
feat(base): keyboard navigation for multi-select cell dropdown
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { useState, useRef, useEffect, useCallback, useMemo } from "react";
|
import { useState, useRef, useEffect, useCallback, useMemo } from "react";
|
||||||
import { Popover, TextInput } from "@mantine/core";
|
import { Popover, TextInput } from "@mantine/core";
|
||||||
|
import clsx from "clsx";
|
||||||
import {
|
import {
|
||||||
IBaseProperty,
|
IBaseProperty,
|
||||||
SelectTypeOptions,
|
SelectTypeOptions,
|
||||||
@@ -9,12 +10,17 @@ import { choiceColor } from "@/features/base/components/cells/choice-color";
|
|||||||
import { useUpdatePropertyMutation } from "@/features/base/queries/base-property-query";
|
import { useUpdatePropertyMutation } from "@/features/base/queries/base-property-query";
|
||||||
import { v7 as uuid7 } from "uuid";
|
import { v7 as uuid7 } from "uuid";
|
||||||
import cellClasses from "@/features/base/styles/cells.module.css";
|
import cellClasses from "@/features/base/styles/cells.module.css";
|
||||||
|
import { useListKeyboardNav } from "@/features/base/hooks/use-list-keyboard-nav";
|
||||||
|
|
||||||
const CHOICE_COLORS = [
|
const CHOICE_COLORS = [
|
||||||
"gray", "red", "pink", "grape", "violet", "indigo",
|
"gray", "red", "pink", "grape", "violet", "indigo",
|
||||||
"blue", "cyan", "teal", "green", "lime", "yellow", "orange",
|
"blue", "cyan", "teal", "green", "lime", "yellow", "orange",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
type NavItem =
|
||||||
|
| { kind: "choice"; choice: Choice }
|
||||||
|
| { kind: "add" };
|
||||||
|
|
||||||
type CellMultiSelectProps = {
|
type CellMultiSelectProps = {
|
||||||
value: unknown;
|
value: unknown;
|
||||||
property: IBaseProperty;
|
property: IBaseProperty;
|
||||||
@@ -78,6 +84,17 @@ export function CellMultiSelect({
|
|||||||
[choices.length],
|
[choices.length],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const navItems = useMemo<NavItem[]>(
|
||||||
|
() => [
|
||||||
|
...filteredChoices.map((c) => ({ kind: "choice" as const, choice: c })),
|
||||||
|
...(showAddOption ? [{ kind: "add" as const }] : []),
|
||||||
|
],
|
||||||
|
[filteredChoices, showAddOption],
|
||||||
|
);
|
||||||
|
|
||||||
|
const { activeIndex, setActiveIndex, handleNavKey, setOptionRef } =
|
||||||
|
useListKeyboardNav(navItems.length, [search, isEditing, showAddOption]);
|
||||||
|
|
||||||
const handleAddOption = useCallback(() => {
|
const handleAddOption = useCallback(() => {
|
||||||
if (!trimmedSearch) return;
|
if (!trimmedSearch) return;
|
||||||
const newChoice: Choice = {
|
const newChoice: Choice = {
|
||||||
@@ -104,18 +121,30 @@ export function CellMultiSelect({
|
|||||||
if (e.key === "Escape") {
|
if (e.key === "Escape") {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
onCancel();
|
onCancel();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (e.key === "Enter" && showAddOption) {
|
if (handleNavKey(e)) return;
|
||||||
e.preventDefault();
|
if (e.key === "Enter") {
|
||||||
handleAddOption();
|
if (activeIndex >= 0 && activeIndex < navItems.length) {
|
||||||
|
e.preventDefault();
|
||||||
|
const item = navItems[activeIndex];
|
||||||
|
if (item.kind === "choice") handleToggle(item.choice);
|
||||||
|
else handleAddOption();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (showAddOption) {
|
||||||
|
e.preventDefault();
|
||||||
|
handleAddOption();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[onCancel, showAddOption, handleAddOption],
|
[onCancel, handleNavKey, activeIndex, navItems, handleToggle, handleAddOption, showAddOption],
|
||||||
);
|
);
|
||||||
|
|
||||||
const MAX_VISIBLE = 3;
|
const MAX_VISIBLE = 3;
|
||||||
|
|
||||||
if (isEditing) {
|
if (isEditing) {
|
||||||
|
const addOptionIdx = filteredChoices.length;
|
||||||
return (
|
return (
|
||||||
<Popover
|
<Popover
|
||||||
opened
|
opened
|
||||||
@@ -140,27 +169,44 @@ export function CellMultiSelect({
|
|||||||
mb={4}
|
mb={4}
|
||||||
/>
|
/>
|
||||||
<div className={cellClasses.selectDropdown}>
|
<div className={cellClasses.selectDropdown}>
|
||||||
{filteredChoices.map((choice) => (
|
{filteredChoices.map((choice, idx) => {
|
||||||
<div
|
const isSelected = selectedSet.has(choice.id);
|
||||||
key={choice.id}
|
return (
|
||||||
className={`${cellClasses.selectOption} ${
|
<div
|
||||||
selectedSet.has(choice.id)
|
key={choice.id}
|
||||||
? cellClasses.selectOptionActive
|
ref={setOptionRef(idx)}
|
||||||
: ""
|
className={clsx(
|
||||||
}`}
|
cellClasses.selectOption,
|
||||||
onClick={() => handleToggle(choice)}
|
isSelected && cellClasses.selectOptionActive,
|
||||||
>
|
idx === activeIndex && cellClasses.selectOptionKeyboardActive,
|
||||||
<span
|
)}
|
||||||
className={cellClasses.badge}
|
onMouseEnter={() => setActiveIndex(idx)}
|
||||||
style={choiceColor(choice.color)}
|
onMouseDown={(e) => {
|
||||||
|
// Keep focus on the search input so click doesn't blur + close popover.
|
||||||
|
e.preventDefault();
|
||||||
|
}}
|
||||||
|
onClick={() => handleToggle(choice)}
|
||||||
>
|
>
|
||||||
{choice.name}
|
<span
|
||||||
</span>
|
className={cellClasses.badge}
|
||||||
</div>
|
style={choiceColor(choice.color)}
|
||||||
))}
|
>
|
||||||
|
{choice.name}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
{showAddOption && (
|
{showAddOption && (
|
||||||
<div
|
<div
|
||||||
className={cellClasses.addOptionRow}
|
ref={setOptionRef(addOptionIdx)}
|
||||||
|
className={clsx(
|
||||||
|
cellClasses.addOptionRow,
|
||||||
|
addOptionIdx === activeIndex && cellClasses.selectOptionKeyboardActive,
|
||||||
|
)}
|
||||||
|
onMouseEnter={() => setActiveIndex(addOptionIdx)}
|
||||||
|
onMouseDown={(e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
}}
|
||||||
onClick={handleAddOption}
|
onClick={handleAddOption}
|
||||||
>
|
>
|
||||||
<span className={cellClasses.addOptionLabel}>Add option:</span>
|
<span className={cellClasses.addOptionLabel}>Add option:</span>
|
||||||
|
|||||||
Reference in New Issue
Block a user