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

feat: ComboBox component #315

Merged
merged 18 commits into from
May 22, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: move type declarations to component file
  • Loading branch information
arthurgeron committed Apr 30, 2024
commit ed72a6182d71deeef69adc1a7e1057306b1f836f
3 changes: 1 addition & 2 deletions packages/ui/src/components/ComboBox/ComboBox.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Meta, StoryObj } from '@storybook/react';
import { useRef, useState } from 'react';
import { ComboBox } from './ComboBox';
import type { ComboBoxProps } from './types';
import { ComboBox, type ComboBoxProps } from './ComboBox';

const meta: Meta<typeof ComboBox> = {
title: 'Form/ComboBox',
50 changes: 40 additions & 10 deletions packages/ui/src/components/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
@@ -10,18 +10,48 @@ import React, {
createContext,
} from 'react';
import { createComponent, withNamespace } from '../../utils/component';
import { Flex } from '../Box';
import { Input } from '../Input';
import { type BoxProps, Flex, type FlexProps } from '../Box';
import { Input, type InputFieldProps, type InputProps } from '../Input';
import { Popover } from '../Popover';
import { Text } from '../Text';
import type {
ComboBoxContentProps,
ComboBoxInputFieldProps,
ComboBoxInputProps,
ComboBoxItemProps,
ComboBoxProps,
Context,
} from './types';

export type ComboBoxProps<T = string> = Pick<
InputFieldProps,
'onFocus' | 'onClick' | 'onBlur' | 'onKeyDown' | 'children'
> & {
suggestions: Array<T>;
debounce?: number;
value?: string | undefined;
onChange?: (value: string | undefined) => void;
suggestionFilter?: (suggestion: T) => boolean;
itemNameSelector?: (suggestion: T) => string;
onItemSelected: (suggestion: T) => void;
inputRef: React.RefObject<HTMLInputElement>;
/**
* @description If true, input can only be a value from the ComboBox list
*/
strict?: boolean;
};

export type ComboBoxInputProps = InputProps;
export type ComboBoxInputFieldProps = InputFieldProps & {
ref: React.Ref<HTMLInputElement>;
};
export type ComboBoxContentProps = FlexProps;
export type ComboBoxItemProps<T = string> = BoxProps &
Pick<ComboBoxProps<T>, 'itemNameSelector'> & {
suggestion: T | null;
onItemSelected: (suggestion: T) => void;
};

export type Context<T = string> = Pick<
ComboBoxProps<T>,
'itemNameSelector' | 'onFocus' | 'onClick' | 'onBlur' | 'onKeyDown'
> & {
filteredSuggestions: Array<T>;
handleSuggestionClick: (suggestion: T) => void;
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
};

const context = createContext<Context | undefined>(undefined);