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(react): add Autocomplete component #165

Merged
merged 11 commits into from
Oct 16, 2023
4 changes: 4 additions & 0 deletions packages/react/.storybook/story-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type Stories =
| 'AlertTitle'
Copy link
Member

Choose a reason for hiding this comment

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

This file will get removed. Fine to keep it for now.

| 'AppBar'
| 'AppShell'
| 'Autocomplete'
| 'Avatar'
| 'Backdrop'
| 'Badge'
Expand Down Expand Up @@ -161,6 +162,9 @@ const StoryConfig: StorybookConfig = {
AppShell: {
hierarchy: `${StorybookCategories.Layout}/App Shell`,
},
Autocomplete: {
hierarchy: `${StorybookCategories.Inputs}/Autocomplete`,
},
Avatar: {
hierarchy: `${StorybookCategories.DataDisplay}/Avatar`,
},
Expand Down
100 changes: 100 additions & 0 deletions packages/react/src/components/Autocomplete/Autocomplete.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {ArgsTable, Source, Story, Canvas, Meta} from '@storybook/addon-docs';
import dedent from 'ts-dedent';
import StoryConfig from '../../../.storybook/story-config.ts';
import Autocomplete from './Autocomplete.tsx';
import TextField from '../TextField';

export const meta = {
component: Autocomplete,
title: StoryConfig.Autocomplete.hierarchy,
};

<Meta title="Inputs/Autocomplete" component={Autocomplete} />

export const Template = args => <Autocomplete {...args} />;

# Autocomplete

- [Overview](#overview)
- [Props](#props)
- [Usage](#usage)

## Overview

Autocomplete is a conventional text input field that is improved with a panel displaying suggested choices.

### Combination box

The textbox's value needs to be chosen from a predefined set of allowed values.

<Canvas>
<Story
name="Combination box"
args={
{
options: [
{ label: 'The Shawshank Redemption', year: 1994 },
{ label: 'The Godfather', year: 1972 },
{ label: 'The Godfather: Part II', year: 1974 },
{ label: 'The Dark Knight', year: 2008 },
{ label: '12 Angry Men', year: 1957 },
{ label: "Schindler's List", year: 1993 },
{ label: 'Pulp Fiction', year: 1994 }
],
renderInput: params => <TextField {...params} label="Movie" />
}
}
>
{Template.bind({})}
</Story>
</Canvas>

#### Props

<ArgsTable story="Combination box" />

### Multiple values

This is also known as tags, this allows the user to enter more than one value.

<Canvas>
Achintha444 marked this conversation as resolved.
Show resolved Hide resolved
<Story
name="Multiple values"
args={
{
options: [
{ label: 'The Shawshank Redemption', year: 1994 },
{ label: 'The Godfather', year: 1972 },
{ label: 'The Godfather: Part II', year: 1974 },
{ label: 'The Dark Knight', year: 2008 },
{ label: '12 Angry Men', year: 1957 },
{ label: "Schindler's List", year: 1993 },
{ label: 'Pulp Fiction', year: 1994 }
],
renderInput: params => <TextField {...params} label="Movie" />,
multiple: true,
defaultValue: [
{ label: 'The Shawshank Redemption', year: 1994 },
{ label: 'The Godfather', year: 1972 }
]
}
}
>
{Template.bind({})}
</Story>
</Canvas>

#### Props

<ArgsTable story="Multiple values" />

## Usage

Import and use the `Autocomplete` component in your components as follows.

<Source
language="jsx"
dark
format
code={dedent`import Autocomplete from '@oxygen-ui/react/Autocomplete';\n`}
/>
45 changes: 45 additions & 0 deletions packages/react/src/components/Autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
Achintha444 marked this conversation as resolved.
Show resolved Hide resolved
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import MuiAutocomplete, {AutocompleteProps as MuiAutocompleteProps} from '@mui/material/Autocomplete';
import clsx from 'clsx';
import {forwardRef, ForwardRefExoticComponent, ReactElement, MutableRefObject} from 'react';
import {WithWrapperProps} from '../../models';
import {composeComponentDisplayName} from '../../utils';
import './autocomplete.scss';

export type AutocompleteProps<T> = MuiAutocompleteProps<T, boolean, boolean, boolean>;

const COMPONENT_NAME: string = 'Autocomplete';

const Autocomplete: ForwardRefExoticComponent<AutocompleteProps<Record<string, unknown>>> & WithWrapperProps =
forwardRef(
(props: AutocompleteProps<Record<string, unknown>>, ref: MutableRefObject<HTMLDivElement>): ReactElement => {
const {className, ...rest} = props;

const classes: string = clsx('oxygen-autocomplete', className);

return <MuiAutocomplete className={classes} {...rest} ref={ref} />;
},
) as ForwardRefExoticComponent<AutocompleteProps<Record<string, unknown>>> & WithWrapperProps;

Autocomplete.displayName = composeComponentDisplayName(COMPONENT_NAME);
Autocomplete.muiName = COMPONENT_NAME;
Autocomplete.defaultProps = {};

export default Autocomplete;
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
Achintha444 marked this conversation as resolved.
Show resolved Hide resolved
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import {AutocompleteRenderInputParams} from '@mui/material/Autocomplete/Autocomplete';
import {render} from '@unit-testing';
import {ReactNode} from 'react';
import TextField from '../../TextField';
import Autocomplete from '../Autocomplete';

describe('Alert', () => {
it('should render successfully', () => {
const {baseElement} = render(
<Autocomplete
disablePortal
options={[
{label: 'The Shawshank Redemption', year: 1994},
{label: 'The Godfather', year: 1972},
{label: 'The Godfather: Part II', year: 1974},
{label: 'The Dark Knight', year: 2008},
{label: '12 Angry Men', year: 1957},
{label: "Schindler's List", year: 1993},
{label: 'Pulp Fiction', year: 1994},
]}
renderInput={(params: AutocompleteRenderInputParams): ReactNode => <TextField {...params} label="Movie" />}
/>,
);
expect(baseElement).toBeTruthy();
});

it('should match the snapshot', () => {
const {baseElement} = render(
<Autocomplete
disablePortal
options={[
{label: 'The Shawshank Redemption', year: 1994},
{label: 'The Godfather', year: 1972},
{label: 'The Godfather: Part II', year: 1974},
{label: 'The Dark Knight', year: 2008},
{label: '12 Angry Men', year: 1957},
{label: "Schindler's List", year: 1993},
{label: 'Pulp Fiction', year: 1994},
]}
renderInput={(params: AutocompleteRenderInputParams): ReactNode => <TextField {...params} label="Movie" />}
/>,
);
expect(baseElement).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Alert should match the snapshot 1`] = `
<body>
<div>
<div
class="MuiAutocomplete-root MuiAutocomplete-hasPopupIcon oxygen-autocomplete css-l3ln04-MuiAutocomplete-root"
>
<div
class="oxygen-text-field"
>
<label
aria-describedby=":r2:"
class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-animated MuiFormLabel-colorPrimary MuiInputLabel-root MuiInputLabel-animated oxygen-input-label css-f3834g-MuiFormLabel-root-MuiInputLabel-root"
for=":r2:"
id=":r2:-label"
>
Movie
</label>
<div
class="MuiFormControl-root MuiFormControl-fullWidth MuiTextField-root css-wb57ya-MuiFormControl-root-MuiTextField-root"
>
<div
class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-colorPrimary MuiInputBase-fullWidth MuiInputBase-formControl MuiInputBase-adornedEnd MuiAutocomplete-inputRoot css-115mmlx-MuiInputBase-root-MuiOutlinedInput-root"
>
<input
aria-autocomplete="list"
aria-expanded="false"
aria-invalid="false"
autocapitalize="none"
autocomplete="off"
class="MuiInputBase-input MuiOutlinedInput-input MuiInputBase-inputAdornedEnd MuiAutocomplete-input MuiAutocomplete-inputFocused css-11fn00k-MuiInputBase-input-MuiOutlinedInput-input"
id=":r2:"
role="combobox"
spellcheck="false"
type="text"
value=""
/>
<div
class="MuiAutocomplete-endAdornment css-1q60rmi-MuiAutocomplete-endAdornment"
>
<button
aria-label="Open"
class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium MuiAutocomplete-popupIndicator css-1n4oo3i-MuiButtonBase-root-MuiIconButton-root-MuiAutocomplete-popupIndicator"
tabindex="-1"
title="Open"
type="button"
>
<svg
aria-hidden="true"
class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-i4bv87-MuiSvgIcon-root"
data-testid="ArrowDropDownIcon"
focusable="false"
viewBox="0 0 24 24"
>
<path
d="M7 10l5 5 5-5z"
/>
</svg>
<span
class="MuiTouchRipple-root css-8je8zh-MuiTouchRipple-root"
/>
</button>
</div>
<fieldset
aria-hidden="true"
class="MuiOutlinedInput-notchedOutline css-116asjz-MuiOutlinedInput-notchedOutline"
>
<legend
class="css-ihdtdm"
>
<span
class="notranslate"
>
</span>
</legend>
</fieldset>
</div>
</div>
</div>
</div>
</div>
</body>
`;
26 changes: 26 additions & 0 deletions packages/react/src/components/Autocomplete/autocomplete.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
Achintha444 marked this conversation as resolved.
Show resolved Hide resolved
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

.oxygen-autocomplete {
padding-top: 14px !important;
padding-bottom: 14px !important;

.MuiButtonBase-root {
height: 32px !important;
}
}
20 changes: 20 additions & 0 deletions packages/react/src/components/Autocomplete/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
Achintha444 marked this conversation as resolved.
Show resolved Hide resolved
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export {default} from './Autocomplete';
export type {AutocompleteProps} from './Autocomplete';
3 changes: 3 additions & 0 deletions packages/react/src/components/Chip/chip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/

.oxygen-chip {
font-size: 0.8571rem !important;
padding: 7px 3px !important;

&-premium {
background: var(--oxygen-customComponents-Chip-properties-premium-background);
color: var(--oxygen-customComponents-Chip-properties-premium-text-color);
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export * from './AppBar';
export {default as AppShell} from './AppShell';
export * from './AppShell';

export {default as Autocomplete} from './Autocomplete';
export * from './Autocomplete';

export {default as Avatar} from './Avatar';
export * from './Avatar';

Expand Down
Loading