ClosableTextInput.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 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. };
  38. const onFocusHandler = async(e: React.ChangeEvent<HTMLInputElement>) => {
  39. const inputText = e.target.value;
  40. await createValidation(inputText);
  41. };
  42. const onPressEnter = () => {
  43. if (props.onPressEnter != null) {
  44. const text = inputText != null ? inputText.trim() : null;
  45. if (currentAlertInfo == null) {
  46. props.onPressEnter(text);
  47. }
  48. }
  49. };
  50. const onKeyDownHandler = (e) => {
  51. switch (e.key) {
  52. case 'Enter':
  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 className={props.isShown ? 'd-block' : 'd-none'}>
  90. <input
  91. value={inputText}
  92. ref={inputRef}
  93. type="text"
  94. className="form-control"
  95. placeholder={props.placeholder}
  96. name="input"
  97. onFocus={onFocusHandler}
  98. onChange={onChangeHandler}
  99. onKeyDown={onKeyDownHandler}
  100. onBlur={onBlurHandler}
  101. autoFocus={false}
  102. />
  103. <AlertInfo />
  104. </div>
  105. );
  106. });
  107. export default ClosableTextInput;