|
| 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 | +import {MULTI_VALUED} from 'lib/constants'; |
| 9 | + |
| 10 | +class UnconnectedGroupCreator extends Component { |
| 11 | + getAllGroups() { |
| 12 | + return [...new Set(this.context.data.map(t => t[this.props.attr]))].filter(g => Boolean(g)); |
| 13 | + } |
| 14 | + |
| 15 | + canAddGroup() { |
| 16 | + const {fullContainer, attr} = this.props; |
| 17 | + const currentGroup = fullContainer[attr]; |
| 18 | + const currentTraceIndex = fullContainer.index; |
| 19 | + |
| 20 | + if (fullContainer.index === MULTI_VALUED) { |
| 21 | + return this.getAllGroups().length === 0; |
| 22 | + } |
| 23 | + |
| 24 | + return ( |
| 25 | + !currentGroup || |
| 26 | + this.context.fullData.some(d => d.index !== currentTraceIndex && d[attr] === currentGroup) |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + addAndUpdateGroup() { |
| 31 | + const allGroups = this.context.fullData |
| 32 | + .map(t => parseInt(t[this.props.attr], 10)) |
| 33 | + .filter(n => Number.isInteger(n)); |
| 34 | + // don't want to pass empty array to max |
| 35 | + allGroups.push(0); |
| 36 | + |
| 37 | + const lastGroupNumber = Math.max.apply(Math, allGroups); |
| 38 | + |
| 39 | + this.props.updatePlot(lastGroupNumber + 1); |
| 40 | + } |
| 41 | + |
| 42 | + render() { |
| 43 | + const {localize: _} = this.context; |
| 44 | + const {attr, label, prefix, updatePlot} = this.props; |
| 45 | + |
| 46 | + const options = [{label: _('None'), value: ''}]; |
| 47 | + const allGroups = this.getAllGroups(); |
| 48 | + allGroups.forEach(g => options.push({label: `${prefix} ${g}`, value: g})); |
| 49 | + options.sort((a, b) => a.value - b.value); |
| 50 | + |
| 51 | + const icon = <PlusIcon />; |
| 52 | + const addButton = this.canAddGroup() ? ( |
| 53 | + <Button variant="no-text" icon={icon} onClick={() => this.addAndUpdateGroup()} /> |
| 54 | + ) : ( |
| 55 | + <Button variant="no-text--disabled" icon={icon} onClick={() => {}} /> |
| 56 | + ); |
| 57 | + |
| 58 | + return ( |
| 59 | + <Dropdown |
| 60 | + label={label} |
| 61 | + attr={attr} |
| 62 | + clearable={false} |
| 63 | + options={options} |
| 64 | + updatePlot={updatePlot} |
| 65 | + extraComponent={addButton} |
| 66 | + /> |
| 67 | + ); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +UnconnectedGroupCreator.propTypes = { |
| 72 | + attr: PropTypes.string, |
| 73 | + fullContainer: PropTypes.object, |
| 74 | + prefix: PropTypes.string, |
| 75 | + ...Field.propTypes, |
| 76 | +}; |
| 77 | + |
| 78 | +UnconnectedGroupCreator.contextTypes = { |
| 79 | + localize: PropTypes.func, |
| 80 | + data: PropTypes.array, |
| 81 | + fullData: PropTypes.array, |
| 82 | +}; |
| 83 | + |
| 84 | +export default connectToContainer(UnconnectedGroupCreator); |
0 commit comments