import PropTypes from 'prop-types' import get from 'lodash.get' import React, {useEffect, useRef, useState} from 'react' import {CSSTransition, TransitionGroup} from 'react-transition-group' import {CopyToClipboard} from 'react-copy-to-clipboard' import useWindowsUtils from '../../utils/WindowsUtils' import {IconTimes} from '../icons' function Popover({ shareUrl, shareSrc, open, onClose, position }) { const [button, setButton] = useState('Copy') const wrapper = useRef(null) const input = useRef(null) const link = useRef(null) const windowsUtils = useWindowsUtils() useEffect(() => { const clickOutside = event => { const children = get(wrapper, 'current') if (children && !children.contains(event.target)) { onClose() } } document.addEventListener('mousedown', clickOutside) if (get(input, 'current')) { get(input, 'current').focus() } return () => { document.removeEventListener('mousedown', clickOutside) } }, [onClose, input]) const onEnter = () => { setButton('复制') } const onCopy = () => { setButton('已复制!') input.current.focus() setTimeout(() => { onClose() }, 500) } const urlToShare = `${windowsUtils.origin}/bootstrap.html/${shareSrc}#!${shareUrl}` return ( <> {open && (

分享你的配置

{ e.preventDefault() onClose() }} className='close' >
{/* eslint-disable-next-line */}
{ event.target.select() }} id='input-share' className={`control-input ${ button === 'Copied!' ? 'padding-lg' : '' }`} readOnly value={urlToShare} ref={input} /> { e.preventDefault() }} className='link' ref={link} > {button}
)}
{open && ( )} ) } Popover.propTypes = { shareUrl: PropTypes.string.isRequired, shareSrc: PropTypes.string.isRequired, open: PropTypes.bool.isRequired, onClose: PropTypes.func.isRequired, position: PropTypes.shape({ x: PropTypes.number.isRequired, y: PropTypes.number.isRequired, }).isRequired, } export default Popover