diff --git a/src/editor/widgets/Autocomplete.js b/src/editor/widgets/Autocomplete.js
index a86274d..e7c942a 100644
--- a/src/editor/widgets/Autocomplete.js
+++ b/src/editor/widgets/Autocomplete.js
@@ -5,19 +5,26 @@ const Autocomplete = props => {
const element = useRef();
+ const [ value, setValue ] = useState('');
+
const [ inputItems, setInputItems ] = useState(props.vocabulary);
- useEffect(() =>
- element.current?.querySelector('input').focus(), []);
+ useEffect(() => element.current?.querySelector('input')?.focus(), []);
+ const onSubmit = inputValue => {
+ setValue('');
+ setInputItems([]); // Force-hide the popup
+ if (inputValue.trim().length > 0)
+ props.onSubmit(inputValue);
+ }
+
const onInputValueChange = ({ inputValue }) => {
- props.onChange(inputValue);
-
+ setValue(inputValue);
setInputItems(
props.vocabulary.filter(item =>
item.toLowerCase().startsWith(inputValue.toLowerCase())))
}
-
+
const {
isOpen,
getMenuProps,
@@ -25,19 +32,23 @@ const Autocomplete = props => {
getComboboxProps,
highlightedIndex,
getItemProps,
- } = useCombobox({ items: inputItems, onInputValueChange });
+ setInputValue
+ } = useCombobox({
+ items: inputItems,
+ onInputValueChange,
+ onSelectedItemChange: ({ inputValue }) => {
+ onSubmit(inputValue);
+ setInputValue('');
+ }
+ });
- // TODO onEnter?
const onKeyDown = evt => {
- if (evt.which == 13) {
- if (!isOpen || highlightedIndex == -1) {
- setInputItems([]); // To prevent the popup from showing up afterwards
- props.onKeyDown(evt);
- }
- } else if (evt.which == 40 && props.content.length == 0) {
- // To make options appear on key down
- setInputItems(props.vocabulary);
- }
+ if (evt.which == 13 && highlightedIndex == -1)
+ onSubmit(value);
+ else if (evt.which == 40 && value.length == 0)
+ setInputItems(props.vocabulary); // Show all options on key down
+ else if (evt.which == 27)
+ props.onCancel && props.onCancel();
}
return (
@@ -45,8 +56,9 @@ const Autocomplete = props => {
+ placeholder={props.placeholder}
+ defaultValue={props.initialValue}
+ value={value} />
{isOpen && inputItems.map((item, index) => (
diff --git a/src/editor/widgets/tag/TagWidget.jsx b/src/editor/widgets/tag/TagWidget.jsx
index 2cf372b..f057385 100644
--- a/src/editor/widgets/tag/TagWidget.jsx
+++ b/src/editor/widgets/tag/TagWidget.jsx
@@ -9,7 +9,6 @@ import Autocomplete from '../Autocomplete';
const TagWidget = props => {
const [ showDelete, setShowDelete ] = useState(false);
- const [ newTag, setNewTag ] = useState('');
// Every body with a 'tagging' purpose is considered a tag
const tagBodies = props.annotation ?
@@ -27,11 +26,8 @@ const TagWidget = props => {
props.onRemoveBody(tag);
}
- const onKeyDown = evt => {
- if (evt.which === 13) { // Enter
- props.onAppendBody({ type: 'TextualBody', purpose: 'tagging', value: newTag.trim() });
- setNewTag(''); // Clear the input
- }
+ const onSubmit = tag => {
+ props.onAppendBody({ type: 'TextualBody', purpose: 'tagging', value: tag.trim() });
}
return (
@@ -58,10 +54,8 @@ const TagWidget = props => {
{ !props.readOnly &&
}
diff --git a/src/relations/editor/RelationEditor.jsx b/src/relations/editor/RelationEditor.jsx
index ef39faa..25c99a3 100644
--- a/src/relations/editor/RelationEditor.jsx
+++ b/src/relations/editor/RelationEditor.jsx
@@ -26,11 +26,6 @@ export default class RelationEditor extends Component {
constructor(props) {
super(props);
-
- this.state = {
- content: getContent(props.relation)
- }
-
this.element = React.createRef();
}
@@ -42,11 +37,6 @@ export default class RelationEditor extends Component {
this.setPosition();
}
- componentWillReceiveProps(next) {
- if (this.props.relation !== next.relation)
- this.setState({ content : getContent(next.relation) });
- }
-
setPosition() {
if (this.element.current) {
const el = this.element.current;
@@ -56,33 +46,20 @@ export default class RelationEditor extends Component {
el.style.left = `${midX}px`;
}
}
-
- onChange = value =>
- this.setState({ content: value });
-
- onKeyDown = evt => {
- if (evt.which === 13) { // Enter = Submit
- evt.preventDefault();
- this.onSubmit();
- } else if (evt.which === 27) {
- this.props.onCancel();
- }
- }
- onSubmit = () => {
+ onSubmit = value => {
const updatedAnnotation = this.props.relation.annotation.clone({
motivation: 'linking',
body: [{
type: 'TextualBody',
- value: this.state.content,
+ value,
purpose: 'tagging'
}]
});
const updatedRelation = { ...this.props.relation, annotation: updatedAnnotation };
-
- if (this.state.content) {
+ if (value) {
// Fire create or update event
if (this.props.relation.annotation.bodies.length === 0)
this.props.onRelationCreated(updatedRelation, this.props.relation);
@@ -102,10 +79,10 @@ export default class RelationEditor extends Component {