|
| 1 | +import React, {Fragment, Component} from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import {connectToContainer} from 'lib'; |
| 4 | +import Field from './Field'; |
| 5 | +import Radio from './Radio'; |
| 6 | +import {UnconnectedDropdown} from './Dropdown'; |
| 7 | +import DataSelector from './DataSelector'; |
| 8 | + |
| 9 | +const LocationmodeVisible = connectToContainer(UnconnectedDropdown, { |
| 10 | + modifyPlotProps: (props, context, plotProps) => { |
| 11 | + if (!plotProps.fullValue) { |
| 12 | + plotProps.isVisible = true; |
| 13 | + plotProps.fullValue = plotProps.container.locationmode; |
| 14 | + return; |
| 15 | + } |
| 16 | + }, |
| 17 | +}); |
| 18 | + |
| 19 | +class UnconnectedLocation extends Component { |
| 20 | + render() { |
| 21 | + const {localize: _} = this.context; |
| 22 | + |
| 23 | + return ( |
| 24 | + <Fragment> |
| 25 | + <DataSelector label={_('Locations')} attr="locations" /> |
| 26 | + <LocationmodeVisible |
| 27 | + label={_('Location Format')} |
| 28 | + attr="locationmode" |
| 29 | + clearable={false} |
| 30 | + options={[ |
| 31 | + {label: _('Country Names'), value: 'country names'}, |
| 32 | + {label: _('Country Abbreviations (ISO-3)'), value: 'ISO-3'}, |
| 33 | + { |
| 34 | + label: _('USA State Abbreviations (e.g. NY)'), |
| 35 | + value: 'USA-states', |
| 36 | + }, |
| 37 | + ]} |
| 38 | + /> |
| 39 | + </Fragment> |
| 40 | + ); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +UnconnectedLocation.propTypes = { |
| 45 | + attr: PropTypes.string, |
| 46 | + ...Field.propTypes, |
| 47 | +}; |
| 48 | + |
| 49 | +UnconnectedLocation.contextTypes = { |
| 50 | + localize: PropTypes.func, |
| 51 | + updateContainer: PropTypes.func, |
| 52 | +}; |
| 53 | + |
| 54 | +const Location = connectToContainer(UnconnectedLocation); |
| 55 | + |
| 56 | +class UnconnectedLocationSelector extends Component { |
| 57 | + constructor(props, context) { |
| 58 | + super(props, context); |
| 59 | + |
| 60 | + this.state = { |
| 61 | + mode: props.container.locations ? 'location' : 'latlon', |
| 62 | + }; |
| 63 | + |
| 64 | + this.setMode = this.setMode.bind(this); |
| 65 | + } |
| 66 | + |
| 67 | + componentWillMount() { |
| 68 | + this.setState({ |
| 69 | + mode: this.props.container.locations ? 'location' : 'latlon', |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + setMode(mode) { |
| 74 | + this.setState({mode: mode}); |
| 75 | + this.props.updateContainer( |
| 76 | + mode === 'latlon' |
| 77 | + ? { |
| 78 | + locations: null, |
| 79 | + locationmode: null, |
| 80 | + locationssrc: null, |
| 81 | + locationmodesrc: null, |
| 82 | + } |
| 83 | + : {lat: null, lon: null, latsrc: null, lonsrc: null} |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + render() { |
| 88 | + const {mode} = this.state; |
| 89 | + const { |
| 90 | + localize: _, |
| 91 | + container: {type: type}, |
| 92 | + } = this.context; |
| 93 | + |
| 94 | + return type === 'scattergeo' ? ( |
| 95 | + <Fragment> |
| 96 | + <Field {...this.props} attr={this.props.attr}> |
| 97 | + <Radio |
| 98 | + options={[ |
| 99 | + {value: 'latlon', label: _('Lat/Lon')}, |
| 100 | + {value: 'location', label: _('Location')}, |
| 101 | + ]} |
| 102 | + fullValue={mode} |
| 103 | + updatePlot={this.setMode} |
| 104 | + attr={this.props.attr} |
| 105 | + /> |
| 106 | + </Field> |
| 107 | + {mode === 'latlon' ? ( |
| 108 | + <Fragment> |
| 109 | + <DataSelector label={_('Latitude')} attr="lat" /> |
| 110 | + <DataSelector label={_('Longitude')} attr="lon" /> |
| 111 | + </Fragment> |
| 112 | + ) : ( |
| 113 | + <Location attr="type" /> |
| 114 | + )} |
| 115 | + </Fragment> |
| 116 | + ) : type === 'choropleth' ? ( |
| 117 | + <Location attr="type" /> |
| 118 | + ) : ( |
| 119 | + '' |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +UnconnectedLocationSelector.propTypes = { |
| 125 | + fullValue: PropTypes.any, |
| 126 | + updatePlot: PropTypes.func, |
| 127 | + attr: PropTypes.string, |
| 128 | + ...Field.propTypes, |
| 129 | +}; |
| 130 | + |
| 131 | +UnconnectedLocationSelector.contextTypes = { |
| 132 | + container: PropTypes.object, |
| 133 | + localize: PropTypes.func, |
| 134 | + updateContainer: PropTypes.func, |
| 135 | +}; |
| 136 | + |
| 137 | +export default connectToContainer(UnconnectedLocationSelector); |
0 commit comments