Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add wds multiselect component #39300

Merged
merged 21 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,4 @@
"@types/react": "^17.0.2",
"postcss": "8.4.31"
}
}
}
2 changes: 1 addition & 1 deletion app/client/packages/design-system/widgets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@tabler/icons-react": "^3.10.0",
"clsx": "^2.0.0",
"lodash": "*",
"react-aria-components": "^1.2.1",
"react-aria-components": "^1.6.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need 1.6.0 version as it has the AutoComplete component that we need in multi select dropdown for filtering list

"react-markdown": "^9.0.1",
"react-syntax-highlighter": "^15.5.0",
"react-transition-group": "^4.4.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function CalendarHeading(
[props, ref] = useContextProps(props, ref, HeadingContext);
const state = useContext(CalendarStateContext);

if (!state) return null;

return (
<div className={styles.monthYearDropdown}>
<CalendarMonthDropdown state={state} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ export const DatePicker = <T extends DateValue>(props: DatePickerProps<T>) => {
label="Time"
maxValue={timeMaxValue}
minValue={timeMinValue}
onChange={state.setTimeValue}
onChange={(value) => {
if (value) {
state.setTimeValue(value);
}
}}
placeholderValue={timePlaceholder}
value={state.timeValue}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./FieldError";
export type { FieldErrorProps } from "./types";
export { default as fieldErrorStyles } from "./styles.module.css";
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import styles from "./styles.module.css";
import { ListBox as HeadlessListBox } from "react-aria-components";

import type { ListBoxProps } from "./types";
import clsx from "clsx";

export function ListBox(props: ListBoxProps) {
const { children, ...rest } = props;
const { children, className, ...rest } = props;

return (
<HeadlessListBox {...rest} className={styles.listBox}>
<HeadlessListBox {...rest} className={clsx(styles.listBox, className)}>
{children}
</HeadlessListBox>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { ListBox } from "./ListBox";
export { default as listStyles } from "./styles.module.css";
export { default as listBoxStyles } from "./styles.module.css";
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import { ListBoxItem as HeadlessListBoxItem } from "react-aria-components";

import styles from "./styles.module.css";
import type { ListBoxItemProps } from "./types";
import clsx from "clsx";

export function ListBoxItem(props: ListBoxItemProps) {
const { children, icon, ...rest } = props;
const { children, className, icon, ...rest } = props;

return (
<HeadlessListBoxItem {...rest} className={styles.listBoxItem}>
<HeadlessListBoxItem
{...rest}
className={clsx(styles.listBoxItem, className)}
>
{icon && <Icon name={icon} />}
<Text lineClamp={1}>{children}</Text>
</HeadlessListBoxItem>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ListBoxItemProps as AriaListBoxItemProps } from "react-aria-components";
import type { IconProps } from "@appsmith/wds";
import type { ListBoxItemProps as AriaListBoxItemProps } from "react-aria-components";

export interface ListBoxItemProps extends AriaListBoxItemProps<object> {
icon?: IconProps["name"];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
import {
Popover,
listStyles,
listBoxStyles,
useRootContainer,
POPOVER_LIST_BOX_MAX_HEIGHT,
} from "@appsmith/wds";
import React, { createContext, useContext } from "react";
import React from "react";
import { Menu as HeadlessMenu } from "react-aria-components";

import type { MenuProps } from "./types";
import clsx from "clsx";

const MenuNestingContext = createContext(0);

export const Menu = (props: MenuProps) => {
const { children, className, ...rest } = props;
const root = useRootContainer();

const nestingLevel = useContext(MenuNestingContext);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KelvinOm seems all this is not required anymore.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thank you for fixing this!

const isRootMenu = nestingLevel === 0;

return (
<MenuNestingContext.Provider value={nestingLevel + 1}>
{/* Only the parent Popover should be placed in the root. Placing child popoves in root would cause the menu to function incorrectly */}
<Popover
UNSTABLE_portalContainer={isRootMenu ? root : undefined}
maxHeight={POPOVER_LIST_BOX_MAX_HEIGHT}
<Popover
UNSTABLE_portalContainer={root}
maxHeight={POPOVER_LIST_BOX_MAX_HEIGHT}
>
<HeadlessMenu
className={clsx(listBoxStyles.listBox, className)}
{...rest}
>
<HeadlessMenu className={clsx(listStyles.listBox, className)} {...rest}>
{children}
</HeadlessMenu>
</Popover>
</MenuNestingContext.Provider>
{children}
</HeadlessMenu>
</Popover>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./src";
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import clsx from "clsx";
import React, { useRef, useState } from "react";

import { useField } from "react-aria";
import { type Selection, ListBoxItem } from "react-aria-components";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was to modify WDS component, which is already have needed styles, instead of using spectrum component directly. This way we can reuse it more easily and styles will be used only in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea right. Had to update our ListBoxItem to accept renderProps too.

Updated with our ListBoxItem.

import { setInteractionModality } from "@react-aria/interactions";

import { Text } from "../../Text";
import styles from "./styles.module.css";
import { ListBox } from "../../ListBox";
import {
Popover,
POPOVER_LIST_BOX_MAX_HEIGHT,
useRootContainer,
} from "../../Popover";
import { selectStyles } from "../../Select";
import { TextField } from "../../TextField";
import { FieldLabel } from "../../FieldLabel";
import { textInputStyles } from "../../Input";
import { inputFieldStyles } from "../../Field";
import type { MultiSelectProps } from "./types";
import { fieldErrorStyles } from "../../FieldError";
import { listBoxItemStyles } from "../../ListBoxItem";

import {
DialogTrigger,
UNSTABLE_Autocomplete,
useFilter,
ButtonContext,
} from "react-aria-components";
import { MultiSelectValue } from "./MultiSelectValue";
import { Checkbox } from "../../Checkbox";

const EmptyState = () => {
return (
<Text className={styles.emptyState} color="neutral-subtle">
No options found
</Text>
);
};

/**
* Note: React aria components does not provide us any mutliselect componennt or hooks for it.
* We are just replicating the behaviour of mutli select component with all available hooks and components.
* Few things are implemented manually like opening the popover on keydown or keyup when the button is focused
* or focusing the trigger on click of label.
*
* This is a temporary solution until we have a mutli select component from react aria components library.
*/
export const MultiSelect = <T extends { label: string; value: string }>(
props: MultiSelectProps<T>,
) => {
const {
contextualHelp,
defaultSelectedKeys = new Set(),
disabledKeys,
errorMessage,
excludeFromTabOrder,
isDisabled,
isInvalid,
isLoading,
isRequired,
items,
label,
onSelectionChange: onSelectionChangeProp,
placeholder,
selectedKeys: selectedKeysProp,
size,
} = props;
const root = useRootContainer();
const [_selectedKeys, _setSelectedKeys] = useState<Selection>();
const selectedKeys = selectedKeysProp ?? _selectedKeys ?? defaultSelectedKeys;
const setSelectedKeys = onSelectionChangeProp ?? _setSelectedKeys;
const { labelProps } = useField(props);
const { contains } = useFilter({ sensitivity: "base" });
const triggerRef = useRef<HTMLButtonElement>(null);
// Note we have to use controlled state for the popover as we need a custom logic to open the popover
// for the usecase where we need to open the popover on keydown or keyup when the button is focused.
const [isOpen, setOpen] = useState(false);

const onKeyDown = (e: React.KeyboardEvent) => {
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
setOpen(true);
}
};

const filter = (textValue: string, inputValue: string) =>
contains(textValue, inputValue);

return (
<ButtonContext.Provider value={{ onKeyDown, ref: triggerRef }}>
<div className={inputFieldStyles.field}>
{Boolean(label) && (
<FieldLabel
{...labelProps}
contextualHelp={contextualHelp}
isDisabled={isDisabled}
isRequired={isRequired}
// this is required to imitate the behavior where on click of label, the trigger or input is focused.
// In our select component, this is done by the useSelect hook. Since we don't have that for multi select,
// we are doing this manually here
onClick={() => {
if (triggerRef.current) {
triggerRef.current.focus();

setInteractionModality("keyboard");
}
}}
>
{label}
</FieldLabel>
)}
<div
className={clsx(
textInputStyles.inputGroup,
selectStyles.selectInputGroup,
)}
>
<DialogTrigger isOpen={isOpen} onOpenChange={setOpen}>
<MultiSelectValue
excludeFromTabOrder={excludeFromTabOrder}
isDisabled={isDisabled}
isInvalid={isInvalid}
isLoading={isLoading}
items={items}
placeholder={placeholder}
selectedKeys={selectedKeys}
size={size}
triggerRef={triggerRef}
/>
<Popover
UNSTABLE_portalContainer={root}
className={styles.popover}
maxHeight={POPOVER_LIST_BOX_MAX_HEIGHT}
placement="bottom start"
style={
{
"--trigger-width": `${triggerRef?.current?.offsetWidth}px`,
} as React.CSSProperties
}
triggerRef={triggerRef}
>
<UNSTABLE_Autocomplete filter={filter}>
<TextField autoFocus className={styles.textField} />
<ListBox
className={styles.listBox}
disabledKeys={disabledKeys}
items={items}
onSelectionChange={setSelectedKeys}
renderEmptyState={EmptyState}
selectedKeys={selectedKeys}
selectionMode="multiple"
shouldFocusWrap
>
{(item: T) => (
<ListBoxItem
className={listBoxItemStyles.listBoxItem}
id={item.value}
textValue={item.label}
>
{({ isSelected }) => (
<>
<span className={styles.listBoxItemCheckbox}>
<Checkbox isSelected={isSelected} />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we pass the styles directly to the checkbox?

Suggested change
<Checkbox isSelected={isSelected} />
<Checkbox className={styles.listBoxItemCheckbox} isSelected={isSelected} />

Copy link
Contributor Author

@jsartisan jsartisan Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea. Had to update our checkbox API which was not accepting the className prop before. Fixed it.

</span>
{item.label}
</>
)}
</ListBoxItem>
)}
</ListBox>
</UNSTABLE_Autocomplete>
</Popover>
</DialogTrigger>
</div>
{/* We can't use our FieldError component as it only works when used with FieldErrorContext.
We can use it in our Select and other inputs because the implementation is abstracted in the react aria components library.
But since for MultiSelect, we don't have any component from react-aria, we have to manually render the error message here. */}
<div className={fieldErrorStyles.errorText}>
<Text color="negative" size="caption">
{errorMessage}
</Text>
</div>
</div>
</ButtonContext.Provider>
);
};
Loading
Loading