2018-04-12 14:15:44 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import { Component } from 'react';
|
|
|
|
|
2018-05-22 10:41:23 +00:00
|
|
|
import type { StyleType } from '../../styles';
|
|
|
|
|
2018-04-12 14:15:44 +00:00
|
|
|
export type Styles = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Style for the item's icon.
|
|
|
|
*/
|
2018-05-22 10:41:23 +00:00
|
|
|
iconStyle: StyleType,
|
2018-04-12 14:15:44 +00:00
|
|
|
|
2018-05-15 11:18:42 +00:00
|
|
|
/**
|
2018-05-16 21:49:03 +00:00
|
|
|
* Style for the item's label.
|
2018-05-15 11:18:42 +00:00
|
|
|
*/
|
2018-05-22 10:41:23 +00:00
|
|
|
labelStyle: StyleType,
|
2018-05-15 11:18:42 +00:00
|
|
|
|
2018-04-12 14:15:44 +00:00
|
|
|
/**
|
|
|
|
* Style for the item itself.
|
|
|
|
*/
|
2018-05-22 10:41:23 +00:00
|
|
|
style: StyleType,
|
2018-04-12 14:15:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Color for the item underlay (shows when clicked).
|
|
|
|
*/
|
2018-05-22 10:41:23 +00:00
|
|
|
underlayColor: ?string
|
2018-04-12 14:15:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type Props = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A succinct description of what the item does. Used by accessibility
|
|
|
|
* tools and torture tests.
|
|
|
|
*/
|
|
|
|
accessibilityLabel: string,
|
|
|
|
|
2020-04-01 07:47:51 +00:00
|
|
|
/**
|
|
|
|
* An extra class name to be added at the end of the element's class name
|
|
|
|
* in order to enable custom styling.
|
|
|
|
*/
|
|
|
|
customClass?: string,
|
|
|
|
|
2018-04-12 14:15:44 +00:00
|
|
|
/**
|
|
|
|
* Whether this item is disabled or not. When disabled, clicking an the item
|
|
|
|
* has no effect, and it may reflect on its style.
|
|
|
|
*/
|
|
|
|
disabled: boolean,
|
|
|
|
|
2018-07-17 02:15:34 +00:00
|
|
|
/**
|
|
|
|
* A React Element to display at the end of {@code ToolboxItem}.
|
|
|
|
*/
|
|
|
|
elementAfter?: React$Node,
|
|
|
|
|
2018-04-12 14:15:44 +00:00
|
|
|
/**
|
2019-08-30 16:39:06 +00:00
|
|
|
* The icon to render for this {@code ToolboxItem}.
|
2018-04-12 14:15:44 +00:00
|
|
|
*/
|
2019-08-30 16:39:06 +00:00
|
|
|
icon: Object,
|
2018-04-12 14:15:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The text associated with this item. When `showLabel` is set to
|
|
|
|
* {@code true}, it will be displayed alongside the icon.
|
|
|
|
*/
|
|
|
|
label: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On click handler.
|
|
|
|
*/
|
|
|
|
onClick: Function,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to show the label or not.
|
|
|
|
*/
|
|
|
|
showLabel: boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collection of styles for the item. Used only on native.
|
|
|
|
*/
|
|
|
|
styles: ?Styles,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked to obtain translated strings.
|
|
|
|
*/
|
|
|
|
t: ?Function,
|
|
|
|
|
2019-08-30 16:39:06 +00:00
|
|
|
/**
|
|
|
|
* True if the item is toggled, false otherwise.
|
|
|
|
*/
|
|
|
|
toggled: boolean,
|
|
|
|
|
2018-04-12 14:15:44 +00:00
|
|
|
/**
|
|
|
|
* The text to display in the tooltip. Used only on web.
|
|
|
|
*/
|
2018-06-28 13:25:58 +00:00
|
|
|
tooltip: ?string,
|
2018-04-12 14:15:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* From which direction the tooltip should appear, relative to the
|
|
|
|
* item. Used only on web.
|
|
|
|
*/
|
|
|
|
tooltipPosition: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this item is visible or not.
|
|
|
|
*/
|
|
|
|
visible: boolean
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract (base) class for an item in {@link Toolbox}. The item can be located
|
|
|
|
* anywhere in the {@link Toolbox}, it will morph its shape to accommodate it.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
export default class AbstractToolboxItem<P : Props> extends Component<P> {
|
|
|
|
/**
|
|
|
|
* Default values for {@code AbstractToolboxItem} component's properties.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static defaultProps = {
|
|
|
|
disabled: false,
|
|
|
|
label: '',
|
|
|
|
showLabel: false,
|
|
|
|
t: undefined,
|
|
|
|
tooltip: '',
|
|
|
|
tooltipPosition: 'top',
|
|
|
|
visible: true
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new {@code AbstractToolboxItem} instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The React {@code Component} props to initialize
|
|
|
|
* the new {@code AbstractToolboxItem} instance with.
|
|
|
|
*/
|
|
|
|
constructor(props: P) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
// Bind event handlers so they are only bound once per instance.
|
|
|
|
this._onClick = this._onClick.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper property to get the item label. If a translation function was
|
|
|
|
* provided then it will be translated using it.
|
|
|
|
*
|
|
|
|
* @protected
|
2018-05-15 11:22:20 +00:00
|
|
|
* @returns {?string}
|
2018-04-12 14:15:44 +00:00
|
|
|
*/
|
2018-05-15 11:22:20 +00:00
|
|
|
get label(): ?string {
|
2018-04-12 14:15:44 +00:00
|
|
|
return this._maybeTranslateAttribute(this.props.label);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper property to get the item tooltip. If a translation function was
|
|
|
|
* provided then it will be translated using it.
|
|
|
|
*
|
|
|
|
* @protected
|
2018-05-15 11:22:20 +00:00
|
|
|
* @returns {?string}
|
2018-04-12 14:15:44 +00:00
|
|
|
*/
|
2018-05-15 11:22:20 +00:00
|
|
|
get tooltip(): ?string {
|
2018-04-12 14:15:44 +00:00
|
|
|
return this._maybeTranslateAttribute(this.props.tooltip);
|
|
|
|
}
|
|
|
|
|
2018-06-07 20:32:18 +00:00
|
|
|
/**
|
|
|
|
* Helper property to get the item accessibilityLabel. If a translation
|
|
|
|
* function was provided then it will be translated using it.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* @returns {?string}
|
|
|
|
*/
|
|
|
|
get accessibilityLabel(): ?string {
|
|
|
|
return this._maybeTranslateAttribute(this.props.accessibilityLabel);
|
|
|
|
}
|
|
|
|
|
2018-04-12 14:15:44 +00:00
|
|
|
/**
|
|
|
|
* Utility function to translate the given string, if a translation
|
|
|
|
* function is available.
|
|
|
|
*
|
|
|
|
* @param {string} text - What needs translating.
|
|
|
|
* @private
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
_maybeTranslateAttribute(text) {
|
|
|
|
const { t } = this.props;
|
|
|
|
|
2018-05-11 02:10:26 +00:00
|
|
|
return typeof t === 'function' ? t(text) : text;
|
2018-04-12 14:15:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onClick: (*) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles clicking/pressing this {@code AbstractToolboxItem} by
|
|
|
|
* forwarding the event to the {@code onClick} prop of this instance if any.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onClick(...args) {
|
|
|
|
const { disabled, onClick } = this.props;
|
|
|
|
|
2018-05-11 02:10:26 +00:00
|
|
|
disabled || (onClick && onClick(...args));
|
2018-04-12 14:15:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-16 21:49:03 +00:00
|
|
|
* Renders this {@code AbstractToolboxItem} (if it is {@code visible}). To
|
|
|
|
* be implemented/overridden by extenders. The default implementation of
|
|
|
|
* {@code AbstractToolboxItem} does nothing.
|
2018-04-12 14:15:44 +00:00
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_renderItem() {
|
|
|
|
// To be implemented by a subclass.
|
2019-03-19 15:42:25 +00:00
|
|
|
return null;
|
2018-04-12 14:15:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
2018-05-11 02:10:26 +00:00
|
|
|
return this.props.visible ? this._renderItem() : null;
|
2018-04-12 14:15:44 +00:00
|
|
|
}
|
|
|
|
}
|