ClosableTextInput.tsx 3.5 KB

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