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

Combobox component (and Empty state) #21

Merged
merged 7 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
65 changes: 65 additions & 0 deletions lib/components/Command/CommandInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { useEffect, useRef } from 'react';
import { Combobox } from '@headlessui/react';
import { cx } from '../common/utils';
import { MagnifyingGlassIcon } from '../../icons/MagnifyingGlassIcon';

interface ICommandInputProps {
placeholder?: string;
value?: string;
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
icon?: React.ReactNode;
className?: string;
}

export const CommandInput: React.FC<ICommandInputProps> = ({
placeholder = "Search...",
onChange,
icon = (
<MagnifyingGlassIcon
className="pointer-events-none absolute inset-y-0 left-2 h-full w-4 text-gray-400"
aria-hidden="true"
/>
),
className,
value,
}) => {
const inputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
if (inputRef.current) {
inputRef.current.focus();
}
}, []);

useEffect(() => {
const handleKeyUp = (event: KeyboardEvent) => {
if (event.key === "Escape") {
onChange({
target: { value: "" },
} as React.ChangeEvent<HTMLInputElement>);
}
};

window.addEventListener("keyup", handleKeyUp);

return () => {
window.removeEventListener("keyup", handleKeyUp);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
<div className={cx("relative flex items-center", className ?? "")}>
{icon}
<Combobox.Input
as="input"
ref={inputRef}
autoComplete="off"
value={value ?? ""}
onChange={onChange}
placeholder={placeholder ?? "Search..."}
className="block w-full rounded-md border-0 py-1.5 pl-8 pr-14 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-gray-300 sm:text-sm sm:leading-4"
/>
</div>
);
};
28 changes: 28 additions & 0 deletions lib/components/Command/CommandList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { HTMLAttributes } from 'react';
import { Combobox } from '@headlessui/react';
import { cx } from '../common/utils';

interface ICommandListProps extends Omit<HTMLAttributes<HTMLUListElement>, 'onCopy'> {
className?: string;
children?: React.ReactNode;
"data-testid"?: string;
}

export const CommandList: React.FC<ICommandListProps> = ({
children,
className,
"data-testid": dataTestId,
...props
}) => {
return (
<Combobox.Options
static
as="ul"
className={cx(className ?? "flex flex-col gap-1")}
data-testid={dataTestId}
{...props}
>
{children}
</Combobox.Options>
);
};
48 changes: 48 additions & 0 deletions lib/components/Command/CommandOption.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { HTMLAttributes } from 'react';
import { Combobox } from '@headlessui/react';
import { cx } from '../common/utils';

interface ICommandOptionProps extends HTMLAttributes<HTMLLIElement> {
value: string | (() => void);
disabled?: boolean;
activeClassName?: string;
children: React.ReactNode;
rightIcon?: React.ReactNode;
testId?: string;
}

export const CommandOption: React.FC<ICommandOptionProps> = ({
value,
activeClassName = "bg-gray-200",
children,
disabled,
rightIcon,
testId,
...props
}) => {
return (
<Combobox.Option
data-testid={testId}
value={value}
disabled={disabled}
{...props}
>
{({ active: isActive }) => (
<div
className={cx(
"flex w-full cursor-pointer items-center justify-between rounded-md px-2 py-1.5",
isActive
? `hover:${activeClassName} ${activeClassName}`
: "bg-transparent hover:bg-gray-100",
disabled ? "cursor-not-allowed text-gray-400" : "text-gray-900",
)}
>
<p className={"line-clamp-1 w-full text-left sm:leading-4"}>
{children}
</p>
{rightIcon}
</div>
)}
</Combobox.Option>
);
};
16 changes: 16 additions & 0 deletions lib/components/Command/Lifecycle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useEffect, useRef } from 'react';

export const Lifecycle = ({ onUnmount }: { onUnmount?: () => void }) => {
const onUnmountRef = useRef(onUnmount);
useEffect(() => {
onUnmountRef.current = onUnmount;
}, [onUnmount]);

useEffect(() => {
return () => {
onUnmountRef.current?.();
};
}, []);

return null;
};
32 changes: 32 additions & 0 deletions lib/components/Command/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { HTMLAttributes } from 'react';
import { Combobox } from '@headlessui/react';
import { cx } from '../common/utils';

interface ICommandProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange'> {
className?: string;
children?: React.ReactNode;
}

export const Command: React.FC<ICommandProps> = ({
className,
children,
...props
}) => {
return (
<Combobox
as="div"
className={cx(
"absolute z-popover mt-1 max-h-60 w-[200px] overflow-y-auto overflow-x-hidden rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm",
className ?? "",
)}
{...props}
>
<div className="flex flex-col gap-2 px-2 py-1">{children}</div>
</Combobox>
);
};

export { CommandOption } from "./CommandOption";
export { CommandList } from "./CommandList";
export { CommandInput } from "./CommandInput";
export { Lifecycle } from "./Lifecycle";
4 changes: 2 additions & 2 deletions lib/components/EmptyState/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventIcon } from '../../assets/EventIcon';
import { EventIcon } from '../../icons/EventIcon';

export interface EmptyStateProps {
IconComponent?: React.ElementType;
Expand All @@ -23,6 +23,6 @@ export const EmptyState: React.FC<EmptyStateProps> = ({
</p>
</div>
</div>
);
);
};

4 changes: 4 additions & 0 deletions lib/components/Popover/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Popover } from '@headlessui/react';

export { Popover };

File renamed without changes.
14 changes: 14 additions & 0 deletions lib/icons/MagnifyingGlassIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { cx } from '../components/common/utils';

interface MagnifyingGlassIconProps {
className?: string;
}

export const MagnifyingGlassIcon: React.FC<MagnifyingGlassIconProps> = ({ className = '' }) => {
return (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" className={cx("w-4 h-4", className)}>
<path fillRule="evenodd" d="M9 3.5a5.5 5.5 0 1 0 0 11 5.5 5.5 0 0 0 0-11ZM2 9a7 7 0 1 1 12.452 4.391l3.328 3.329a.75.75 0 1 1-1.06 1.06l-3.329-3.328A7 7 0 0 1 2 9Z" clipRule="evenodd" />
</svg>
);
}
3 changes: 2 additions & 1 deletion lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export { defaultGridProps } from './components/Grid/constants'
export { ChartTooltip, DefaultTooltip, ChartTooltipTitle, ChartTooltipValue, ChartTooltipFooter } from './components/ChartTooltip'
export { EmptyState } from './components/EmptyState'
export type { EmptyStateProps } from './components/EmptyState'

export { Lifecycle, Command, CommandOption, CommandList, CommandInput } from './components/Command'
export { Popover } from './components/Popover'
47 changes: 47 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"release-version": "npm version patch && git push && git push --tags"
},
"dependencies": {
"@headlessui/react": "^1.7.19",
"recharts": "^2.12.5"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion src/stories/BarChart.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import '../../lib/styles.css';
import {
BarChart,
BarItem,
Expand Down
Loading
Loading