2020-02-03 18:42:31 +05:00
|
|
|
import React, { useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react';
|
2019-08-31 04:07:25 +05:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import TextareaAutosize from 'react-textarea-autosize';
|
|
|
|
import { Button, Form, TextArea } from 'semantic-ui-react';
|
|
|
|
|
|
|
|
import { useClosableForm, useField } from '../../hooks';
|
|
|
|
|
2020-05-29 19:31:19 +05:00
|
|
|
import styles from './EditDescription.module.scss';
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
const EditDescription = React.forwardRef(({ children, defaultValue, onUpdate }, ref) => {
|
|
|
|
const [t] = useTranslation();
|
|
|
|
const [isOpened, setIsOpened] = useState(false);
|
|
|
|
const [value, handleFieldChange, setValue] = useField(null);
|
|
|
|
|
|
|
|
const field = useRef(null);
|
|
|
|
|
|
|
|
const open = useCallback(() => {
|
|
|
|
setIsOpened(true);
|
|
|
|
setValue(defaultValue || '');
|
|
|
|
}, [defaultValue, setValue]);
|
|
|
|
|
|
|
|
const close = useCallback(() => {
|
|
|
|
const cleanValue = value.trim() || null;
|
|
|
|
|
|
|
|
if (cleanValue !== defaultValue) {
|
|
|
|
onUpdate(cleanValue);
|
|
|
|
}
|
|
|
|
|
2020-04-30 05:27:46 +05:00
|
|
|
setIsOpened(false);
|
|
|
|
setValue(null);
|
|
|
|
}, [defaultValue, onUpdate, value, setValue]);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
useImperativeHandle(
|
|
|
|
ref,
|
|
|
|
() => ({
|
|
|
|
open,
|
|
|
|
close,
|
|
|
|
}),
|
|
|
|
[open, close],
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleChildrenClick = useCallback(() => {
|
2019-10-03 03:02:46 +05:00
|
|
|
if (!getSelection().toString()) {
|
|
|
|
open();
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
}, [open]);
|
|
|
|
|
|
|
|
const handleFieldKeyDown = useCallback(
|
2020-03-25 00:15:47 +05:00
|
|
|
(event) => {
|
2019-10-03 03:02:46 +05:00
|
|
|
if (event.ctrlKey && event.key === 'Enter') {
|
2020-04-30 05:27:46 +05:00
|
|
|
close();
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
},
|
2020-04-30 05:27:46 +05:00
|
|
|
[close],
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
|
|
|
|
|
|
|
const [handleFieldBlur, handleControlMouseOver, handleControlMouseOut] = useClosableForm(
|
|
|
|
close,
|
2020-05-16 04:09:46 +05:00
|
|
|
isOpened,
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
|
|
|
|
|
|
|
const handleSubmit = useCallback(() => {
|
2020-04-30 05:27:46 +05:00
|
|
|
close();
|
|
|
|
}, [close]);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isOpened) {
|
|
|
|
field.current.ref.current.select();
|
|
|
|
}
|
|
|
|
}, [isOpened]);
|
|
|
|
|
|
|
|
if (!isOpened) {
|
|
|
|
return React.cloneElement(children, {
|
|
|
|
onClick: handleChildrenClick,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form onSubmit={handleSubmit}>
|
|
|
|
<TextArea
|
|
|
|
ref={field}
|
|
|
|
as={TextareaAutosize}
|
|
|
|
value={value}
|
|
|
|
placeholder={t('common.enterDescription')}
|
|
|
|
minRows={3}
|
|
|
|
spellCheck={false}
|
|
|
|
className={styles.field}
|
|
|
|
onKeyDown={handleFieldKeyDown}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
onBlur={handleFieldBlur}
|
|
|
|
/>
|
|
|
|
<div className={styles.controls}>
|
|
|
|
{/* eslint-disable-next-line jsx-a11y/mouse-events-have-key-events */}
|
|
|
|
<Button
|
|
|
|
positive
|
|
|
|
content={t('action.save')}
|
|
|
|
onMouseOver={handleControlMouseOver}
|
|
|
|
onMouseOut={handleControlMouseOut}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
EditDescription.propTypes = {
|
|
|
|
children: PropTypes.element.isRequired,
|
|
|
|
defaultValue: PropTypes.string,
|
|
|
|
onUpdate: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
EditDescription.defaultProps = {
|
|
|
|
defaultValue: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.memo(EditDescription);
|