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): add comboBox component #454

Merged
merged 18 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
36 changes: 36 additions & 0 deletions src/components/Combobox/Combobox.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const CLASS_PREFIX = 'md-combobox';

const DEFAULTS = {
HEIGHT:'2rem',
WIDTH:'100%',
ICON_SCALE:12 as const,
PLACEHOLDER:'Search or select an option',
NO_RESULT_TEXT:'No results found',
SELECTEDKEY:'',
DISABLEDKEYS:[],
ERROR:false,
};

const STYLE = {
description: `${CLASS_PREFIX}-description`,
label: `${CLASS_PREFIX}-label`,
wrapper: `${CLASS_PREFIX}-wrapper`,
inputSection: `${CLASS_PREFIX}-inputSection`,
input: `${CLASS_PREFIX}-input`,
divider: `${CLASS_PREFIX}-divider`,
btnContainer: `${CLASS_PREFIX}-btnContainer`,
button: `${CLASS_PREFIX}-button`,
arrowIcon: `${CLASS_PREFIX}-arrowIcon`,
selectionContainer: `${CLASS_PREFIX}-selectionContainer`,
selection: `${CLASS_PREFIX}-selection`,
noResultText: `${CLASS_PREFIX}-noResultText`,
};

const KEYS = {
INPUT_SEARCH_NO_RESULT: 'inputSearch_noResult',
};

export{STYLE,DEFAULTS,KEYS};



128 changes: 128 additions & 0 deletions src/components/Combobox/Combobox.documentation.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { Story } from '@storybook/addon-docs';

`<Combobox />`
The component is composed of three parts: Button, Menu and Input. In the component, the items
passed in are filtered based on the value of the Input and then passed to the internal Menu
to implement the search function.

## Content

