-
-
Notifications
You must be signed in to change notification settings - Fork 202
[WIP] SelectBox #197
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
Closed
Closed
[WIP] SelectBox #197
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
82f2a8f
feat(SelectBox): Init
pxd3v 113417a
* removes unused es-lint disable rule
pxd3v 89472e2
* Add End and Home button listeners
pxd3v 3c844c3
* fix options size to 31px
pxd3v f8f2793
* fix selectbox not working just by keyboard
pxd3v 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,104 @@ | ||
import React, { useRef, useEffect } from 'react'; | ||
import propTypes from 'prop-types'; | ||
import { StyledOptionsList, StyledOptionsListItem } from './SelectBox.styles'; | ||
import { StyledCutout } from '../Cutout/Cutout'; | ||
|
||
const SelectBox = React.forwardRef(function SelectBox(props, ref) { | ||
const { options, value, onSelect, width, height } = props; | ||
const selectedListItemRef = useRef(null); | ||
const listRef = useRef(null); | ||
|
||
const handleKeyDown = event => { | ||
pxd3v marked this conversation as resolved.
Show resolved
Hide resolved
|
||
switch (event.key) { | ||
case 'ArrowDown': | ||
if (value < options.length - 1) { | ||
event.preventDefault(); | ||
onSelect(value + 1); | ||
listRef.current.childNodes[value + 1].scrollIntoView({ | ||
block: 'end' | ||
}); | ||
} | ||
break; | ||
case 'ArrowUp': | ||
if (value > 0) { | ||
event.preventDefault(); | ||
onSelect(value - 1); | ||
listRef.current.childNodes[value - 1].scrollIntoView({ | ||
block: 'nearest' | ||
}); | ||
} | ||
break; | ||
case 'Home': | ||
onSelect(0); | ||
listRef.current.childNodes[0].scrollIntoView({ | ||
block: 'start' | ||
}); | ||
break; | ||
case 'End': | ||
onSelect(options.length - 1); | ||
listRef.current.childNodes[options.length - 1].scrollIntoView({ | ||
block: 'end' | ||
}); | ||
break; | ||
default: | ||
break; | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
selectedListItemRef.current.scrollIntoView({ | ||
block: 'start' | ||
}); | ||
listRef.current.focus(); | ||
}, [selectedListItemRef]); | ||
|
||
const handleClickOnItem = itemValue => { | ||
onSelect(itemValue); | ||
listRef.current.childNodes[itemValue].scrollIntoView({ | ||
block: 'nearest' | ||
}); | ||
}; | ||
|
||
return ( | ||
<StyledCutout ref={ref}> | ||
<StyledOptionsList | ||
style={{ width, height }} | ||
ref={listRef} | ||
onKeyDown={handleKeyDown} | ||
tabIndex={-1} | ||
> | ||
{options.map(option => ( | ||
<StyledOptionsListItem | ||
key={option.value.toString()} | ||
onClick={() => handleClickOnItem(option.value)} | ||
type='button' | ||
isSelected={option.value === value} | ||
ref={option.value === value ? selectedListItemRef : null} | ||
> | ||
{option.label} | ||
</StyledOptionsListItem> | ||
))} | ||
</StyledOptionsList> | ||
</StyledCutout> | ||
); | ||
}); | ||
|
||
SelectBox.defaultProps = { | ||
onSelect: () => {}, | ||
options: [], | ||
value: 0, | ||
width: '300px', | ||
height: '150px' | ||
}; | ||
|
||
SelectBox.propTypes = { | ||
onSelect: propTypes.func, | ||
options: propTypes.arrayOf( | ||
propTypes.shape({ value: propTypes.number, label: propTypes.string }) | ||
), | ||
value: propTypes.number, | ||
width: propTypes.string, | ||
height: propTypes.string | ||
}; | ||
|
||
export default SelectBox; |
This file contains hidden or 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,21 @@ | ||
import React from 'react'; | ||
import { renderWithTheme } from '../../test/utils'; | ||
import SelectBox from './SelectBox'; | ||
|
||
const options = [ | ||
{ label: 'ten', value: 0 }, | ||
{ label: 'twenty', value: 1 }, | ||
{ label: 'thirty', value: 2 } | ||
]; | ||
|
||
describe('<SelectBox />', () => { | ||
beforeEach(() => { | ||
window.HTMLElement.prototype.scrollIntoView = function() {}; | ||
}); | ||
it('should be able to mount the component', () => { | ||
const { container } = renderWithTheme(<SelectBox options={options} />); | ||
expect(container.querySelector('ul').querySelector('li').textContent).toBe( | ||
'ten' | ||
); | ||
}); | ||
}); |
This file contains hidden or 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,89 @@ | ||
import React, { useState } from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
import { Window, WindowContent, Cutout, Fieldset } from 'react95'; | ||
import SelectBox from './SelectBox'; | ||
|
||
const Wrapper = styled.div` | ||
background: ${({ theme }) => theme.material}; | ||
padding: 5rem; | ||
fieldset, | ||
fieldset { | ||
margin-bottom: 2rem; | ||
} | ||
legend + * { | ||
margin-bottom: 1rem; | ||
} | ||
#default-selects { | ||
width: 200px; | ||
} | ||
#cutout > div { | ||
width: 250px; | ||
padding: 1rem; | ||
background: ${({ theme }) => theme.canvas}; | ||
& > p { | ||
margin-bottom: 2rem; | ||
} | ||
} | ||
`; | ||
|
||
export default { | ||
title: 'SelectBox', | ||
component: SelectBox, | ||
decorators: [story => <Wrapper>{story()}</Wrapper>] | ||
}; | ||
|
||
const options = [ | ||
{ value: 0, label: '[None]' }, | ||
{ value: 1, label: 'Pikachu' }, | ||
{ value: 2, label: 'Bulbasaur' }, | ||
{ value: 3, label: 'Squirtle' }, | ||
{ value: 4, label: 'Mega Charizard Y' }, | ||
{ value: 5, label: 'Jigglypuff' }, | ||
{ value: 6, label: 'Snorlax' }, | ||
{ value: 7, label: 'Geodude' } | ||
]; | ||
|
||
export const Default = () => { | ||
const [selected, setSelected] = useState(0); | ||
const onSelect = value => { | ||
setSelected(value); | ||
}; | ||
return ( | ||
<div id='default-selects'> | ||
<Fieldset label='default'> | ||
<SelectBox options={options} value={selected} onSelect={onSelect} /> | ||
</Fieldset> | ||
</div> | ||
); | ||
}; | ||
|
||
Default.story = { | ||
name: 'default' | ||
}; | ||
|
||
export const Flat = () => ( | ||
<Window> | ||
<WindowContent> | ||
<Cutout id='cutout'> | ||
<p> | ||
When you want to use SelectBox on a light background (like scrollable | ||
content), just use the flat variant: | ||
</p> | ||
<Fieldset label='flat' variant='flat'> | ||
<SelectBox /> | ||
</Fieldset> | ||
</Cutout> | ||
</WindowContent> | ||
</Window> | ||
); | ||
|
||
Flat.story = { | ||
name: 'flat' | ||
}; | ||
|
||
export const CustomDisplayFormatting = () => <SelectBox />; | ||
|
||
CustomDisplayFormatting.story = { | ||
name: 'custom display formatting' | ||
}; |
This file contains hidden or 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,33 @@ | ||
import styled from 'styled-components'; | ||
pxd3v marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import { createScrollbars } from '../common'; | ||
import { blockSizes } from '../common/system'; | ||
|
||
export const StyledOptionsList = styled.ul` | ||
position: relative; | ||
box-sizing: border-box; | ||
background-color: #fff; | ||
font-size: 1rem; | ||
line-height: 1.5; | ||
overflow-y: auto; | ||
${({ variant }) => createScrollbars(variant)} | ||
outline: none; | ||
`; | ||
|
||
export const StyledOptionsListItem = styled.li` | ||
box-sizing: border-box; | ||
margin: 0; | ||
padding: 0; | ||
height: 31px; | ||
line-height: calc(${blockSizes.md} - 4px); | ||
background: ${({ theme, isSelected }) => | ||
isSelected ? theme.hoverBackground : 'none'}; | ||
color: ${({ theme, isSelected }) => | ||
isSelected ? theme.canvasTextInvert : '#000'}; | ||
width: 100%; | ||
padding-left: 8px; | ||
white-space: nowrap; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
user-select: none; | ||
`; |
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.
Uh oh!
There was an error while loading. Please reload this page.