-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(selectbox): add SelectBox component
- Loading branch information
Artur Bien
committed
Dec 5, 2020
1 parent
aaa331a
commit 38094ba
Showing
7 changed files
with
214 additions
and
71 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,32 @@ | ||
import React, { useState } from 'react'; | ||
import { Panel, SelectBox, Fieldset } from 'react95-native'; | ||
|
||
const options = ['apple', 'orange', 'banana', 'pear', 'watermelon'].map(o => ({ | ||
label: o, | ||
value: o, | ||
})); | ||
|
||
const SelectBoxExample = () => { | ||
const [value, setValue] = useState(options[0].value); | ||
return ( | ||
<Panel style={{ flex: 1, padding: 20 }}> | ||
<Fieldset label='Default:' style={[{ padding: 20 }]}> | ||
<SelectBox | ||
options={options} | ||
value={value} | ||
onChange={newValue => setValue(newValue)} | ||
/> | ||
</Fieldset> | ||
<Fieldset label='Custom size:' style={[{ padding: 20 }]}> | ||
<SelectBox | ||
options={options} | ||
value={value} | ||
onChange={newValue => setValue(newValue)} | ||
style={[{ width: 200, height: 120 }]} | ||
/> | ||
</Fieldset> | ||
</Panel> | ||
); | ||
}; | ||
|
||
export default SelectBoxExample; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import React, { useState } from 'react'; | ||
import { StyleSheet, View, Text, TouchableHighlight } from 'react-native'; | ||
import { original as theme } from '../common/themes'; | ||
import { blockSizes } from '../common/styles'; | ||
|
||
export type Option = { | ||
value: any; | ||
label: React.ReactNode; | ||
}; | ||
|
||
type SelectItemProps = { | ||
option: Option; | ||
onPress: () => void; | ||
isSelected: boolean; | ||
}; | ||
|
||
const SelectItem = ({ option, onPress, isSelected }: SelectItemProps) => { | ||
const [isPressed, setIsPressed] = useState(false); | ||
|
||
return ( | ||
<TouchableHighlight | ||
onPress={() => onPress(option)} | ||
onHideUnderlay={() => setIsPressed(false)} | ||
onShowUnderlay={() => setIsPressed(true)} | ||
accessibilityRole='menuitem' | ||
underlayColor='none' | ||
// delay to prevent item highlighting on scroll | ||
delayPressIn={70} | ||
> | ||
<View | ||
style={[ | ||
styles.center, | ||
styles.optionWrapper, | ||
{ | ||
backgroundColor: | ||
isPressed || isSelected ? theme.hoverBackground : theme.canvas, | ||
}, | ||
]} | ||
> | ||
<Text | ||
style={[ | ||
styles.optionText, | ||
{ | ||
color: | ||
isPressed || isSelected | ||
? theme.canvasTextInvert | ||
: theme.canvasText, | ||
}, | ||
]} | ||
> | ||
{option.label} | ||
</Text> | ||
</View> | ||
</TouchableHighlight> | ||
); | ||
}; | ||
|
||
const selectHeight = blockSizes.md + 2; | ||
|
||
const styles = StyleSheet.create({ | ||
center: { | ||
flexGrow: 1, | ||
flex: 1, | ||
height: '100%', | ||
justifyContent: 'center', | ||
}, | ||
optionWrapper: { | ||
height: selectHeight - 4, | ||
}, | ||
optionText: { | ||
fontSize: 16, | ||
paddingLeft: 6, | ||
}, | ||
}); | ||
|
||
type SelectOptionsProps = { | ||
options: Array<Option>; | ||
values: [any]; | ||
disabled?: boolean; | ||
// TODO: what to put below? | ||
onChange: () => void; | ||
}; | ||
|
||
export default function getSelectOptions({ | ||
options, | ||
values, | ||
onChange, | ||
}: SelectOptionsProps) { | ||
const selectedOptions = options.filter(option => | ||
values.includes(option.value), | ||
); | ||
|
||
const optionItems = options.map(option => ( | ||
<SelectItem | ||
key={option.value} | ||
option={option} | ||
isSelected={selectedOptions.includes(option)} | ||
onPress={onChange} | ||
/> | ||
)); | ||
return [selectedOptions, optionItems]; | ||
} |
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,60 @@ | ||
import React from 'react'; | ||
import { StyleSheet, View, ScrollView } from 'react-native'; | ||
import { original as theme } from '../common/themes'; | ||
import { Border } from '../common/styleElements'; | ||
|
||
import getSelectOptions, { Option } from './SelectBase'; | ||
|
||
// TODO: multiselect | ||
// TODO: disabled, | ||
// TODO: original scrollbars | ||
|
||
type SelectBoxProps = { | ||
options: Array<Option>; | ||
value: [any] | any; | ||
// disabled?: boolean; | ||
// TODO: what to put below? | ||
onChange: () => void; | ||
style?: Object; | ||
}; | ||
|
||
const SelectBox = ({ | ||
value, | ||
options = [], | ||
// disabled = false, | ||
onChange, | ||
style, | ||
}: SelectBoxProps) => { | ||
function handleOptionSelect(option: Option) { | ||
onChange(option.value); | ||
} | ||
|
||
const [, selectOptions] = getSelectOptions({ | ||
options, | ||
values: [value], | ||
onChange: handleOptionSelect, | ||
}); | ||
|
||
return ( | ||
<View style={[styles.wrapper, style]}> | ||
<Border variant='cutout' /> | ||
<ScrollView> | ||
<View style={[styles.content]}>{selectOptions}</View> | ||
</ScrollView> | ||
</View> | ||
); | ||
}; | ||
|
||
export default SelectBox; | ||
|
||
const styles = StyleSheet.create({ | ||
wrapper: { | ||
paddingVertical: 4, | ||
paddingHorizontal: 4, | ||
backgroundColor: theme.canvas, | ||
}, | ||
content: { | ||
backgroundColor: theme.canvas, | ||
padding: 2, | ||
}, | ||
}); |
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 { default } from './Select'; | ||
export { default as Select } from './Select'; | ||
export { default as SelectBox } from './SelectBox'; |
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