Fix Share access, initials, copy confirm, and mailed export links #112
No files matched your search
@@ -16,8 +16,8 @@ const SelectDropdown = forwardRef<HTMLDivElement, SelectDropdownProps>(
|
||||
rounded-[var(--radius-400)]
|
||||
shadow-lg
|
||||
p-[4px]
|
||||
w-full
|
||||
min-w-[200px]
|
||||
max-w-[300px]
|
||||
${className}
|
||||
`
|
||||
.trim()
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import React, {
|
||||
Children,
|
||||
type CSSProperties,
|
||||
type ReactElement,
|
||||
type ReactNode,
|
||||
forwardRef,
|
||||
@@ -12,11 +13,13 @@ import React, {
|
||||
memo,
|
||||
useImperativeHandle,
|
||||
useEffect,
|
||||
useLayoutEffect,
|
||||
useMemo,
|
||||
} from "react";
|
||||
import { useClickOutside } from "../../../hooks";
|
||||
import { useTranslation } from "../../../contexts/MessagesContext";
|
||||
import { SelectInputView } from "./SelectInput.view";
|
||||
import type { SelectInputProps } from "./SelectInput.types";
|
||||
import type { SelectInputProps, SelectOptionData } from "./SelectInput.types";
|
||||
|
||||
/**
|
||||
* Figma: "Control / SelectInput". Custom-styled select dropdown
|
||||
@@ -70,11 +73,31 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
|
||||
const generatedId = useId();
|
||||
const selectId = id || `select-input-${generatedId}`;
|
||||
const labelId = `${selectId}-label`;
|
||||
const listboxId = `${selectId}-listbox`;
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [selectedValue, setSelectedValue] = useState(value || "");
|
||||
const [highlightedIndex, setHighlightedIndex] = useState(0);
|
||||
const [menuStyle, setMenuStyle] = useState<CSSProperties | null>(null);
|
||||
const selectRef = useRef<HTMLButtonElement>(null);
|
||||
const menuRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const optionItems = useMemo((): SelectOptionData[] => {
|
||||
if (options && Array.isArray(options)) return options;
|
||||
return Children.toArray(children).flatMap((child) => {
|
||||
if (!React.isValidElement(child) || child.type !== "option") return [];
|
||||
const optionProps = child.props as {
|
||||
value: string;
|
||||
children: ReactNode;
|
||||
};
|
||||
return [
|
||||
{
|
||||
value: optionProps.value,
|
||||
label: String(optionProps.children),
|
||||
},
|
||||
];
|
||||
});
|
||||
}, [options, children]);
|
||||
|
||||
// Internal state management: track if focused and how (mouse vs keyboard)
|
||||
const [isFocused, setIsFocused] = useState(false);
|
||||
const [focusMethod, setFocusMethod] = useState<"mouse" | "keyboard" | null>(
|
||||
@@ -99,14 +122,69 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
|
||||
() => selectRef.current as HTMLButtonElement | null,
|
||||
);
|
||||
|
||||
const closeMenu = useCallback(() => {
|
||||
setIsOpen(false);
|
||||
setMenuStyle(null);
|
||||
}, []);
|
||||
|
||||
// Handle click outside to close menu
|
||||
useClickOutside([menuRef, selectRef], () => setIsOpen(false), isOpen);
|
||||
useClickOutside([menuRef, selectRef], closeMenu, isOpen);
|
||||
|
||||
const indexForValue = useCallback(
|
||||
(current: string) => {
|
||||
const index = optionItems.findIndex((option) => option.value === current);
|
||||
return index >= 0 ? index : 0;
|
||||
},
|
||||
[optionItems],
|
||||
);
|
||||
|
||||
const openMenu = useCallback(() => {
|
||||
const trigger = selectRef.current?.getBoundingClientRect();
|
||||
setMenuStyle({
|
||||
top: (trigger?.bottom ?? 0) + 4,
|
||||
left: trigger?.left ?? 0,
|
||||
width: Math.max(trigger?.width ?? 0, 200),
|
||||
});
|
||||
setHighlightedIndex(indexForValue(selectedValue));
|
||||
setIsOpen(true);
|
||||
}, [indexForValue, selectedValue]);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (!isOpen) {
|
||||
setMenuStyle(null);
|
||||
return;
|
||||
}
|
||||
const update = () => {
|
||||
const trigger = selectRef.current;
|
||||
if (!trigger) return;
|
||||
const rect = trigger.getBoundingClientRect();
|
||||
const menuHeight = menuRef.current?.offsetHeight ?? optionItems.length * 44 + 8;
|
||||
const gap = 4;
|
||||
const openUp =
|
||||
rect.bottom + gap + menuHeight > window.innerHeight &&
|
||||
rect.top > menuHeight + gap;
|
||||
setMenuStyle({
|
||||
top: openUp
|
||||
? Math.max(8, rect.top - menuHeight - gap)
|
||||
: rect.bottom + gap,
|
||||
left: rect.left,
|
||||
width: Math.max(rect.width, 200),
|
||||
});
|
||||
};
|
||||
update();
|
||||
window.addEventListener("resize", update);
|
||||
window.addEventListener("scroll", update, true);
|
||||
return () => {
|
||||
window.removeEventListener("resize", update);
|
||||
window.removeEventListener("scroll", update, true);
|
||||
};
|
||||
}, [isOpen, optionItems.length, highlightedIndex]);
|
||||
|
||||
// Handle option selection
|
||||
const handleOptionSelect = useCallback(
|
||||
(optionValue: string, optionText: string) => {
|
||||
setSelectedValue(optionValue);
|
||||
setIsOpen(false);
|
||||
closeMenu();
|
||||
if (onChange) {
|
||||
onChange({ target: { value: optionValue, text: optionText } });
|
||||
}
|
||||
@@ -114,7 +192,7 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
|
||||
selectRef.current.focus();
|
||||
}
|
||||
},
|
||||
[onChange],
|
||||
[closeMenu, onChange],
|
||||
);
|
||||
|
||||
// Handle mouse down to detect mouse clicks
|
||||
@@ -126,24 +204,74 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
|
||||
|
||||
// Handle select button click
|
||||
const handleSelectClick = useCallback(() => {
|
||||
if (!disabled) {
|
||||
setIsOpen(!isOpen);
|
||||
if (disabled) return;
|
||||
if (isOpen) {
|
||||
closeMenu();
|
||||
return;
|
||||
}
|
||||
}, [disabled, isOpen]);
|
||||
openMenu();
|
||||
}, [closeMenu, disabled, isOpen, openMenu]);
|
||||
|
||||
// Handle keyboard navigation
|
||||
const handleKeyDown = useCallback(
|
||||
(e: React.KeyboardEvent<HTMLButtonElement>) => {
|
||||
if (disabled) return;
|
||||
|
||||
if (e.key === "ArrowDown") {
|
||||
e.preventDefault();
|
||||
if (!isOpen) {
|
||||
openMenu();
|
||||
return;
|
||||
}
|
||||
setHighlightedIndex((index) =>
|
||||
Math.min(index + 1, Math.max(optionItems.length - 1, 0)),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (e.key === "ArrowUp") {
|
||||
e.preventDefault();
|
||||
if (!isOpen) {
|
||||
openMenu();
|
||||
return;
|
||||
}
|
||||
setHighlightedIndex((index) => Math.max(index - 1, 0));
|
||||
return;
|
||||
}
|
||||
if (e.key === "Home" && isOpen) {
|
||||
e.preventDefault();
|
||||
setHighlightedIndex(0);
|
||||
return;
|
||||
}
|
||||
if (e.key === "End" && isOpen) {
|
||||
e.preventDefault();
|
||||
setHighlightedIndex(Math.max(optionItems.length - 1, 0));
|
||||
return;
|
||||
}
|
||||
if (e.key === "Enter" || e.key === " ") {
|
||||
e.preventDefault();
|
||||
setIsOpen(!isOpen);
|
||||
} else if (e.key === "Escape") {
|
||||
setIsOpen(false);
|
||||
if (!isOpen) {
|
||||
openMenu();
|
||||
return;
|
||||
}
|
||||
const option = optionItems[highlightedIndex];
|
||||
if (option) {
|
||||
handleOptionSelect(option.value, option.label);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (e.key === "Escape") {
|
||||
closeMenu();
|
||||
}
|
||||
},
|
||||
[disabled, isOpen],
|
||||
[
|
||||
closeMenu,
|
||||
disabled,
|
||||
handleOptionSelect,
|
||||
highlightedIndex,
|
||||
isOpen,
|
||||
openMenu,
|
||||
optionItems,
|
||||
],
|
||||
);
|
||||
|
||||
// Handle focus to detect mouse vs keyboard
|
||||
@@ -237,6 +365,9 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
|
||||
onOptionClick={handleOptionSelect}
|
||||
selectRef={selectRef}
|
||||
menuRef={menuRef}
|
||||
listboxId={listboxId}
|
||||
highlightedIndex={highlightedIndex}
|
||||
menuStyle={menuStyle}
|
||||
ariaLabelledby={shouldShowLabel ? labelId : undefined}
|
||||
ariaInvalid={error}
|
||||
asterisk={asterisk}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import React, { Children, type ReactNode } from "react";
|
||||
import React, { Children, type CSSProperties, type ReactNode } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { getAssetPath, ASSETS } from "../../../../lib/assetUtils";
|
||||
import SelectDropdown from "./SelectDropdown";
|
||||
import SelectOption from "../SelectOption";
|
||||
@@ -30,6 +31,9 @@ export interface SelectInputViewProps {
|
||||
// Refs
|
||||
selectRef: React.RefObject<HTMLButtonElement>;
|
||||
menuRef: React.RefObject<HTMLDivElement>;
|
||||
listboxId: string;
|
||||
highlightedIndex: number;
|
||||
menuStyle: CSSProperties | null;
|
||||
// Additional props
|
||||
ariaLabelledby?: string;
|
||||
ariaInvalid?: boolean;
|
||||
@@ -66,6 +70,9 @@ export function SelectInputView({
|
||||
onOptionClick,
|
||||
selectRef,
|
||||
menuRef,
|
||||
listboxId,
|
||||
highlightedIndex,
|
||||
menuStyle,
|
||||
ariaLabelledby,
|
||||
ariaInvalid,
|
||||
asterisk = false,
|
||||
@@ -132,6 +139,29 @@ export function SelectInputView({
|
||||
|
||||
const buttonClasses = getButtonClasses();
|
||||
|
||||
const optionEntries: SelectOptionData[] =
|
||||
options && Array.isArray(options)
|
||||
? options
|
||||
: Children.toArray(children).flatMap((child) => {
|
||||
if (!React.isValidElement(child) || child.type !== "option") {
|
||||
return [];
|
||||
}
|
||||
const optionProps = child.props as {
|
||||
value: string;
|
||||
children: ReactNode;
|
||||
};
|
||||
return [
|
||||
{
|
||||
value: optionProps.value,
|
||||
label: String(optionProps.children),
|
||||
},
|
||||
];
|
||||
});
|
||||
|
||||
const highlightedValue = optionEntries[highlightedIndex]?.value;
|
||||
const activeOptionId =
|
||||
highlightedValue != null ? `${listboxId}-option-${highlightedIndex}` : undefined;
|
||||
|
||||
// Text color based on filled state
|
||||
const textColorClass = isFilled
|
||||
? "text-[var(--color-content-default-primary)]"
|
||||
@@ -183,6 +213,8 @@ export function SelectInputView({
|
||||
aria-invalid={ariaInvalid}
|
||||
aria-expanded={isOpen}
|
||||
aria-haspopup="listbox"
|
||||
aria-controls={isOpen ? listboxId : undefined}
|
||||
aria-activedescendant={isOpen ? activeOptionId : undefined}
|
||||
onClick={onButtonClick}
|
||||
onKeyDown={onButtonKeyDown}
|
||||
onMouseDown={onButtonMouseDown}
|
||||
@@ -219,53 +251,31 @@ export function SelectInputView({
|
||||
/>
|
||||
)}
|
||||
|
||||
{isOpen && (
|
||||
<div
|
||||
ref={menuRef}
|
||||
className="absolute top-full left-0 right-0 z-50 mt-1"
|
||||
>
|
||||
<SelectDropdown ariaLabel={selectAriaLabel}>
|
||||
{options && Array.isArray(options)
|
||||
? options.map((option) => (
|
||||
{isOpen && menuStyle && typeof document !== "undefined"
|
||||
? createPortal(
|
||||
<div
|
||||
ref={menuRef}
|
||||
className="fixed z-[10050]"
|
||||
style={menuStyle}
|
||||
>
|
||||
<SelectDropdown id={listboxId} ariaLabel={selectAriaLabel}>
|
||||
{optionEntries.map((option, index) => (
|
||||
<SelectOption
|
||||
key={option.value}
|
||||
id={`${listboxId}-option-${index}`}
|
||||
selected={option.value === selectedValue}
|
||||
highlighted={index === highlightedIndex}
|
||||
size="medium"
|
||||
onClick={() => onOptionClick(option.value, option.label)}
|
||||
>
|
||||
{option.label}
|
||||
</SelectOption>
|
||||
))
|
||||
: Children.map(children, (child) => {
|
||||
if (
|
||||
React.isValidElement(child) &&
|
||||
child.type === "option"
|
||||
) {
|
||||
const optionProps = child.props as {
|
||||
value: string;
|
||||
children: ReactNode;
|
||||
};
|
||||
return (
|
||||
<SelectOption
|
||||
key={optionProps.value}
|
||||
selected={optionProps.value === selectedValue}
|
||||
size="medium"
|
||||
onClick={() =>
|
||||
onOptionClick(
|
||||
optionProps.value,
|
||||
String(optionProps.children),
|
||||
)
|
||||
}
|
||||
>
|
||||
{optionProps.children}
|
||||
</SelectOption>
|
||||
);
|
||||
}
|
||||
return child;
|
||||
})}
|
||||
</SelectDropdown>
|
||||
</div>
|
||||
)}
|
||||
))}
|
||||
</SelectDropdown>
|
||||
</div>,
|
||||
document.body,
|
||||
)
|
||||
: null}
|
||||
</div>
|
||||
{textHint && (
|
||||
<div className="flex items-start relative shrink-0 w-full">
|
||||
|
||||
@@ -13,8 +13,10 @@ const SelectOptionContainer = forwardRef<HTMLDivElement, SelectOptionProps>(
|
||||
{
|
||||
children,
|
||||
selected = false,
|
||||
highlighted = false,
|
||||
disabled = false,
|
||||
className = "",
|
||||
id,
|
||||
onClick,
|
||||
size: sizeProp = "medium",
|
||||
...props
|
||||
@@ -35,15 +37,17 @@ const SelectOptionContainer = forwardRef<HTMLDivElement, SelectOptionProps>(
|
||||
}
|
||||
};
|
||||
|
||||
const active = selected || highlighted;
|
||||
const itemClasses = `
|
||||
flex items-center justify-between
|
||||
px-[8px] py-[4px]
|
||||
min-h-11 px-[8px] py-2
|
||||
md:min-h-0 md:py-[4px]
|
||||
text-[var(--color-content-default-brand-primary)]
|
||||
${getTextSize()}
|
||||
cursor-pointer
|
||||
transition-colors duration-150
|
||||
${
|
||||
selected
|
||||
active
|
||||
? "bg-[var(--color-surface-default-secondary)] rounded-[var(--radius-300)]"
|
||||
: ""
|
||||
}
|
||||
@@ -85,6 +89,7 @@ const SelectOptionContainer = forwardRef<HTMLDivElement, SelectOptionProps>(
|
||||
disabled={disabled}
|
||||
className={className}
|
||||
itemClasses={itemClasses}
|
||||
optionId={id}
|
||||
handleClick={handleClick}
|
||||
handleKeyDown={handleKeyDown}
|
||||
{...props}
|
||||
|
||||
@@ -3,8 +3,11 @@ export type SelectOptionSizeValue = "small" | "medium" | "large";
|
||||
export interface SelectOptionProps {
|
||||
children?: React.ReactNode;
|
||||
selected?: boolean;
|
||||
/** Keyboard highlight in a listbox; distinct from the selected value. */
|
||||
highlighted?: boolean;
|
||||
disabled?: boolean;
|
||||
className?: string;
|
||||
id?: string;
|
||||
onClick?: (
|
||||
_e: React.MouseEvent<HTMLDivElement> | React.KeyboardEvent<HTMLDivElement>,
|
||||
) => void;
|
||||
@@ -20,6 +23,7 @@ export interface SelectOptionViewProps {
|
||||
disabled: boolean;
|
||||
className: string;
|
||||
itemClasses: string;
|
||||
optionId?: string;
|
||||
handleClick: (_e: React.MouseEvent<HTMLDivElement>) => void;
|
||||
handleKeyDown: (_e: React.KeyboardEvent<HTMLDivElement>) => void;
|
||||
}
|
||||
@@ -11,6 +11,7 @@ export const SelectOptionView = forwardRef<
|
||||
selected,
|
||||
disabled,
|
||||
itemClasses,
|
||||
optionId,
|
||||
handleClick,
|
||||
handleKeyDown,
|
||||
...props
|
||||
@@ -20,9 +21,10 @@ export const SelectOptionView = forwardRef<
|
||||
return (
|
||||
<div
|
||||
ref={ref}
|
||||
id={optionId}
|
||||
className={itemClasses}
|
||||
role="option"
|
||||
tabIndex={disabled ? -1 : 0}
|
||||
tabIndex={-1}
|
||||
aria-selected={selected}
|
||||
aria-disabled={disabled}
|
||||
onClick={handleClick}
|
||||
|
||||
@@ -206,7 +206,7 @@ export const ShareView = memo(function ShareView({
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<div className="flex w-full flex-col items-start gap-2 overflow-hidden">
|
||||
<div className="flex w-full flex-col items-start gap-2 overflow-visible">
|
||||
<p className="w-full text-small-label text-[var(--color-content-default-secondary)]">
|
||||
{copy.whoCanRead}
|
||||
</p>
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import "@testing-library/jest-dom/vitest";
|
||||
import React from "react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import SelectInput from "../../app/components/controls/SelectInput";
|
||||
import { componentTestSuite } from "../utils/componentTestSuite";
|
||||
import { renderWithProviders, screen } from "../utils/test-utils";
|
||||
|
||||
type SelectInputProps = React.ComponentProps<typeof SelectInput>;
|
||||
|
||||
@@ -31,3 +35,33 @@ componentTestSuite<SelectInputProps>({
|
||||
errorProps: { error: true },
|
||||
},
|
||||
});
|
||||
|
||||
describe("SelectInput listbox keyboard", () => {
|
||||
it("moves between options with arrows and picks with Enter", async () => {
|
||||
const onChange = vi.fn();
|
||||
const user = userEvent.setup();
|
||||
renderWithProviders(
|
||||
<SelectInput
|
||||
labelText="Who can read"
|
||||
showLabel
|
||||
value="option1"
|
||||
onChange={onChange}
|
||||
options={[
|
||||
{ value: "option1", label: "Option 1" },
|
||||
{ value: "option2", label: "Option 2" },
|
||||
]}
|
||||
/>,
|
||||
);
|
||||
|
||||
const trigger = screen.getByRole("button", { name: /Who can read/i });
|
||||
trigger.focus();
|
||||
await user.keyboard("{ArrowDown}");
|
||||
expect(await screen.findByRole("option", { name: "Option 1" })).toBeVisible();
|
||||
expect(screen.getByRole("option", { name: "Option 2" })).toBeVisible();
|
||||
await user.keyboard("{ArrowDown}{Enter}");
|
||||
expect(onChange).toHaveBeenCalledWith({
|
||||
target: { value: "option2", text: "Option 2" },
|
||||
});
|
||||
expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -201,6 +201,28 @@ describe("Share modal", () => {
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("shows both access options and picks one with arrow keys", async () => {
|
||||
const user = userEvent.setup();
|
||||
openShare();
|
||||
const trigger = screen.getByRole("button", {
|
||||
name: /Only people you invite/i,
|
||||
});
|
||||
trigger.focus();
|
||||
await user.keyboard("{ArrowDown}");
|
||||
const inviteOnly = await screen.findByRole("option", {
|
||||
name: "Only people you invite",
|
||||
});
|
||||
const anyone = screen.getByRole("option", { name: "Anyone with the link" });
|
||||
expect(inviteOnly).toBeVisible();
|
||||
expect(anyone).toBeVisible();
|
||||
await user.keyboard("{ArrowDown}{Enter}");
|
||||
expect(
|
||||
screen.getByText(
|
||||
"Anyone with the link can read it without logging in and pass it on. Check with your community first.",
|
||||
),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("calls onClose when Done is clicked", async () => {
|
||||
const user = userEvent.setup();
|
||||
const onClose = vi.fn();
|
||||
|
||||
Reference in new issue
Block a user