-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
af35479
commit 15f4e55
Showing
7 changed files
with
111 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
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,57 @@ | ||
import { useCombobox } from 'downshift' | ||
import React, { memo, useState } from 'react' | ||
|
||
export interface Props { | ||
items: any[] | ||
} | ||
|
||
export const Select = memo(({ items }: Props) => { | ||
const [inputItems, setInputItems] = useState(items) | ||
const { | ||
isOpen, | ||
// selectedItem, | ||
getToggleButtonProps, | ||
getLabelProps, | ||
getMenuProps, | ||
getInputProps, | ||
getComboboxProps, | ||
highlightedIndex, | ||
getItemProps, | ||
} = useCombobox({ | ||
items: inputItems, | ||
onInputValueChange: ({ inputValue }) => { | ||
setInputItems( | ||
items.filter((item) => | ||
item.toLowerCase().startsWith(inputValue.toLowerCase()), | ||
), | ||
) | ||
}, | ||
}) | ||
return ( | ||
<> | ||
<label {...getLabelProps()}>Choose an element:</label> | ||
<div {...getComboboxProps()}> | ||
<input {...getInputProps()} /> | ||
<button {...getToggleButtonProps()} aria-label="toggle menu"> | ||
↓ | ||
</button> | ||
</div> | ||
<ul {...getMenuProps()}> | ||
{isOpen && | ||
inputItems.map((item, index) => ( | ||
<li | ||
style={ | ||
highlightedIndex === index | ||
? { backgroundColor: '#bde4ff' } | ||
: {} | ||
} | ||
key={`${item}${index}`} | ||
{...getItemProps({ item, index })} | ||
> | ||
{item} | ||
</li> | ||
))} | ||
</ul> | ||
</> | ||
) | ||
}) |
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 @@ | ||
import { array, withKnobs } from '@storybook/addon-knobs' | ||
import React from 'react' | ||
|
||
import { Select } from '.' | ||
|
||
export default { | ||
title: 'Select', | ||
component: Select, | ||
decorators: [withKnobs], | ||
} | ||
|
||
export const SelectStory = () => { | ||
const items = array('items', []) | ||
|
||
return <Select items={items} /> | ||
} | ||
|
||
SelectStory.story = { | ||
name: '', | ||
} |
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,3 @@ | ||
import { styled } from '@storybook/theming' | ||
|
||
export const Root = styled.div`` |
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 @@ | ||
import React, { memo } from 'react' | ||
|
||
import { SelectSchema } from '../../types' | ||
import { Select } from '../Select' | ||
import { Error, Row } from './styled' | ||
|
||
export interface Props { | ||
schema: SelectSchema | ||
value: any | ||
error: string | null | ||
isValid: boolean | ||
onChange: (value: any) => void | ||
} | ||
|
||
export const SelectInput = memo(({ error, isValid }: Props) => { | ||
return ( | ||
<Row> | ||
<Select items={[]} /> | ||
{!isValid && <Error>{error}</Error>} | ||
</Row> | ||
) | ||
}) |
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