forked from react-native-elements/react-native-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added New Component: Speed dial (react-native-elements#2896)
* Docs Fixed * Added SpeedDial * Added SpeedDial * Docs Update * Fixed * Docs * Update SpeedDial.tsx * Fixed * Changed Props * Docs * Docs * Fixed Strict Errors * Fixed
- Loading branch information
1 parent
49f97d8
commit 7fdec69
Showing
13 changed files
with
431 additions
and
8 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,148 @@ | ||
import React from 'react'; | ||
import { | ||
Text, | ||
View, | ||
Animated, | ||
StyleSheet, | ||
SafeAreaView, | ||
TouchableWithoutFeedback, | ||
} from 'react-native'; | ||
import FAB, { FABProps } from './FAB'; | ||
import { withTheme } from '../config'; | ||
import { IconNode } from '../icons/Icon'; | ||
import Color from 'color'; | ||
import { RneFunctionComponent } from '../helpers'; | ||
|
||
export type SpeedDialActionProps = Omit<FABProps, 'size'>; | ||
|
||
const SpeedDialAction: RneFunctionComponent<SpeedDialActionProps> = withTheme( | ||
({ title, titleStyle, ...actionProps }) => { | ||
return ( | ||
<View style={styles.action}> | ||
{title && <Text style={[styles.title, titleStyle]}>{title}</Text>} | ||
<FAB {...actionProps} size="small" style={[actionProps.style]} /> | ||
</View> | ||
); | ||
}, | ||
'SpeedDial.Action' | ||
); | ||
|
||
export type SpeedDialProps = { | ||
isOpen: boolean; | ||
onOpen: () => void; | ||
onClose: () => void; | ||
openIcon?: IconNode; | ||
children?: React.ReactChild[]; | ||
transitionDuration?: number; | ||
} & FABProps; | ||
|
||
const SpeedDial: RneFunctionComponent<SpeedDialProps> = ({ | ||
theme, | ||
isOpen, | ||
onOpen = () => {}, | ||
onClose = () => {}, | ||
icon, | ||
openIcon, | ||
children, | ||
transitionDuration = 150, | ||
style, | ||
...props | ||
}) => { | ||
const animations = React.useRef<Animated.Value[]>( | ||
[...new Array(React.Children.count(children))].map( | ||
() => new Animated.Value(Number(isOpen)) | ||
) | ||
); | ||
|
||
React.useEffect(() => { | ||
Animated.stagger( | ||
50, | ||
animations.current | ||
.map((animation) => | ||
Animated.timing(animation, { | ||
toValue: Number(isOpen), | ||
duration: transitionDuration, | ||
useNativeDriver: true, | ||
}) | ||
) | ||
[isOpen ? 'reverse' : 'sort']() | ||
).start(); | ||
}, [isOpen, animations, children, transitionDuration]); | ||
|
||
return ( | ||
<View style={[styles.container, style]}> | ||
<TouchableWithoutFeedback onPress={onClose}> | ||
<Animated.View | ||
style={[ | ||
StyleSheet.absoluteFillObject, | ||
{ | ||
opacity: animations.current[0], | ||
backgroundColor: Color(theme?.colors?.black) | ||
.alpha(0.6) | ||
.rgb() | ||
.toString(), | ||
}, | ||
]} | ||
pointerEvents={isOpen ? 'auto' : 'none'} | ||
/> | ||
</TouchableWithoutFeedback> | ||
|
||
<SafeAreaView pointerEvents="box-none" style={styles.safeArea}> | ||
{React.Children.toArray(children).map((ChildAction, i: number) => ( | ||
<Animated.View | ||
pointerEvents={isOpen ? 'auto' : 'none'} | ||
key={i} | ||
style={{ | ||
transform: [{ scale: animations.current[i] }], | ||
opacity: animations.current[i], | ||
}} | ||
> | ||
{ChildAction} | ||
</Animated.View> | ||
))} | ||
<FAB | ||
style={[styles.fab]} | ||
icon={isOpen ? openIcon : icon} | ||
{...props} | ||
onPress={isOpen ? onClose : onOpen} | ||
/> | ||
</SafeAreaView> | ||
</View> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
safeArea: { | ||
alignItems: 'flex-end', | ||
}, | ||
container: { | ||
...StyleSheet.absoluteFillObject, | ||
justifyContent: 'flex-end', | ||
}, | ||
fab: { | ||
margin: 16, | ||
marginTop: 0, | ||
}, | ||
title: { | ||
backgroundColor: 'white', | ||
color: 'black', | ||
borderRadius: 5, | ||
paddingHorizontal: 12, | ||
paddingVertical: 6, | ||
marginVertical: 8, | ||
marginHorizontal: 16, | ||
elevation: 2, | ||
}, | ||
action: { | ||
marginBottom: 16, | ||
marginRight: 24, | ||
flexDirection: 'row', | ||
justifyContent: 'flex-end', | ||
alignItems: 'center', | ||
}, | ||
}); | ||
export { SpeedDial }; | ||
|
||
export default Object.assign(withTheme(SpeedDial, 'SpeedDial'), { | ||
Action: SpeedDialAction, | ||
}); |
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,31 @@ | ||
import React from 'react'; | ||
import { SpeedDial } from '../SpeedDial'; | ||
import { shallow } from 'enzyme'; | ||
import toJson from 'enzyme-to-json'; | ||
import theme from '../../config/theme'; | ||
|
||
describe('Speed Dial Component', () => { | ||
it('should render without issues', () => { | ||
const app = shallow( | ||
<SpeedDial | ||
theme={theme} | ||
open={true} | ||
icon={{ name: 'edit', color: '#fff' }} | ||
openIcon={{ name: 'close', color: '#fff' }} | ||
> | ||
<SpeedDial.Action | ||
icon={{ name: 'add', color: '#fff' }} | ||
title="Add" | ||
onPress={() => console.log('Add Something')} | ||
/> | ||
<SpeedDial.Action | ||
icon={{ name: 'delete', color: '#fff' }} | ||
title="Delete" | ||
onPress={() => console.log('Delete Something')} | ||
/> | ||
</SpeedDial> | ||
); | ||
expect(app.length).toBe(1); | ||
expect(toJson(app)).toMatchSnapshot(); | ||
}); | ||
}); |
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,121 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Speed Dial Component should render without issues 1`] = ` | ||
<View | ||
style={ | ||
Array [ | ||
Object { | ||
"bottom": 0, | ||
"justifyContent": "flex-end", | ||
"left": 0, | ||
"position": "absolute", | ||
"right": 0, | ||
"top": 0, | ||
}, | ||
undefined, | ||
] | ||
} | ||
> | ||
<TouchableWithoutFeedback | ||
onPress={[Function]} | ||
> | ||
<ForwardRef(AnimatedComponentWrapper) | ||
pointerEvents="none" | ||
style={ | ||
Array [ | ||
Object { | ||
"bottom": 0, | ||
"left": 0, | ||
"position": "absolute", | ||
"right": 0, | ||
"top": 0, | ||
}, | ||
Object { | ||
"backgroundColor": "rgba(36, 36, 36, 0.6)", | ||
"opacity": NaN, | ||
}, | ||
] | ||
} | ||
/> | ||
</TouchableWithoutFeedback> | ||
<ForwardRef(SafeAreaView) | ||
pointerEvents="box-none" | ||
style={ | ||
Object { | ||
"alignItems": "flex-end", | ||
} | ||
} | ||
> | ||
<ForwardRef(AnimatedComponentWrapper) | ||
key="0" | ||
pointerEvents="none" | ||
style={ | ||
Object { | ||
"opacity": NaN, | ||
"transform": Array [ | ||
Object { | ||
"scale": NaN, | ||
}, | ||
], | ||
} | ||
} | ||
> | ||
<Component | ||
icon={ | ||
Object { | ||
"color": "#fff", | ||
"name": "add", | ||
} | ||
} | ||
key=".0" | ||
onPress={[Function]} | ||
title="Add" | ||
/> | ||
</ForwardRef(AnimatedComponentWrapper)> | ||
<ForwardRef(AnimatedComponentWrapper) | ||
key="1" | ||
pointerEvents="none" | ||
style={ | ||
Object { | ||
"opacity": NaN, | ||
"transform": Array [ | ||
Object { | ||
"scale": NaN, | ||
}, | ||
], | ||
} | ||
} | ||
> | ||
<Component | ||
icon={ | ||
Object { | ||
"color": "#fff", | ||
"name": "delete", | ||
} | ||
} | ||
key=".1" | ||
onPress={[Function]} | ||
title="Delete" | ||
/> | ||
</ForwardRef(AnimatedComponentWrapper)> | ||
<Themed.FloatingActionButton | ||
icon={ | ||
Object { | ||
"color": "#fff", | ||
"name": "edit", | ||
} | ||
} | ||
onPress={[Function]} | ||
open={true} | ||
style={ | ||
Array [ | ||
Object { | ||
"margin": 16, | ||
"marginTop": 0, | ||
}, | ||
] | ||
} | ||
/> | ||
</ForwardRef(SafeAreaView)> | ||
</View> | ||
`; |
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
Oops, something went wrong.