// // MDL-style Button component. // // - @see [MDL Button](http://www.getmdl.io/components/index.html#buttons-section) // - [Props](#props) // - [Defaults](#defaults) // - [Built-in builders](#builders) // // Created by ywu on 15/7/2. // import React, {Component, SFC} from 'react' import { LayoutChangeEvent, TextStyle, TouchableWithoutFeedback, TouchableWithoutFeedbackProps, } from 'react-native' import MKColor from '../MKColor' import {AttrValue, getTheme, Theme} from '../theme' import * as utils from '../utils' import Ripple, {RippleProps} from './Ripple' // ##
ButtonProps
export interface ButtonProps extends TouchableWithoutFeedbackProps, RippleProps { // Whether this's a FAB fab?: boolean // Whether the button is enabled enabled?: boolean } interface ButtonState { width: number height: number } // // ##
Button
// The `Button` component. With configurable shadow, ripple effect, and FAB style. // export default class Button extends Component { // ##
Defaults
static defaultProps: ButtonProps = { // [Ripple defaults](Ripple.html#defaults)... ...Ripple.defaultProps, enabled: true, fab: false, pointerEvents: 'box-only', }; theme: Theme; constructor(props: ButtonProps) { super(props); this.theme = getTheme(); this.state = { height: 0, width: 0, }; } _onLayout = ({nativeEvent: {layout: {width, height}}}: LayoutChangeEvent) => { if (width !== this.state.width || height !== this.state.height) { this.setState({ height, width, }); } }; render() { const touchableProps: TouchableWithoutFeedbackProps = {}; if (this.props.enabled) { Object.assign(touchableProps, utils.extractTouchableProps(this)); } // fix #57 applying `onLayout` to , clones `onLayout` to it's child touchableProps.onLayout = this._onLayout; const fabStyle: TextStyle = {}; const maskProps: RippleProps = {}; if (this.props.fab) { maskProps.maskBorderRadiusInPercent = 50; if (this.state.width > 0 || this.state.height > 0) { let size = Math.min(this.state.width, this.state.height); if (!size || size <= 0) { size = Math.max(this.state.width, this.state.height); } fabStyle.width = size; fabStyle.height = size; fabStyle.borderRadius = size / 2; } } return ( ); } } // -------------------------- // Pre-defined button variances. export const RaisedButton: SFC = props => customizedButton(raisedButton(), props); export const ColoredRaisedButton: SFC = props => customizedButton(coloredRaisedButton(), props); export const AccentRaisedButton: SFC = props => customizedButton(accentRaisedButton(), props); export const FlatButton: SFC = props => customizedButton(flatButton(), props); export const Fab: SFC = props => customizedButton(fab(), props); export const ColoredFab: SFC = props => customizedButton(coloredFab(), props); export const AccentFab: SFC = props => customizedButton(accentFab(), props); // Factory method to create a button variance function customizedButton( {style: baseStyle, ...baseProps}: ButtonProps, {style: customStyle, ...customProps}: ButtonProps ): JSX.Element { return