Skip to content
Snippets Groups Projects
Commit 16f3502a authored by chris's avatar chris
Browse files

delete option from dropdown

parent 1ca4628d
No related branches found
No related tags found
No related merge requests found
...@@ -68,6 +68,8 @@ const StyledIcon = styled(Icon)` ...@@ -68,6 +68,8 @@ const StyledIcon = styled(Icon)`
const DropComponent = ({ getPos, node, view, uniqueId }) => { const DropComponent = ({ getPos, node, view, uniqueId }) => {
const [selectedOption, setSelectedOption] = useState(node.attrs.correct); const [selectedOption, setSelectedOption] = useState(node.attrs.correct);
const [allOptions, setAllOptions] = useState(node.attrs.options);
const itemRefs = useRef([]); const itemRefs = useRef([]);
const wrapperRef = useRef(); const wrapperRef = useRef();
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
...@@ -81,8 +83,9 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => { ...@@ -81,8 +83,9 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => {
const isEditable = main.props.editable(editable => { const isEditable = main.props.editable(editable => {
return editable; return editable;
}); });
let isDisabled = !isEditable; let isDisabled = !isEditable;
if (node.attrs.options.length === 0) isDisabled = true; if (allOptions.length === 0) isDisabled = true;
const onChange = option => { const onChange = option => {
const allNodes = getNodes(main); const allNodes = getNodes(main);
...@@ -104,6 +107,12 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => { ...@@ -104,6 +107,12 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => {
useOnClickOutside(wrapperRef, () => setIsOpen(false)); useOnClickOutside(wrapperRef, () => setIsOpen(false));
useEffect(() => {
const theNode = getMatchingNode(main, node);
setAllOptions(theNode.attrs.options);
setSelectedOption(theNode.attrs.correct);
}, [getMatchingNode(main, node).attrs.options]);
useEffect(() => { useEffect(() => {
if (isDisabled) setIsOpen(false); if (isDisabled) setIsOpen(false);
}, [isDisabled]); }, [isDisabled]);
...@@ -153,7 +162,7 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => { ...@@ -153,7 +162,7 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => {
const MultipleDropDown = useMemo(() => { const MultipleDropDown = useMemo(() => {
let selectedValue; let selectedValue;
if (selectedOption) { if (selectedOption) {
selectedValue = node.attrs.options.filter(option => { selectedValue = allOptions.filter(option => {
return option.value === selectedOption; return option.value === selectedOption;
}); });
} }
...@@ -182,7 +191,7 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => { ...@@ -182,7 +191,7 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => {
> >
{selectedOption === null || !selectedOption {selectedOption === null || !selectedOption
? 'Select Option' ? 'Select Option'
: selectedValue[0].label} : selectedValue[0]?.label}
<StyledIcon name="expand" /> <StyledIcon name="expand" />
</DropDownButton> </DropDownButton>
<DropDownMenu <DropDownMenu
...@@ -191,7 +200,7 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => { ...@@ -191,7 +200,7 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => {
isOpen={isOpen} isOpen={isOpen}
role="listbox" role="listbox"
> >
{node.attrs.options.map((option, index) => { {allOptions.map((option, index) => {
itemRefs.current[index] = itemRefs.current[index] || createRef(); itemRefs.current[index] = itemRefs.current[index] || createRef();
return ( return (
<span <span
...@@ -210,7 +219,7 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => { ...@@ -210,7 +219,7 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => {
</DropDownMenu> </DropDownMenu>
</Wrapper> </Wrapper>
); );
}, [node.attrs.options, selectedOption, isOpen, isDisabled]); }, [allOptions, selectedOption, isOpen, isDisabled]);
return MultipleDropDown; return MultipleDropDown;
}; };
...@@ -218,5 +227,26 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => { ...@@ -218,5 +227,26 @@ const DropComponent = ({ getPos, node, view, uniqueId }) => {
export default DropComponent; export default DropComponent;
const getNodes = view => { const getNodes = view => {
return DocumentHelpers.findInlineNodes(view.state.doc); const allNodes = DocumentHelpers.findInlineNodes(view.state.doc);
const matchingOptionNodes = [];
allNodes.forEach(node => {
if (node.node.type.name === 'matching_option') {
matchingOptionNodes.push(node);
}
});
return matchingOptionNodes;
};
const getMatchingNode = (view, node) => {
const allNodes = DocumentHelpers.findInlineNodes(view.state.doc);
let matchingNode = '';
allNodes.forEach(singleNode => {
if (
singleNode.node.type.name === 'matching_option' &&
singleNode.node.attrs.id === node.attrs.id
) {
matchingNode = singleNode.node;
}
});
return matchingNode;
}; };
...@@ -220,18 +220,35 @@ export default ({ node, view, getPos }) => { ...@@ -220,18 +220,35 @@ export default ({ node, view, getPos }) => {
setTimeout(() => { setTimeout(() => {
setAddingOption(false); setAddingOption(false);
}); });
const allNodes = getNodes(context.pmViews.main); const allNodes = getNodes(context.pmViews.main);
// const allNodesOptions = getOptionsNodes(context.pmViews.main);
// console.log(allNodesOptions);
allNodes.forEach(singleNode => { allNodes.forEach(singleNode => {
if (singleNode.node.attrs.id === node.attrs.id) { if (singleNode.node.attrs.id === node.attrs.id) {
singleNode.node.content.content.forEach(parentNodes => { singleNode.node.content.content.forEach(parentNodes => {
parentNodes.forEach(optionNode => { parentNodes.forEach(optionNode => {
if (optionNode.type.name === 'matching_option') { if (optionNode.type.name === 'matching_option') {
// setTimeout(() => {
// context.pmViews.main.dispatch(
// context.pmViews.main.state.tr
// .setMeta('addToHistory', false)
// .setNodeMarkup(allNodesOptions[0].pos, undefined, {
// ...allNodesOptions[0].node.attrs,
// options: options.filter(option => option.value !== value),
// correct: '',
// }),
// );
// console.log(allNodesOptions);
// });
/* eslint-disable-next-line no-param-reassign */ /* eslint-disable-next-line no-param-reassign */
optionNode.attrs.options = options.filter( optionNode.attrs.options = options.filter(
option => option.value !== value, option => option.value !== value,
); );
if (optionNode.attrs.correct === value) { if (optionNode.attrs.correct === value) {
optionNode.attrs.correct = ''; optionNode.attrs.correct = null;
} }
} }
}); });
...@@ -393,3 +410,14 @@ const getNodes = view => { ...@@ -393,3 +410,14 @@ const getNodes = view => {
}); });
return matchingContainerNodes; return matchingContainerNodes;
}; };
const getOptionsNodes = view => {
const allNodes = DocumentHelpers.findInlineNodes(view.state.doc);
const matchingOptionNodes = [];
allNodes.forEach(node => {
if (node.node.type.name === 'matching_option') {
matchingOptionNodes.push(node);
}
});
return matchingOptionNodes;
};
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment