|
| 1 | +import React, {Component} from 'react'; |
| 2 | +import {connectToContainer} from 'lib'; |
| 3 | +import Field from './Field'; |
| 4 | +import Dropdown from './Dropdown'; |
| 5 | +import PropTypes from 'prop-types'; |
| 6 | +import Button from '../widgets/Button'; |
| 7 | +import {PlusIcon} from 'plotly-icons'; |
| 8 | + |
| 9 | +class UnconnectedLegendgroupCreator extends Component { |
| 10 | + canAddGroup() { |
| 11 | + const currentGroup = this.props.fullContainer.legendgroup; |
| 12 | + const currentTraceIndex = this.props.fullContainer.index; |
| 13 | + |
| 14 | + return ( |
| 15 | + !currentGroup || |
| 16 | + this.context.fullData.some( |
| 17 | + d => d.index !== currentTraceIndex && d.legendgroup === currentGroup |
| 18 | + ) |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + addAndUpdateGroup() { |
| 23 | + const lastGroupNumber = |
| 24 | + Math.max.apply(Math, this.context.fullData.map(t => t.legendgroup)) || 0; |
| 25 | + |
| 26 | + this.props.updatePlot(lastGroupNumber + 1); |
| 27 | + } |
| 28 | + |
| 29 | + render() { |
| 30 | + const {localize: _} = this.context; |
| 31 | + |
| 32 | + const options = [{label: _('None'), value: ''}]; |
| 33 | + const allGroups = [...new Set(this.context.data.map(t => t.legendgroup))].filter( |
| 34 | + g => g !== undefined // eslint-disable-line no-undefined |
| 35 | + ); |
| 36 | + allGroups.forEach(g => options.push({label: _('Group ') + g, value: g})); |
| 37 | + options.sort((a, b) => a.value - b.value); |
| 38 | + |
| 39 | + const icon = <PlusIcon />; |
| 40 | + const addButton = this.canAddGroup() ? ( |
| 41 | + <Button variant="no-text" icon={icon} onClick={() => this.addAndUpdateGroup()} /> |
| 42 | + ) : ( |
| 43 | + <Button variant="no-text--disabled" icon={icon} onClick={() => {}} /> |
| 44 | + ); |
| 45 | + |
| 46 | + return ( |
| 47 | + <Dropdown |
| 48 | + label={_('Legend Group')} |
| 49 | + attr={this.props.attr} |
| 50 | + clearable={false} |
| 51 | + options={options} |
| 52 | + updatePlot={this.props.updatePlot} |
| 53 | + extraComponent={addButton} |
| 54 | + /> |
| 55 | + ); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +UnconnectedLegendgroupCreator.propTypes = { |
| 60 | + attr: PropTypes.string, |
| 61 | + container: PropTypes.object, |
| 62 | + fullContainer: PropTypes.object, |
| 63 | + ...Field.propTypes, |
| 64 | +}; |
| 65 | + |
| 66 | +UnconnectedLegendgroupCreator.contextTypes = { |
| 67 | + localize: PropTypes.func, |
| 68 | + data: PropTypes.array, |
| 69 | + fullData: PropTypes.array, |
| 70 | +}; |
| 71 | + |
| 72 | +export default connectToContainer(UnconnectedLegendgroupCreator); |
0 commit comments