|
| 1 | +/* eslint-disable react/forbid-prop-types */ |
| 2 | +/* eslint-disable react/jsx-props-no-spreading */ |
| 3 | +import React, { useState } from 'react'; |
| 4 | +import PropTypes from 'prop-types'; |
| 5 | +import Select, { components } from 'react-select'; |
| 6 | +import { Button } from '@trussworks/react-uswds'; |
| 7 | + |
| 8 | +import 'uswds/dist/css/uswds.css'; |
| 9 | +import '@trussworks/react-uswds/lib/index.css'; |
| 10 | +import './index.css'; |
| 11 | + |
| 12 | +import triangleDown from '../../images/triange_down.png'; |
| 13 | +import check from '../../images/check.svg'; |
| 14 | + |
| 15 | +const DropdownIndicator = (props) => ( |
| 16 | + <components.DropdownIndicator {...props}> |
| 17 | + <img alt="" style={{ width: '22px' }} src={triangleDown} /> |
| 18 | + </components.DropdownIndicator> |
| 19 | +); |
| 20 | + |
| 21 | +const Placeholder = (props) => <components.Placeholder {...props} />; |
| 22 | + |
| 23 | +export const getUserOptions = (regions) => regions.map((region) => ({ value: region, label: `Region ${region}` })); |
| 24 | + |
| 25 | +const styles = { |
| 26 | + container: (provided, state) => { |
| 27 | + // To match the focus indicator provided by uswds |
| 28 | + const outline = state.isFocused ? '0.25rem solid #2491ff;' : ''; |
| 29 | + return { |
| 30 | + ...provided, |
| 31 | + outline, |
| 32 | + }; |
| 33 | + }, |
| 34 | + input: () => ({ display: 'none' }), |
| 35 | + control: (provided) => ({ |
| 36 | + ...provided, |
| 37 | + borderColor: '#0166AB', |
| 38 | + backgroundColor: '#0166AB', |
| 39 | + borderRadius: '5px', |
| 40 | + paddingLeft: '5px', |
| 41 | + paddingTop: '4px', |
| 42 | + paddingBottom: '4px', |
| 43 | + whiteSpace: 'nowrap', |
| 44 | + color: 'white', |
| 45 | + width: '120px', |
| 46 | + }), |
| 47 | + indicatorSeparator: () => ({ display: 'none' }), |
| 48 | + menu: (provided) => ({ |
| 49 | + ...provided, |
| 50 | + width: '200px', |
| 51 | + }), |
| 52 | + option: (provided, state) => ({ |
| 53 | + ...provided, |
| 54 | + color: state.isSelected ? '#0166AB' : 'black', |
| 55 | + fontWeight: state.isSelected ? '700' : 'normal', |
| 56 | + backgroundColor: state.isSelected ? '#F8F8F8' : '#FFFFFF', |
| 57 | + padding: 11, |
| 58 | + }), |
| 59 | + singleValue: (provided) => { |
| 60 | + const single = { color: '#FFFFFF', fontWeight: 600 }; |
| 61 | + |
| 62 | + return { |
| 63 | + ...provided, ...single, |
| 64 | + }; |
| 65 | + }, |
| 66 | + valueContainer: () => ({ padding: '10px 8px' }), |
| 67 | +}; |
| 68 | + |
| 69 | +function RegionalSelect(props) { |
| 70 | + const { |
| 71 | + regions, onApply, |
| 72 | + } = props; |
| 73 | + |
| 74 | + const [selectedItem, setSelectedItem] = useState(); |
| 75 | + const [appliedItem, setAppliedItem] = useState(); |
| 76 | + const [menuIsOpen, setMenuIsOpen] = useState(false); |
| 77 | + |
| 78 | + // const delayedCloseMenu = () => setTimeout(setMenuIsOpen(false), 1000); |
| 79 | + |
| 80 | + const CustomOption = (customOptionProps) => { |
| 81 | + const { |
| 82 | + data, innerRef, innerProps, isSelected, |
| 83 | + } = customOptionProps; |
| 84 | + return data.custom ? ( |
| 85 | + <div ref={innerRef} {...innerProps}> |
| 86 | + <Button |
| 87 | + type="button" |
| 88 | + className="float-left margin-2 smart-hub--filter-button" |
| 89 | + onClick={() => { |
| 90 | + onApply(selectedItem); |
| 91 | + setAppliedItem(selectedItem); |
| 92 | + setMenuIsOpen(false); |
| 93 | + }} |
| 94 | + > |
| 95 | + Apply |
| 96 | + </Button> |
| 97 | + </div> |
| 98 | + ) : ( |
| 99 | + <components.Option {...customOptionProps}> |
| 100 | + {data.label} |
| 101 | + {isSelected && ( |
| 102 | + <img |
| 103 | + className="tta-smarthub--check" |
| 104 | + src={check} |
| 105 | + style={{ |
| 106 | + width: 32, |
| 107 | + float: 'right', |
| 108 | + marginTop: '-9px ', |
| 109 | + }} |
| 110 | + alt={data.label} |
| 111 | + /> |
| 112 | + )} |
| 113 | + </components.Option> |
| 114 | + ); |
| 115 | + }; |
| 116 | + |
| 117 | + CustomOption.propTypes = { |
| 118 | + data: PropTypes.object.isRequired, |
| 119 | + innerRef: PropTypes.string.isRequired, |
| 120 | + innerProps: PropTypes.object.isRequired, |
| 121 | + }; |
| 122 | + |
| 123 | + const options = [...getUserOptions(regions), { custom: true }]; |
| 124 | + return ( |
| 125 | + <Select |
| 126 | + options={options} |
| 127 | + menuIsOpen={menuIsOpen} |
| 128 | + onChange={(value) => { if (value && value.value) setSelectedItem(value); }} |
| 129 | + onMenuOpen={() => setMenuIsOpen(true)} |
| 130 | + onBlur={() => setMenuIsOpen(false)} |
| 131 | + // onBlur={() => delayedCloseMenu()} |
| 132 | + name="RegionalSelect" |
| 133 | + defaultValue={options[0]} |
| 134 | + value={{ |
| 135 | + value: selectedItem ? selectedItem.value : options[0].value, |
| 136 | + label: appliedItem ? appliedItem.label : options[0].label, |
| 137 | + }} |
| 138 | + styles={styles} |
| 139 | + components={{ Placeholder, DropdownIndicator, Option: CustomOption }} |
| 140 | + placeholder="Select Region" |
| 141 | + closeMenuOnSelect={false} |
| 142 | + maxMenuHeight={600} |
| 143 | + /> |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +RegionalSelect.propTypes = { |
| 148 | + regions: PropTypes.arrayOf(PropTypes.number).isRequired, |
| 149 | + onApply: PropTypes.func.isRequired, |
| 150 | +}; |
| 151 | + |
| 152 | +export default RegionalSelect; |
0 commit comments