ClosableTextInput.tsx 3.3 KB

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