The Combobox element follows the
[Collection Components API](https://react-spectrum.adobe.com/react-stately/collections.html) from
the @react-stately library, accepting both static and dynamic collections. Combobox accepts `<Item>`
elements as children, each with a key prop. Basic usage of `Combobox`, seen in the example below,
shows multiple options populated with a string. Simple element with simple options as text.

```js
import { Item, Section } from '@react-stately/collections';

<Combobox label="Single Value" onComboboxionChange={(key) => console.log}>
<Item>Red</Item>
<Item>Blue</Item>
<Item>Green</Item>
<Item>Yellow</Item>
</Combobox>;
```


## Dynamic Content

An iterable list of sectionGroups is can be passed to the Combobox using the items prop.
It should be noted that the outermost array is used to distinguish the group of sections,
and the items within the section accept an iterable list of options.

```js
import { Item } from '@react-stately/collections';

const items = [
{
section: '',
items: [
{ key: 'key1', label: 'item1' },
{ key: 'key2', label: 'item2' },
{ key: 'key3', label: 'item3' },
{ key: 'key4', label: 'item4' },
]
},
]

<Combobox onSelectionChange={(item) => console.log} items={items}>
{(sectionGroup) => {
return (
<Section key="noSection">
{sectionGroup.items.map((menuItem) => {
return (
<Item key={menuItem.key} textValue={menuItem.key}>
<div>{menuItem.label}</div>
</Item>
);
})}
</Section>
);
}}
</Combobox>
```

## Sections

Combobox supports sections in order to group options. Sections can be used by wrapping groups of items
in a `Section` element. Each `Section` takes a title and key prop.

```js
import { Item, Section } from '@react-stately/collections';

<Combobox onComboboxionChange={(key) => console.log}>
<Section title="Colors">
<Item>Red</Item>
<Item>Blue</Item>
<Item>Green</Item>
<Item>Yellow</Item>
</Section>
<Section title="Animals">
<Item>Dog</Item>
<Item>Cat</Item>
</Section>
</Combobox>;



const items = [
{
section: 'section1',
items: [
{ key: 'key1', label: 'item1' },
{ key: 'key2', label: 'item2' },
{ key: 'key3', label: 'item3' },
{ key: 'key4', label: 'item4' },
]
},
{
section: 'section2',
items: [
{ key: 'key1', label: 'item1' },
{ key: 'key2', label: 'item2' },
{ key: 'key3', label: 'item3' },
{ key: 'key4', label: 'item4' },
]
},
]

<Combobox onSelectionChange={(item) => console.log} items={items}>
{(group: IGroup) => {
return (
<Section title={group.section} key={group.section}>
{group.items.map((menuItem: IItem) => {
return (
<Item key={menuItem.key} textValue={menuItem.key}>
<div>{menuItem.label}</div>
</Item>
);
})}
</Section>
);
}}
</Combobox>
```


## Default Preview
205 changes: 205 additions & 0 deletions src/components/Combobox/Combobox.stories.args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import { commonStyles } from '../../storybook/helper.stories.argtypes';
import { Combobox_CONSTANTS as CONSTANTS } from '.';
import { ICON_CONSTANTS } from '../Icon';
export default {
...commonStyles,
width: {
defaultValue: '15rem',
description:
'To override the Combobox container and selection list width.',
control: { type: 'text' },
table: {
type: {
summary: 'string',
},
defaultValue: {
summary: CONSTANTS.DEFAULTS.WIDTH,
},
},
},
height: {
defaultValue: CONSTANTS.DEFAULTS.HEIGHT,
description:
'To override the Combobox container height, It also affects the width of the arrowButton, which is used to ensure that the button is square',
control: { type: 'text' },
table: {
type: {
summary: 'string',
},
defaultValue: {
summary: CONSTANTS.DEFAULTS.HEIGHT,
},
},
},
error: {
defaultValue: false,
description: 'Sets whether the Combobox is in error state',
control: { type: 'boolean' },
table: {
type: {
summary: 'boolean',
},
defaultValue: {
summary: false,
},
},
},
placeholder: {
defaultValue: CONSTANTS.DEFAULTS.PLACEHOLDER,
description: 'Text to display inside the input when there is no inputValue or item selected',
control: { type: 'text' },
table: {
category: 'React Aria - Input',
type: {
summary: 'string',
},
defaultValue: {
summary: 'undefined',
},
},
},
onInputChange: {
defaultValue: undefined,
description:
'Handler that is called when the InputValue changes.',
control: { type: 'function' },
table: {
category: 'React Aria - Input',
type: {
summary: '(event: InputEvent) => void',
},
defaultValue: {
summary: 'undefined',
},
},
},
onPress: {
defaultValue: undefined,
description:
'Handler that is called when the press is released over the arrowButton.',
control: { type: 'function' },
table: {
category: 'React Aria - Button',
type: {
summary: '(event: PressEvent) => void',
},
defaultValue: {
summary: 'undefined',
},
},
},
iconScale: {
defaultValue: CONSTANTS.DEFAULTS.ICON_SCALE,
description: 'Size of the arrow icon.',
control: { type: 'select' },
options: [undefined, ...Object.values(ICON_CONSTANTS.SIZES)],
table: {
category: 'React Aria - Button',
type: {
summary: 'IconScale',
},
defaultValue: {
summary: CONSTANTS.DEFAULTS.ICON_SCALE,
},
},
},
selectedKey: {
defaultValue: CONSTANTS.DEFAULTS.SELECTEDKEY,
description:
'It also affects the value of the input (displayed as the label of the corresponding item',
control: { type: 'text' },
table: {
category: 'React Aria - Select',
type: {
summary: 'React.Key',
},
defaultValue: {
summary: CONSTANTS.DEFAULTS.SELECTEDKEY,
},
},
},
disabledKeys: {
defaultValue: [],
description:
'List with disabled keys. (They must be exact type as the key)',
control: { type: 'array' },
table: {
category: 'React Aria - Select',
type: {
summary: 'React.Key[]',
},
defaultValue: {
summary: '[]',
},
},
},
items: {
defaultValue: [],
description:
'The options for this selection list element.',
control: { type: 'array' },
table: {
category: 'React Aria - Select',
type: {
summary: 'IComboboxGroup[]',
},
defaultValue: {
summary: '[]',
},
},
},
children: {
description: 'Provides the items nodes for this selection list element.',
table: {
category: 'React Aria - Select',
type: {
summary: 'CollectionChildren<any>',
},
defaultValue: {
summary: 'undefined',
},
},
},
noResultText: {
defaultValue:'No results found',
description: 'Text to display inside the dropdown when there is no results.',
control: { type: 'text' },
table: {
category: 'React Aria - Select',
type: {
summary: 'string',
},
defaultValue: {
summary: 'undefined',
},
},
},
onAction: {
action: 'onAction',
description: 'Handler that is called when an item is selected.',
table: {
category: 'React Aria - Select',
type: {
summary: '(item: IComboboxItem) => void',
},
defaultValue: {
summary: 'undefined',
},
},
},
onSelectionChange: {
defaultValue: undefined,
description:
'Handler that is called when the selection changes.',
control: { type: 'function' },
table: {
category: 'React Aria - Select',
type: {
summary: '(item: IComboboxItem) => void',
},
defaultValue: {
summary: 'undefined',
},
},
},
};
Loading
Loading