|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + Modal, |
| 5 | + ButtonGroup, |
| 6 | + Icon, |
| 7 | + Tabs, |
| 8 | + Tab, |
| 9 | +} from 'patternfly-react'; |
| 10 | +import PropTypes from 'prop-types'; |
| 11 | +import classNames from 'classnames'; |
| 12 | + |
| 13 | +import IconList from './icon-list'; |
| 14 | + |
| 15 | +const FontIconPicker = ({ iconTypes, selected, onChangeURL }) => { |
| 16 | + const [{ |
| 17 | + showModal, |
| 18 | + selectedIcon, |
| 19 | + activeTab, |
| 20 | + activeIcon, |
| 21 | + }, setState] = useState({ |
| 22 | + showModal: false, |
| 23 | + selectedIcon: selected, |
| 24 | + activeIcon: selected, |
| 25 | + activeTab: selected ? selected.split(' ')[0] : Object.keys(iconTypes)[0], |
| 26 | + }); |
| 27 | + |
| 28 | + const onModalApply = () => { |
| 29 | + // This is required to connect the old session-backed form with the component |
| 30 | + window.miqObserveRequest(onChangeURL, { data: { button_icon: activeIcon } }); |
| 31 | + setState(state => ({ ...state, showModal: false, selectedIcon: activeIcon })); |
| 32 | + }; |
| 33 | + |
| 34 | + const hideModal = () => setState(state => ({ ...state, showModal: false })); |
| 35 | + |
| 36 | + return ( |
| 37 | + <div className="fonticon-picker"> |
| 38 | + <ButtonGroup> |
| 39 | + <Button className="icon-picker-btn"> |
| 40 | + { selectedIcon ? (<i id="selected-icon" className={classNames('fa-lg', selectedIcon)} />) : __('No icon') } |
| 41 | + </Button> |
| 42 | + <Button onClick={() => setState(state => ({ ...state, showModal: true }))}><Icon type="fa" name="angle-down" /></Button> |
| 43 | + </ButtonGroup> |
| 44 | + <Modal show={showModal} onHide={hideModal} bsSize="large"> |
| 45 | + <Modal.Header> |
| 46 | + <button |
| 47 | + id="close-icon-picker-modal" |
| 48 | + type="button" |
| 49 | + className="close" |
| 50 | + onClick={hideModal} |
| 51 | + aria-label={__('Close')} |
| 52 | + > |
| 53 | + <Icon type="pf" name="close" /> |
| 54 | + </button> |
| 55 | + <Modal.Title>{ __('Select an icon') }</Modal.Title> |
| 56 | + </Modal.Header> |
| 57 | + <Modal.Body> |
| 58 | + <div className="fonticon-picker-modal"> |
| 59 | + <Tabs id="font-icon-tabs" activeKey={activeTab} animation={false} onSelect={activeTab => setState(state => ({ ...state, activeTab }))}> |
| 60 | + { Object.keys(iconTypes).map(type => ( |
| 61 | + <Tab eventKey={type} key={type} title={iconTypes[type]}> |
| 62 | + <IconList {...{ |
| 63 | + type, |
| 64 | + activeIcon, |
| 65 | + activeTab, |
| 66 | + setState, |
| 67 | + }} /> |
| 68 | + </Tab> |
| 69 | + )) } |
| 70 | + </Tabs> |
| 71 | + </div> |
| 72 | + </Modal.Body> |
| 73 | + <Modal.Footer> |
| 74 | + <Button |
| 75 | + id="apply-icon-picker-icon" |
| 76 | + bsStyle="primary" |
| 77 | + onClick={onModalApply} |
| 78 | + disabled={selectedIcon === activeIcon || activeIcon === undefined} |
| 79 | + > |
| 80 | + { __('Apply') } |
| 81 | + </Button> |
| 82 | + <Button id="cancel-icon-picker-modal" bsStyle="default" className="btn-cancel" onClick={hideModal}> |
| 83 | + { __('Cancel') } |
| 84 | + </Button> |
| 85 | + </Modal.Footer> |
| 86 | + </Modal> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +FontIconPicker.propTypes = { |
| 92 | + iconTypes: PropTypes.any, |
| 93 | + selected: PropTypes.string, |
| 94 | + onChangeURL: PropTypes.string.isRequired, |
| 95 | +}; |
| 96 | + |
| 97 | +FontIconPicker.defaultProps = { |
| 98 | + selected: undefined, |
| 99 | + iconTypes: { |
| 100 | + ff: 'Font Fabulous', |
| 101 | + pficon: 'PatternFly', |
| 102 | + fa: 'Font Awesome', |
| 103 | + }, |
| 104 | +}; |
| 105 | + |
| 106 | +export default FontIconPicker; |
0 commit comments