-
Notifications
You must be signed in to change notification settings - Fork 34
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 #1725 from okta/ee/autocomplete
Style Autocomplete
- Loading branch information
Showing
8 changed files
with
441 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/*! | ||
* Copyright (c) 2023-present, Okta, Inc. and/or its affiliates. All rights reserved. | ||
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (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 { | ||
Autocomplete as MuiAutocomplete, | ||
AutocompleteProps as MuiAutocompleteProps, | ||
InputBase, | ||
} from "@mui/material"; | ||
import { memo, useCallback } from "react"; | ||
|
||
import { Field } from "./Field"; | ||
|
||
export type AutocompleteProps< | ||
OptionType, | ||
HasMultipleChoices extends boolean | undefined, | ||
IsCustomValueAllowed extends boolean | undefined | ||
> = { | ||
hasMultipleChoices?: MuiAutocompleteProps< | ||
OptionType, | ||
HasMultipleChoices, | ||
undefined, | ||
IsCustomValueAllowed | ||
>["multiple"]; | ||
hint?: string; | ||
isCustomValueAllowed?: MuiAutocompleteProps< | ||
OptionType, | ||
HasMultipleChoices, | ||
undefined, | ||
IsCustomValueAllowed | ||
>["freeSolo"]; | ||
isDisabled?: MuiAutocompleteProps< | ||
OptionType, | ||
HasMultipleChoices, | ||
undefined, | ||
IsCustomValueAllowed | ||
>["disabled"]; | ||
isLoading?: MuiAutocompleteProps< | ||
OptionType, | ||
HasMultipleChoices, | ||
undefined, | ||
IsCustomValueAllowed | ||
>["loading"]; | ||
isReadOnly?: MuiAutocompleteProps< | ||
OptionType, | ||
HasMultipleChoices, | ||
undefined, | ||
IsCustomValueAllowed | ||
>["readOnly"]; | ||
label: string; | ||
onChange?: MuiAutocompleteProps< | ||
OptionType, | ||
HasMultipleChoices, | ||
undefined, | ||
IsCustomValueAllowed | ||
>["onChange"]; | ||
options: MuiAutocompleteProps< | ||
OptionType, | ||
HasMultipleChoices, | ||
undefined, | ||
IsCustomValueAllowed | ||
>["options"]; | ||
value?: MuiAutocompleteProps< | ||
OptionType, | ||
HasMultipleChoices, | ||
undefined, | ||
IsCustomValueAllowed | ||
>["value"]; | ||
}; | ||
|
||
const Autocomplete = < | ||
OptionType, | ||
HasMultipleChoices extends boolean | undefined, | ||
IsCustomValueAllowed extends boolean | undefined | ||
>({ | ||
isCustomValueAllowed, | ||
hasMultipleChoices, | ||
isDisabled, | ||
isLoading, | ||
isReadOnly, | ||
hint, | ||
label, | ||
onChange, | ||
options, | ||
value, | ||
}: AutocompleteProps<OptionType, HasMultipleChoices, IsCustomValueAllowed>) => { | ||
const renderInput = useCallback( | ||
({ InputLabelProps, InputProps, ...params }) => ( | ||
<Field | ||
{...InputLabelProps} | ||
hasVisibleLabel | ||
hint={hint} | ||
label={label} | ||
renderFieldComponent={({ ariaDescribedBy, id }) => ( | ||
<InputBase | ||
{...params} | ||
{...InputProps} | ||
aria-describedby={ariaDescribedBy} | ||
id={id} | ||
/> | ||
)} | ||
/> | ||
), | ||
[hint, label] | ||
); | ||
|
||
return ( | ||
<MuiAutocomplete | ||
disabled={isDisabled} | ||
freeSolo={isCustomValueAllowed} | ||
loading={isLoading} | ||
multiple={hasMultipleChoices} | ||
onChange={onChange} | ||
options={options} | ||
readOnly={isReadOnly} | ||
renderInput={renderInput} | ||
value={value} | ||
/> | ||
); | ||
}; | ||
|
||
const MemoizedAutocomplete = memo(Autocomplete) as typeof Autocomplete; | ||
|
||
export { MemoizedAutocomplete as 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
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
79 changes: 79 additions & 0 deletions
79
...ages/odyssey-storybook/src/components/odyssey-mui/Autocomplete/Autocomplete.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,79 @@ | ||
import { ArgsTable, Canvas, Meta, Story } from "@storybook/addon-docs"; | ||
|
||
<Meta anchor /> | ||
|
||
# Autocomplete | ||
|
||
Similar to <a href="?path=/docs/mui-components-forms-select--default">Select</a>, this input triggers a menu of options a user can select. Country and state Autocompletes are common examples. | ||
|
||
<Canvas> | ||
<Story id="mui-components-forms-autocomplete--default" /> | ||
</Canvas> | ||
|
||
## Behavior | ||
|
||
Interacting with an Autocomplete displays a list of values to choose from. Users may filter the options list by typing. | ||
|
||
## Variants | ||
|
||
Odyssey provides support for both single and multi-value Autocompletes. | ||
|
||
### Single-select | ||
|
||
With the single-select variant, choosing a value will override any previous selection and close the Autocomplete. | ||
|
||
#### Enabled | ||
|
||
<Canvas> | ||
<Story id="mui-components-forms-autocomplete--default" /> | ||
</Canvas> | ||
|
||
#### Disabled | ||
|
||
<Canvas> | ||
<Story id="mui-components-forms-autocomplete--disabled" /> | ||
</Canvas> | ||
|
||
#### Read-only | ||
|
||
<Canvas> | ||
<Story id="mui-components-forms-autocomplete--read-only" /> | ||
</Canvas> | ||
|
||
### Multi-Select | ||
|
||
The multi-Select variant allows users to select many values. | ||
|
||
#### Enabled | ||
|
||
<Canvas> | ||
<Story id="mui-components-forms-autocomplete--multiple" /> | ||
</Canvas> | ||
|
||
#### Disabled | ||
|
||
<Canvas> | ||
<Story id="mui-components-forms-autocomplete--multiple-disabled" /> | ||
</Canvas> | ||
|
||
#### Read-only | ||
|
||
<Canvas> | ||
<Story id="mui-components-forms-autocomplete--multiple-read-only" /> | ||
</Canvas> | ||
|
||
## Loading state | ||
|
||
The loading state is displayed when retrieving values from the server or when data is unavailable. | ||
|
||
<Canvas> | ||
<Story id="mui-components-forms-autocomplete--loading" /> | ||
</Canvas> | ||
|
||
## Custom Values | ||
|
||
Autocomplete also supports user-submitted values via the `isCustomValueAllowed` prop. | ||
|
||
<Canvas> | ||
<Story id="mui-components-forms-autocomplete--is-custom-value-allowed" /> | ||
</Canvas> |
Oops, something went wrong.