mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-15 04:13:47 +00:00
6a6f68b2f9
* feat/89-block-improvements style: replace reload icon * feat/block-feat/block-improvements add cursor buttons * feat/block-improvements hover to create * feat/block-improvements change settings in modal * feat/block-improvements version bump
42 lines
1.2 KiB
React
42 lines
1.2 KiB
React
import { Editable, EditableInput, EditablePreview } from '@chakra-ui/editable';
|
|
import { useEffect, useState } from 'react';
|
|
import style from './EditableText.module.scss';
|
|
|
|
export default function EditableText(props) {
|
|
const { label, defaultValue, placeholder, submitHandler, ...rest } = props;
|
|
const [text, setText] = useState(defaultValue || '');
|
|
const maxchar = props.maxchar || 40;
|
|
|
|
useEffect(() => {
|
|
if (defaultValue == null) setText('');
|
|
else setText(defaultValue);
|
|
}, [defaultValue]);
|
|
|
|
const handleSubmit = (submitedVal) => {
|
|
// No need to update if it hasnt changed
|
|
if (submitedVal === defaultValue) return;
|
|
submitHandler(submitedVal);
|
|
};
|
|
|
|
const handleChange = (val) => {
|
|
if (val.length < maxchar) setText(val);
|
|
};
|
|
|
|
return (
|
|
<div className={style.block}>
|
|
<span className={style.title}>{label}</span>
|
|
<Editable
|
|
onChange={(v) => handleChange(v)}
|
|
onSubmit={(v) => handleSubmit(v)}
|
|
value={text}
|
|
placeholder={placeholder}
|
|
className={style.inline}
|
|
{...rest}
|
|
>
|
|
<EditablePreview color={text === '' ? '#666' : 'inherit'} maxWidth='75%' />
|
|
<EditableInput overflowX='hidden' maxWidth='75%' />
|
|
</Editable>
|
|
</div>
|
|
);
|
|
}
|