-
Notifications
You must be signed in to change notification settings - Fork 27
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
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e7d79de
feat(react): add Autocomplete component
4383e78
chore(react): fix PR comments
bb00ba9
chore(react): fix PR builder failures
8e8fc1b
chore(react): added scss file for the autocomplete component
42d30c7
chore(react): fix eslint issues
9062c46
chore(react): fix PR comments
56e639e
chore(react): fix PR comments
6d8083e
chore(react): edit autocomplete scss file
af0337e
chore(react): edit autocomplete scss file and change the chip scss
be33d05
chore(react): fix eslint issues
aa00d49
chore(react): fix eslint issues
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
98 changes: 98 additions & 0 deletions
98
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,98 @@ | ||
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={meta.title} component={meta.component} /> | ||
Achintha444 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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
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. | ||
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; |
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. | ||
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(); | ||
}); | ||
}); |
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> | ||
`; |
22 changes: 22 additions & 0 deletions
22
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,22 @@ | ||
/** | ||
* 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: 16px; | ||
padding-bottom: 16px; | ||
} |
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. | ||
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'; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.