import React, { Component, Fragment } from 'react'; import PropTypes from 'prop-types'; import { Animated } from 'react-native'; import withTheme from '../../Theme/withTheme'; import Icon from '../Icon/Icon.js'; import Ripple from '../Ripple/Ripple.js'; import BodyText from '../Typography/BodyText/BodyText.js'; import shadowTool from '../../Utils/Shadow/shadow.js'; import styles from './Fab.styles'; export class Fab extends Component { constructor(props) { super(props); } static propTypes = { style: PropTypes.oneOfType([PropTypes.array, PropTypes.object]), backgroundColor: PropTypes.string, onPress: PropTypes.func, disabled: PropTypes.bool, rippleColor: PropTypes.string, icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), shadow: PropTypes.number, mini: PropTypes.bool, theme: PropTypes.object, children: PropTypes.node, visible: PropTypes.bool, initialScale: PropTypes.number, animated: PropTypes.bool, label: PropTypes.string, testID: PropTypes.string, containerStyle: PropTypes.oneOfType([PropTypes.array, PropTypes.object]), }; static defaultProps = { visible: true, icon: 'add', }; state = { scale: new Animated.Value(0), }; componentDidMount() { const { visible, animated } = this.props; if (visible && animated) { this._animate(); } else if (visible) { this.setState({ scale: new Animated.Value(1) }); } } componentDidUpdate(prevProps) { const { visible, animated } = this.props; if (visible != prevProps.visible && animated) { this._animate(); } } _animate() { const { visible } = this.props; const { scale } = this.state; const scaleValue = visible ? 1 : 0; Animated.spring(scale, { toValue: scaleValue, speed: 7, useNativeDriver: true, }).start(); } _renderContent() { const { icon, label } = this.props; return ( {icon ? this._renderIcon() : null} {label ? this._renderLabel() : null} ); } _renderLabel() { const { label, disabled } = this.props; return ( ); } _renderIcon() { const { disabled, icon } = this.props; const color = disabled ? 'rgba(0, 0, 0, 0.26)' : 'white'; if (typeof icon == 'string' || icon instanceof String || !icon) { return ; } else { return React.cloneElement(icon, { size: icon.props.size ? icon.props.size : 24, color: icon.props.color ? icon.props.color : color, }); } } render() { const { style, backgroundColor, onPress, disabled, rippleColor, shadow, mini, theme, children, label, testID, containerStyle, ...props } = this.props; const { scale } = this.state; let backgroundColorApplied = backgroundColor ? backgroundColor : theme.primary.main; if (disabled) backgroundColorApplied = 'rgba(0, 0, 0, 0.12)'; let size = mini ? 40 : 56; let width = size; if (label) { size = 48; width = 'auto'; } return ( {children ? children : this._renderContent()} ); } } export default withTheme(Fab);