Merge pull request #3013 from saghul/toolbox-disabled-button

[RN] Toolbox disabled button styles
This commit is contained in:
Zoltan Bettenbuk 2018-05-23 12:02:49 +02:00 committed by GitHub
commit 33a748b569
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import { Platform } from '../react';
import { ColorPalette } from './components'; import { ColorPalette } from './components';
declare type StyleSheet = Object; declare type StyleSheet = Object;
export type StyleType = StyleSheet | Array<StyleSheet>;
/** /**
* The list of the well-known style properties which may not be numbers on Web * The list of the well-known style properties which may not be numbers on Web
@ -14,7 +15,35 @@ declare type StyleSheet = Object;
*/ */
const _WELL_KNOWN_NUMBER_PROPERTIES = [ 'height', 'width' ]; const _WELL_KNOWN_NUMBER_PROPERTIES = [ 'height', 'width' ];
/* eslint-disable flowtype/space-before-type-colon */ /**
* Combines the given 2 styles into a single one.
*
* @param {StyleType} a - An object or array of styles.
* @param {StyleType} b - An object or array of styles.
* @private
* @returns {StyleType} - The merged styles.
*/
export function combineStyles(a: StyleType, b: StyleType): StyleType {
const result = [];
if (a) {
if (Array.isArray(a)) {
result.push(...a);
} else {
result.push(a);
}
}
if (b) {
if (Array.isArray(b)) {
result.push(...b);
} else {
result.push(b);
}
}
return result;
}
/** /**
* Create a style sheet using the provided style definitions. * Create a style sheet using the provided style definitions.
@ -25,11 +54,8 @@ const _WELL_KNOWN_NUMBER_PROPERTIES = [ 'height', 'width' ];
* (often platform-independent) styles. * (often platform-independent) styles.
* @returns {StyleSheet} * @returns {StyleSheet}
*/ */
export function createStyleSheet(styles: StyleSheet, overrides: StyleSheet = {}) export function createStyleSheet(
: StyleSheet { styles: StyleSheet, overrides: StyleSheet = {}): StyleSheet {
/* eslint-enable flowtype/space-before-type-colon */
const combinedStyles = {}; const combinedStyles = {};
for (const k of Object.keys(styles)) { for (const k of Object.keys(styles)) {

View File

@ -2,6 +2,8 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { combineStyles } from '../../styles';
import type { Styles } from './AbstractToolboxItem'; import type { Styles } from './AbstractToolboxItem';
import ToolboxItem from './ToolboxItem'; import ToolboxItem from './ToolboxItem';
@ -12,6 +14,12 @@ export type Props = {
*/ */
afterClick: ?Function, afterClick: ?Function,
/**
* Extra styles which will be applied in conjunction with `styles` or
* `toggledStyles` when the button is disabled;
*/
disabledStyles: ?Styles,
/** /**
* Whether to show the label or not. * Whether to show the label or not.
*/ */
@ -38,12 +46,27 @@ export type Props = {
visible: boolean visible: boolean
}; };
/**
* Default style for disabled buttons.
*/
export const defaultDisabledButtonStyles = {
iconStyle: {
opacity: 0.5
},
labelStyle: {
opacity: 0.5
},
style: undefined,
underlayColor: undefined
};
/** /**
* An abstract implementation of a button. * An abstract implementation of a button.
*/ */
export default class AbstractButton<P: Props, S: *> extends Component<P, S> { export default class AbstractButton<P: Props, S: *> extends Component<P, S> {
static defaultProps = { static defaultProps = {
afterClick: undefined, afterClick: undefined,
disabledStyles: defaultDisabledButtonStyles,
showLabel: false, showLabel: false,
styles: undefined, styles: undefined,
toggledStyles: undefined, toggledStyles: undefined,
@ -151,10 +174,25 @@ export default class AbstractButton<P: Props, S: *> extends Component<P, S> {
* @private * @private
* @returns {?Styles} * @returns {?Styles}
*/ */
_getStyles() { _getStyles(): ?Styles {
const { styles, toggledStyles } = this.props; const { disabledStyles, styles, toggledStyles } = this.props;
const buttonStyles
= (this._isToggled() ? toggledStyles : styles) || styles;
return (this._isToggled() ? toggledStyles : styles) || styles; if (this._isDisabled() && buttonStyles && disabledStyles) {
return {
iconStyle: combineStyles(
buttonStyles.iconStyle, disabledStyles.iconStyle),
labelStyle: combineStyles(
buttonStyles.labelStyle, disabledStyles.labelStyle),
style: combineStyles(
buttonStyles.style, disabledStyles.style),
underlayColor:
disabledStyles.underlayColor || buttonStyles.underlayColor
};
}
return buttonStyles;
} }
/** /**

View File

@ -2,27 +2,29 @@
import { Component } from 'react'; import { Component } from 'react';
import type { StyleType } from '../../styles';
export type Styles = { export type Styles = {
/** /**
* Style for the item's icon. * Style for the item's icon.
*/ */
iconStyle: Object, iconStyle: StyleType,
/** /**
* Style for the item's label. * Style for the item's label.
*/ */
labelStyle: Object, labelStyle: StyleType,
/** /**
* Style for the item itself. * Style for the item itself.
*/ */
style: Object, style: StyleType,
/** /**
* Color for the item underlay (shows when clicked). * Color for the item underlay (shows when clicked).
*/ */
underlayColor: string underlayColor: ?string
}; };
export type Props = { export type Props = {