-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from Achintha444/main
feat(react): add `Autocomplete` component
- Loading branch information
Showing
9 changed files
with
349 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/react/src/components/Autocomplete/Autocomplete.stories.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
<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
45
packages/react/src/components/Autocomplete/Autocomplete.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* | ||
* 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; |
63 changes: 63 additions & 0 deletions
63
packages/react/src/components/Autocomplete/__tests__/Autocomplete.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* | ||
* 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(); | ||
}); | ||
}); |
85 changes: 85 additions & 0 deletions
85
...ages/react/src/components/Autocomplete/__tests__/__snapshots__/Autocomplete.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
packages/react/src/components/Autocomplete/autocomplete.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* | ||
* 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* | ||
* 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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters