-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
157 additions
and
12 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,8 @@ | ||
--- | ||
title: Autocomplete | ||
description: Autocomplete component offers simple and flexible type-ahead functionality. This is useful when searching large sets of data or even dynamically requesting information from an API. | ||
--- | ||
|
||
## Usage | ||
|
||
<usage></usage> |
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 |
---|---|---|
|
@@ -111,6 +111,9 @@ const components = [ | |
{ | ||
name: 'Ripple' | ||
}, | ||
{ | ||
name: 'Autocomplete' | ||
}, | ||
{ | ||
name: 'Select' | ||
}, | ||
|
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,25 @@ | ||
import { Autocomplete, Item } from 'actify' | ||
|
||
export default () => { | ||
const options = [ | ||
{ id: 1, name: 'Actify' }, | ||
{ id: 2, name: 'Ngroker' }, | ||
{ id: 3, name: 'Taildoor' }, | ||
{ id: 4, name: 'Hugola' } | ||
] | ||
|
||
return ( | ||
<div className="max-sm:flex-wrap flex gap-4"> | ||
<Autocomplete label="project" onSelectionChange={(e) => console.log(e)}> | ||
{options.map(({ id, name }) => ( | ||
<Item key={id}>{name}</Item> | ||
))} | ||
</Autocomplete> | ||
<Autocomplete variant="outlined" label="project"> | ||
{options.map(({ id, name }) => ( | ||
<Item key={id}>{name}</Item> | ||
))} | ||
</Autocomplete> | ||
</div> | ||
) | ||
} |
79 changes: 79 additions & 0 deletions
79
packages/actify/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,79 @@ | ||
import { AriaComboBoxProps, useComboBox, useFilter } from 'react-aria' | ||
import { ComboBoxStateOptions, useComboBoxState } from 'react-stately' | ||
|
||
import { Icon } from '../Icon' | ||
import { ListBox } from '../ListBox' | ||
import { Popover } from '../Popover' | ||
import React from 'react' | ||
import { TextField } from '../TextFields' | ||
|
||
interface AutocompleteProps<T> | ||
extends Omit<AriaComboBoxProps<T>, 'children'>, | ||
ComboBoxStateOptions<T> { | ||
variant?: 'filled' | 'outlined' | ||
} | ||
|
||
const Autocomplete = <T extends object>(props: AutocompleteProps<T>) => { | ||
// Setup filter function and state | ||
const { contains } = useFilter({ sensitivity: 'base' }) | ||
const state = useComboBoxState({ ...props, defaultFilter: contains }) | ||
|
||
// Setup refs and get props for child elements. | ||
const inputRef = React.useRef<HTMLInputElement>(null) | ||
const listBoxRef = React.useRef(null) | ||
const popoverRef = React.useRef<HTMLDivElement>(null) | ||
|
||
const { inputProps, listBoxProps } = useComboBox( | ||
{ | ||
...props, | ||
inputRef, | ||
listBoxRef, | ||
popoverRef | ||
}, | ||
state | ||
) | ||
|
||
return ( | ||
<div style={{ display: 'inline-flex', flexDirection: 'column' }}> | ||
<TextField | ||
label={props.label} | ||
variant={props.variant} | ||
inputRef={inputRef} | ||
inputProps={inputProps} | ||
trailingIcon={ | ||
<Icon | ||
style={{ cursor: 'pointer' }} | ||
onClick={() => { | ||
state.setOpen(!state.isOpen) | ||
// If the input is focused, move the cursor to the end | ||
inputRef.current?.setSelectionRange( | ||
inputRef.current.value.length, | ||
inputRef.current.value.length | ||
) | ||
}} | ||
> | ||
Arrow_Drop_Down | ||
</Icon> | ||
} | ||
/> | ||
|
||
{state.isOpen && ( | ||
<Popover | ||
state={state} | ||
triggerRef={inputRef} | ||
popoverRef={popoverRef} | ||
placement="bottom start" | ||
> | ||
<ListBox | ||
state={state} | ||
listBoxRef={listBoxRef} | ||
listBoxProps={listBoxProps} | ||
/> | ||
</Popover> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
Autocomplete.displayName = 'Actify.Autocomplete' | ||
export { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { Autocomplete } from './Autocomplete' | ||
export { Item } from 'react-stately' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { ListBox } from './ListBox' | ||
export { Option } from './Option' |
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