Newer
Older
import React, { useContext, useEffect, useRef, useState } from 'react';
import styled from 'styled-components';
import FeedbackComponent from './FeedbackComponent';
const MatchingContainer = styled.div`
border: 3px solid #f5f5f7;
margin-bottom: 30px;
padding: 10px;
`;
const QuestionWrapper = styled.div`
display: flex;
flex-direction: row;
`;
const StyledIconAction = styled(Icon)`
height: 24px;
width: 24px;
const CreateOptions = styled.div`
display: flex;
flex-direction: column;
ul {
display: flex;
flex-wrap: wrap;
flex-direction: row;
margin: 0;
padding: 0;
li {
list-style-type: none;
padding-right: 7px;
padding-bottom: 7px;
span {
background: #535e76;
color: white;
padding: 3px 3px 3px 10px;
border-radius: 12px;
}
buttton {
}
svg {
fill: white;
width: 16px;
height: 16px;
}
}
}
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
input {
border: none;
border-bottom: 1px solid black;
&:focus {
outline: none;
}
::placeholder {
color: rgb(170, 170, 170);
font-style: italic;
}
}
button {
border: 1px solid #535e76;
cursor: pointer;
color: #535e76;
margin-left: 20px;
background: #fff;
padding: 4px 8px 4px 8px;
&:hover {
border: 1px solid #535e76;
cursor: pointer;
color: #535e76;
margin-right: 20px;
background: #fff;
background: #535e76;
color: #fff;
padding: 4px 8px 4px 8px;
}
}
export default ({ node, view, getPos }) => {
const context = useContext(WaxContext);
const {
pmViews: { main },
} = context;
const [options, setOptions] = useState(node.attrs.options);
const [addingOption, setAddingOption] = useState(false);
const customProps = main.props.customValues;
const isEditable = main.props.editable(editable => {
return editable;
});
const readOnly = !isEditable;
const obj = { label: addOptionRef.current.value, key: uuidv4() };
setOptions(prevOptions => [...prevOptions, obj]);
setAddingOption(true);
setTimeout(() => {
setAddingOption(false);
});
useEffect(() => {
const allNodes = getNodes(main);
if (!addingOption) return;
allNodes.forEach(singleNode => {
if (singleNode.node.attrs.id === node.attrs.id) {
main.dispatch(
main.state.tr.setNodeMarkup(getPos(), undefined, {
...singleNode.node.attrs,
options,
}),
);
}
});
}, [options]);
const removeOption = key => {
setOptions(options.filter(option => option.key !== key));
setAddingOption(true);
setTimeout(() => {
setAddingOption(false);
});
return (
<MatchingWrapper>
<span>Matching</span>
<MatchingContainer className="matching">
{options.length > 0 && (
<ul>
<li>Options: </li>
{options.map((option, index) => {
return (
<li key={option.key}>
<span>
{option.label}
<ActionButton
onClick={() => removeOption(option.key)}
type="button"
>
<StyledIconAction name="deleteOutlined" />
</ActionButton>
</span>
</li>
);
})}
</ul>
)}
<input
placeholder="Type an option ..."
ref={addOptionRef}
type="text"
/>
{!(readOnly && !customProps.showFeedBack) && (
<FeedbackComponent
getPos={getPos}
node={node}
readOnly={readOnly}
view={view}
/>
)}
</MatchingContainer>
</MatchingWrapper>
);
};
const getNodes = view => {
const allNodes = DocumentHelpers.findBlockNodes(view.state.doc);
const matchingContainerNodes = [];
allNodes.forEach(node => {
if (node.node.type.name === 'matching_container') {
matchingContainerNodes.push(node);
}
});
return matchingContainerNodes;
};