Newer
Older
/* eslint react/prop-types: 0 */
import React, {
useMemo,
useContext,
useState,
useEffect,
useRef,
createRef,
} from 'react';
import { WaxContext, Icon, useOnClickOutside } from 'wax-prosemirror-core';
const DropDownButton = styled.button`
background: #fff;
border: none;
color: #000;
display: flex;
position: relative;
width: 215px;
height: 100%;
span {
position: relative;
top: 12px;
`;
const DropDownMenu = styled.div`
visibility: ${props => (props.isOpen ? 'visible' : 'hidden')};
background: #fff;
display: flex;
flex-direction: column;
border: 1px solid #ddd;
border-radius: 0.25rem;
box-shadow: 0 0.2rem 0.4rem rgb(0 0 0 / 10%);
margin: 2px auto auto;
position: absolute;
width: 220px;
max-height: 150px;
overflow-y: scroll;
z-index: 2;
span:focus {
background: #f2f9fc;
outline: 2px solid #f2f9fc;
const StyledIcon = styled(Icon)`
height: 18px;
width: 18px;
margin-left: auto;
position: relative;
top: 10px;
`;
const DropDownComponent = ({ view, tools }) => {
value: '6',
item: tools[6],
},
{
value: '7',
item: tools[7],
},
const context = useContext(WaxContext);
const {
activeView,
activeViewId,
pmViews: { main },
} = context;
const { state } = view;
const itemRefs = useRef([]);
const wrapperRef = useRef();
const [isOpen, setIsOpen] = useState(false);
useOnClickOutside(wrapperRef, () => setIsOpen(false));
const isEditable = main.props.editable(editable => {
return editable;
});
let isDisabled = !tools[0].select(state, activeView);
useEffect(() => {
if (isDisabled) setIsOpen(false);
}, [isDisabled]);
const openCloseMenu = () => {
if (!isDisabled) setIsOpen(!isOpen);
if (isOpen)
setTimeout(() => {
activeView.focus();
});
};
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const onKeyDown = (e, index) => {
e.preventDefault();
// arrow down
if (e.keyCode === 40) {
if (index === itemRefs.current.length - 1) {
itemRefs.current[0].current.focus();
} else {
itemRefs.current[index + 1].current.focus();
}
}
// arrow up
if (e.keyCode === 38) {
if (index === 0) {
itemRefs.current[itemRefs.current.length - 1].current.focus();
} else {
itemRefs.current[index - 1].current.focus();
}
}
// enter
if (e.keyCode === 13) {
itemRefs.current[index].current.click();
}
// ESC
if (e.keyCode === 27) {
const onChange = option => {
tools[option.value].run(main, context);
openCloseMenu();
};
onKeyDown={e => {
if (e.keyCode === 40) {
itemRefs.current[0].current.focus();
}
setIsOpen(false);
}
if (e.keyCode === 13 || e.keyCode === 32) {
setIsOpen(true);
}}
onMouseDown={openCloseMenu}
type="button"
>
<DropDownMenu
aria-label="Choose a question type"
id="questions-list"
isOpen={isOpen}
role="menu"
>
{dropDownOptions.map((option, index) => {
itemRefs.current[index] = itemRefs.current[index] || createRef();
return (
<span
key={option.value}
onKeyDown={e => onKeyDown(e, index)}
ref={itemRefs.current[index]}
role="menuitem"
tabIndex="-1"
>
{option.label}
</span>
);
})}
</DropDownMenu>