mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-08 17:03:53 +00:00
d486d78594
* feat: implement progress bar on running event * refactor: use event action hook * refactor: align backend data * style: v2 style tweaks * style: v2 style tweaks + ts <Info> * fix: fix delay increment calls * style: v2 style tweaks + ts <Playback> * style: v2 style tweaks + ts <EventEditor> * style: v2 style tweaks + ts <MessageControl> * fix: naming issue with message control socket endpoint * chore: upgrade style dependencies * style: v2 style tweaks + ts <MenuBar> * style: v2 style tweaks + ts <RundownMenu> * style: v2 style tweaks + ts <Playback> * fix: add selected and next information to EventTimer * style: v2 style tweaks + ts <EventBlock> * style: v2 style tweaks + ts <DelayBlock> * style: v2 style tweaks + ts <BlockBlock> * style: v2 style tweaks + ts <QuickEntry> * refactor: extract <TextInput> logic * chore: upgrade build dependencies * chore: folder structure refactor * fix: issue with dependency path in electron * chore: update version in demo db
89 lines
2.0 KiB
TypeScript
89 lines
2.0 KiB
TypeScript
import { ChangeEvent, useCallback, useEffect, useState } from 'react';
|
|
|
|
interface UseReactiveTextInputReturn {
|
|
value: string;
|
|
onChange: (event: ChangeEvent) => void;
|
|
onBlur: (event: ChangeEvent) => void;
|
|
onKeyDown: (event: KeyboardEvent) => void;
|
|
}
|
|
|
|
export default function useReactiveTextInput(
|
|
initialText: string,
|
|
submitCallback: (newValue: string) => void,
|
|
options?: {
|
|
submitOnEnter?: boolean;
|
|
},
|
|
): UseReactiveTextInputReturn {
|
|
const [text, setText] = useState(initialText);
|
|
|
|
|
|
useEffect(() => {
|
|
if (typeof initialText === 'undefined') {
|
|
setText('');
|
|
} else {
|
|
setText(initialText);
|
|
}
|
|
}, [initialText]);
|
|
|
|
/**
|
|
* @description Handles Input value change
|
|
* @param {string} newValue
|
|
*/
|
|
const handleChange = useCallback(
|
|
(newValue: string) => {
|
|
if (newValue !== text) {
|
|
setText(newValue);
|
|
}
|
|
},
|
|
[text],
|
|
);
|
|
|
|
/**
|
|
* @description Handles submit events
|
|
* @param {string} valueToSubmit
|
|
*/
|
|
const handleSubmit = useCallback(
|
|
(valueToSubmit: string) => {
|
|
// No need to update if it hasn't changed
|
|
if (valueToSubmit === initialText) {
|
|
return;
|
|
}
|
|
const cleanVal = valueToSubmit.trim();
|
|
submitCallback(cleanVal);
|
|
|
|
if (cleanVal !== valueToSubmit) {
|
|
setText(cleanVal);
|
|
}
|
|
},
|
|
[initialText, submitCallback],
|
|
);
|
|
|
|
|
|
/**
|
|
* @description Handles common keys for submit and cancel
|
|
* @param {string} key
|
|
*/
|
|
const keyHandler = useCallback(
|
|
(key: string) => {
|
|
switch (key) {
|
|
case 'Escape':
|
|
setText(initialText);
|
|
break;
|
|
case 'Enter':
|
|
if (options?.submitOnEnter) {
|
|
handleSubmit(text);
|
|
}
|
|
break;
|
|
}
|
|
},
|
|
[initialText, handleSubmit, text],
|
|
);
|
|
|
|
return {
|
|
value: text,
|
|
onChange: (event) => handleChange((event.target as HTMLInputElement).value),
|
|
onBlur: (event) => handleSubmit((event.target as HTMLInputElement).value),
|
|
onKeyDown: (event) => keyHandler(event.key),
|
|
};
|
|
}
|