ClosableTextInput.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import React, {
  2. FC, memo, useEffect, useRef, useState,
  3. } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. export const AlertType = {
  6. WARNING: 'warning',
  7. ERROR: 'error',
  8. } as const;
  9. export type AlertType = typeof AlertType[keyof typeof AlertType];
  10. export type AlertInfo = {
  11. type?: AlertType
  12. message?: string
  13. }
  14. type ClosableTextInputProps = {
  15. value?: string
  16. placeholder?: string
  17. inputValidator?(text: string): AlertInfo | Promise<AlertInfo> | null
  18. onPressEnter?(inputText: string | null): void
  19. onClickOutside?(): void
  20. }
  21. const ClosableTextInput: FC<ClosableTextInputProps> = memo((props: ClosableTextInputProps) => {
  22. const { t } = useTranslation();
  23. const inputRef = useRef<HTMLInputElement>(null);
  24. const [inputText, setInputText] = useState(props.value);
  25. const [currentAlertInfo, setAlertInfo] = useState<AlertInfo | null>(null);
  26. const [isAbleToShowAlert, setIsAbleToShowAlert] = useState<boolean>(false);
  27. const createValidation = async(inputText: string) => {
  28. if (props.inputValidator != null) {
  29. const alertInfo = await props.inputValidator(inputText);
  30. setAlertInfo(alertInfo);
  31. }
  32. };
  33. const onChangeHandler = async(e: React.ChangeEvent<HTMLInputElement>) => {
  34. const inputText = e.target.value;
  35. createValidation(inputText);
  36. setInputText(inputText);
  37. setIsAbleToShowAlert(true);
  38. };
  39. const onFocusHandler = async(e: React.ChangeEvent<HTMLInputElement>) => {
  40. const inputText = e.target.value;
  41. await createValidation(inputText);
  42. };
  43. const onPressEnter = () => {
  44. if (props.onPressEnter != null) {
  45. const text = inputText != null ? inputText.trim() : null;
  46. if (currentAlertInfo == null) {
  47. props.onPressEnter(text);
  48. }
  49. }
  50. };
  51. const onKeyDownHandler = (e) => {
  52. switch (e.key) {
  53. case 'Enter':
  54. onPressEnter();
  55. break;
  56. default:
  57. break;
  58. }
  59. };
  60. /*
  61. * Hide when click outside the ref
  62. */
  63. const onBlurHandler = () => {
  64. if (props.onClickOutside == null) {
  65. return;
  66. }
  67. props.onClickOutside();
  68. };
  69. // didMount
  70. useEffect(() => {
  71. // autoFocus
  72. if (inputRef?.current == null) {
  73. return;
  74. }
  75. inputRef.current.focus();
  76. });
  77. const AlertInfo = () => {
  78. if (currentAlertInfo == null) {
  79. return <></>;
  80. }
  81. const alertType = currentAlertInfo.type != null ? currentAlertInfo.type : AlertType.ERROR;
  82. const alertMessage = currentAlertInfo.message != null ? currentAlertInfo.message : 'Invalid value';
  83. const alertTextStyle = alertType === AlertType.ERROR ? 'text-danger' : 'text-warning';
  84. const translation = alertType === AlertType.ERROR ? 'Error' : 'Warning';
  85. return (
  86. <p className={`${alertTextStyle} text-center mt-1`}>{t(translation)}: {alertMessage}</p>
  87. );
  88. };
  89. return (
  90. <div className="d-block flex-fill">
  91. <input
  92. value={inputText || ''}
  93. ref={inputRef}
  94. type="text"
  95. className="form-control"
  96. placeholder={props.placeholder}
  97. name="input"
  98. onFocus={onFocusHandler}
  99. onChange={onChangeHandler}
  100. onKeyDown={onKeyDownHandler}
  101. onBlur={onBlurHandler}
  102. autoFocus={false}
  103. />
  104. {isAbleToShowAlert && <AlertInfo />}
  105. </div>
  106. );
  107. });
  108. export default ClosableTextInput;