|
| 1 | +import PropTypes from 'prop-types' |
| 2 | +import React, { useState } from 'react' |
| 3 | + |
| 4 | +import { useClient } from 'cozy-client' |
| 5 | + |
| 6 | +import CustomMenu from './SelectBox/Menu' |
| 7 | +import CustomOption from './SelectBox/Option' |
| 8 | +import CustomSelectContainer from './SelectBox/SelectContainer' |
| 9 | +import useGroupsSelect from './useGroupSelect' |
| 10 | +import ClickAwayListener from '../../ClickAwayListener' |
| 11 | +import SelectBox from '../../SelectBox' |
| 12 | +import { useBreakpoints } from '../../providers/Breakpoints' |
| 13 | + |
| 14 | +const captureEscapeEvent = e => { |
| 15 | + if (e.key === 'Escape') { |
| 16 | + e.stopPropagation() |
| 17 | + e.target.blur() |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +export const GroupsSelect = ({ |
| 22 | + allGroups, |
| 23 | + closeMenuOnSelect, |
| 24 | + value, |
| 25 | + styles, |
| 26 | + isMulti, |
| 27 | + noOptionsMessage, |
| 28 | + withCheckbox, |
| 29 | + components, |
| 30 | + onGroupCreated, |
| 31 | + onChange, |
| 32 | + onGroupCreate, |
| 33 | + onGroupUpdate, |
| 34 | + onGroupDelete, |
| 35 | + menuPosition |
| 36 | +}) => { |
| 37 | + const client = useClient() |
| 38 | + const { isMobile } = useBreakpoints() |
| 39 | + const [{ menuIsOpen, editedGroupId }, setState] = useState({ |
| 40 | + menuIsOpen: false, |
| 41 | + editedGroupId: '' |
| 42 | + }) |
| 43 | + const { createGroup, renameGroup } = useGroupsSelect({ |
| 44 | + allGroups, |
| 45 | + onGroupCreated, |
| 46 | + client, |
| 47 | + onGroupCreate, |
| 48 | + onGroupUpdate |
| 49 | + }) |
| 50 | + |
| 51 | + const toggleMenu = () => { |
| 52 | + setState(prev => ({ ...prev, menuIsOpen: !prev.menuIsOpen })) |
| 53 | + } |
| 54 | + |
| 55 | + const closeMenu = () => { |
| 56 | + setState(prev => ({ ...prev, menuIsOpen: false })) |
| 57 | + } |
| 58 | + |
| 59 | + const setEditedGroupId = id => { |
| 60 | + setState(prev => ({ ...prev, editedGroupId: id })) |
| 61 | + } |
| 62 | + |
| 63 | + const handleChange = props => { |
| 64 | + if (closeMenuOnSelect) { |
| 65 | + closeMenu() |
| 66 | + } |
| 67 | + |
| 68 | + onChange(props) |
| 69 | + } |
| 70 | + |
| 71 | + const handleDelete = group => { |
| 72 | + closeMenu() |
| 73 | + onGroupDelete(group) |
| 74 | + } |
| 75 | + |
| 76 | + const defaultComponents = { |
| 77 | + Menu: CustomMenu, |
| 78 | + Option: CustomOption, |
| 79 | + SelectContainer: CustomSelectContainer |
| 80 | + } |
| 81 | + |
| 82 | + return ( |
| 83 | + <> |
| 84 | + <ClickAwayListener onClickAway={menuIsOpen ? closeMenu : () => {}}> |
| 85 | + <SelectBox |
| 86 | + className={isMobile ? 'u-mb-half' : 'u-mr-half'} |
| 87 | + classNamePrefix="react-select" |
| 88 | + isMulti={isMulti} |
| 89 | + withCheckbox={withCheckbox} |
| 90 | + menuIsOpen={menuIsOpen} |
| 91 | + blurInputOnSelect={true} |
| 92 | + hideSelectedOptions={false} |
| 93 | + isSearchable={false} |
| 94 | + isClearable={false} |
| 95 | + closeMenuOnSelect={closeMenuOnSelect} |
| 96 | + tabSelectsValue={false} |
| 97 | + onKeyDown={captureEscapeEvent} |
| 98 | + noOptionsMessage={noOptionsMessage} |
| 99 | + options={allGroups} |
| 100 | + value={value} |
| 101 | + onChange={handleChange} |
| 102 | + getOptionLabel={group => group.name} |
| 103 | + getOptionValue={group => group._id} |
| 104 | + components={{ ...defaultComponents, ...components }} |
| 105 | + createGroup={createGroup} |
| 106 | + deleteGroup={handleDelete} |
| 107 | + renameGroup={renameGroup} |
| 108 | + styles={styles} |
| 109 | + onControlClicked={toggleMenu} |
| 110 | + setEditedGroupId={setEditedGroupId} |
| 111 | + editedGroupId={editedGroupId} |
| 112 | + menuPosition={menuPosition} |
| 113 | + fullwidth |
| 114 | + /> |
| 115 | + </ClickAwayListener> |
| 116 | + </> |
| 117 | + ) |
| 118 | +} |
| 119 | + |
| 120 | +GroupsSelect.propTypes = { |
| 121 | + allGroups: PropTypes.array.isRequired, |
| 122 | + styles: PropTypes.object, |
| 123 | + // for multiple selections, value can be an array |
| 124 | + value: PropTypes.oneOfType([PropTypes.array, PropTypes.object]).isRequired, |
| 125 | + // to customize react-select elements |
| 126 | + components: PropTypes.object, |
| 127 | + // to define if it is possible to select more than one option |
| 128 | + isMulti: PropTypes.bool, |
| 129 | + // noOptionsMessage is used to show a message when there is no options in the menu list |
| 130 | + noOptionsMessage: PropTypes.func, |
| 131 | + // hide/show checkbox besides menu list options |
| 132 | + withCheckbox: PropTypes.bool, |
| 133 | + onChange: PropTypes.func.isRequired, |
| 134 | + // function to be triggered after creating a group |
| 135 | + onGroupCreated: PropTypes.func, |
| 136 | + // function to be triggered when creating a group |
| 137 | + onGroupCreate: PropTypes.func, |
| 138 | + // function to be triggered when updating a group |
| 139 | + onGroupUpdate: PropTypes.func, |
| 140 | + // function to be triggered when deleting a group |
| 141 | + onGroupDelete: PropTypes.func, |
| 142 | + closeMenuOnSelect: PropTypes.bool, |
| 143 | + menuPosition: PropTypes.oneOf(['fixed', 'absolute']) |
| 144 | +} |
| 145 | + |
| 146 | +GroupsSelect.defaultProps = { |
| 147 | + isMulti: false, |
| 148 | + components: {}, |
| 149 | + closeMenuOnSelect: false |
| 150 | +} |
| 151 | + |
| 152 | +GroupsSelect.propTypes = { |
| 153 | + allGroups: PropTypes.array.isRequired, |
| 154 | + onGroupCreate: PropTypes.func.isRequired, |
| 155 | + onGroupUpdate: PropTypes.func.isRequired, |
| 156 | + onGroupDelete: PropTypes.func.isRequired |
| 157 | +} |
| 158 | + |
| 159 | +export default GroupsSelect |
0 commit comments