-
-
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.
- Loading branch information
Artur Bien
committed
Dec 5, 2020
1 parent
b4c85a3
commit aaa331a
Showing
7 changed files
with
257 additions
and
10 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,36 @@ | ||
import React, { useState } from 'react'; | ||
import { View } from 'react-native'; | ||
import { Tabs, Panel, Text } from 'react95-native'; | ||
|
||
const AppBarExample = () => { | ||
const [value, setValue] = useState(0); | ||
const [secondValue, setSecondValue] = useState(0); | ||
|
||
return ( | ||
<Panel variant='clear' style={[{ flex: 1 }]}> | ||
<View style={[{ padding: 8 }]}> | ||
<Tabs value={value} onChange={setValue}> | ||
<Tabs.Tab value={0}>Shoes</Tabs.Tab> | ||
<Tabs.Tab value={1}>Accesories</Tabs.Tab> | ||
<Tabs.Tab value={2}>Clothing</Tabs.Tab> | ||
</Tabs> | ||
<Tabs.Body style={[{ height: 200 }]}> | ||
<Text>{value}</Text> | ||
</Tabs.Body> | ||
</View> | ||
|
||
<View style={[{ padding: 8 }]}> | ||
<Tabs stretch value={secondValue} onChange={setSecondValue}> | ||
<Tabs.Tab value={0}>Shoes</Tabs.Tab> | ||
<Tabs.Tab value={1}>A</Tabs.Tab> | ||
<Tabs.Tab value={2}>Clothing</Tabs.Tab> | ||
</Tabs> | ||
<Tabs.Body style={[{ height: 200 }]}> | ||
<Text>{secondValue}</Text> | ||
</Tabs.Body> | ||
</View> | ||
</Panel> | ||
); | ||
}; | ||
|
||
export default AppBarExample; |
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,179 @@ | ||
import React, { useState } from 'react'; | ||
import { | ||
StyleProp, | ||
StyleSheet, | ||
ViewStyle, | ||
TouchableHighlight, | ||
View, | ||
} from 'react-native'; | ||
|
||
import { original as theme } from '../common/themes'; | ||
import { border, padding, margin, blockSizes } from '../common/styles'; | ||
import { Border } from '../common/styleElements'; | ||
import { Text, Panel } from '..'; | ||
|
||
type TabsProps = { | ||
children?: React.ReactNode; | ||
style?: StyleProp<ViewStyle>; | ||
value: any; | ||
onChange?: () => void; | ||
stretch?: boolean; | ||
}; | ||
|
||
const Tabs = ({ | ||
value, | ||
onChange, | ||
children, | ||
stretch = false, | ||
...rest | ||
}: TabsProps) => { | ||
const childrenWithProps = React.Children.map(children, child => { | ||
if (!React.isValidElement(child)) { | ||
return null; | ||
} | ||
const tabProps = { | ||
selected: child.props.value === value, | ||
onPress: onChange, | ||
stretch, | ||
}; | ||
return React.cloneElement(child, tabProps); | ||
}); | ||
|
||
return ( | ||
<View style={[styles.tabs]} {...rest}> | ||
{childrenWithProps} | ||
<View style={[styles.tabBodyBorder]} /> | ||
</View> | ||
); | ||
}; | ||
|
||
type TabBodyProps = { | ||
children?: React.ReactNode; | ||
style?: StyleProp<ViewStyle>; | ||
}; | ||
|
||
const Body = ({ children, style, ...rest }: TabBodyProps) => { | ||
return ( | ||
<Panel style={[styles.body, style]} {...rest}> | ||
{children} | ||
</Panel> | ||
); | ||
}; | ||
|
||
type TabProps = { | ||
children?: React.ReactNode; | ||
style?: StyleProp<ViewStyle>; | ||
value: any; | ||
onPress?: () => void; | ||
selected?: boolean; | ||
stretch?: boolean; | ||
}; | ||
|
||
const Tab = ({ | ||
value, | ||
onPress, | ||
selected, | ||
stretch, | ||
children, | ||
...rest | ||
}: TabProps) => { | ||
const [isPressed, setIsPressed] = useState(false); | ||
|
||
return ( | ||
<TouchableHighlight | ||
onPress={() => onPress(value)} | ||
onHideUnderlay={() => setIsPressed(false)} | ||
onShowUnderlay={() => setIsPressed(true)} | ||
underlayColor='none' | ||
style={[ | ||
styles.tab, | ||
{ zIndex: selected ? 1 : 0 }, | ||
stretch ? { flexGrow: 1 } : { width: 'auto' }, | ||
selected ? margin(0, -8) : margin(0, 0), | ||
]} | ||
{...rest} | ||
> | ||
<View | ||
pointerEvents='none' | ||
style={[ | ||
styles.tabContent, | ||
{ | ||
height: selected ? blockSizes.md + 4 : blockSizes.md, | ||
}, | ||
selected ? padding(0, 16) : padding(0, 10), | ||
]} | ||
> | ||
{/* TODO: add 'background' boolean prop to Border component since its usually used with background color */} | ||
<Border | ||
radius={6} | ||
style={[ | ||
{ | ||
backgroundColor: theme.material, | ||
}, | ||
]} | ||
sharedStyle={{ | ||
borderBottomWidth: 0, | ||
borderBottomLeftRadius: 0, | ||
borderBottomRightRadius: 0, | ||
}} | ||
/> | ||
<Text>{children}</Text> | ||
<View style={[styles.mask]} /> | ||
{isPressed && <View style={[styles.focusOutline]} />} | ||
</View> | ||
</TouchableHighlight> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
tabs: { | ||
display: 'flex', | ||
flexDirection: 'row', | ||
alignItems: 'flex-end', | ||
paddingHorizontal: 8, | ||
zIndex: 1, | ||
bottom: -2, | ||
}, | ||
body: { | ||
display: 'flex', | ||
padding: 16, | ||
}, | ||
tab: { | ||
alignSelf: 'flex-end', | ||
}, | ||
tabContent: { | ||
justifyContent: 'center', | ||
width: 'auto', | ||
}, | ||
tabBodyBorder: { | ||
height: 4, | ||
position: 'absolute', | ||
left: 4, | ||
right: 4, | ||
bottom: -2, | ||
backgroundColor: theme.borderLight, | ||
borderTopWidth: 2, | ||
borderTopColor: theme.borderLightest, | ||
}, | ||
mask: { | ||
height: 4, | ||
position: 'absolute', | ||
left: 4, | ||
right: 4, | ||
bottom: -2, | ||
backgroundColor: theme.material, | ||
}, | ||
focusOutline: { | ||
position: 'absolute', | ||
left: 6, | ||
top: 6, | ||
bottom: 4, | ||
right: 6, | ||
...border.focusOutline, | ||
}, | ||
}); | ||
|
||
Tabs.Tab = Tab; | ||
Tabs.Body = Body; | ||
|
||
export default Tabs; |
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 @@ | ||
export { default } from './Tabs'; |
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