Skip to content

Commit

Permalink
fix: fixed bug when the user couldn't blur from input in the Settings…
Browse files Browse the repository at this point in the history
… Drawer
  • Loading branch information
EduardIvanov22 committed Nov 11, 2021
1 parent 42d5a86 commit 92831fa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/components/molecules/FilePatternList/FilePatternList.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {Button, Input, Tooltip} from 'antd';
import {useEffect, useState} from 'react';
import {useEffect, useRef, useState} from 'react';
import styled from 'styled-components';

import {useOnClickOutside} from '@hooks/useOnClickOutside';

import {useFocus} from '@utils/hooks';

import FilePatternListItem from './FilePatternListItem';
Expand Down Expand Up @@ -29,6 +31,12 @@ const FilePatternList = (props: FilePatternListProps) => {
const [isAddingPattern, setIsAddingPattern] = useState<Boolean>(false);
const [patternInput, setPatternInput] = useState<string>('');
const [inputRef, focusInput] = useFocus<Input>();
const filePatternInputRef = useRef<any>();

useOnClickOutside(filePatternInputRef, () => {
setIsAddingPattern(false);
setPatternInput('');
});

const isPatternUnique = (patternStr: string) => {
return !value.includes(patternStr);
Expand Down Expand Up @@ -86,7 +94,7 @@ const FilePatternList = (props: FilePatternListProps) => {
))}
</StyledUl>
{isAddingPattern ? (
<>
<div ref={filePatternInputRef}>
<Input
ref={inputRef}
defaultValue={patternInput}
Expand All @@ -95,7 +103,7 @@ const FilePatternList = (props: FilePatternListProps) => {
/>
<StyledButton onClick={addPattern}>OK</StyledButton>
<StyledButton onClick={onClickCancel}>Cancel</StyledButton>
</>
</div>
) : (
<Tooltip title={tooltip}>
<Button onClick={() => setIsAddingPattern(true)}>Add Pattern</Button>
Expand Down
11 changes: 10 additions & 1 deletion src/components/organisms/SettingsDrawer/SettingsDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
KustomizeCommandTooltip,
} from '@constants/tooltips';

import {useFocus} from '@utils/hooks';

const StyledDiv = styled.div`
margin-bottom: 20px;
`;
Expand Down Expand Up @@ -68,6 +70,7 @@ const SettingsDrawer = () => {
const [currentFolderReadsMaxDepth, setCurrentFolderReadsMaxDepth] = useState<number>(5);
const [currentKubeConfig, setCurrentKubeConfig] = useState<string>('');
const fileInput = useRef<HTMLInputElement>(null);
const [inputRef, focusInput] = useFocus<Input>();

const isEditingDisabled = uiState.isClusterDiffVisible || isInClusterMode;

Expand Down Expand Up @@ -184,7 +187,13 @@ const SettingsDrawer = () => {
<StyledDiv>
<StyledSpan>KUBECONFIG</StyledSpan>
<Tooltip title={KubeconfigPathTooltip}>
<Input value={currentKubeConfig} onChange={onUpdateKubeconfig} disabled={isEditingDisabled} />
<Input
ref={inputRef}
value={currentKubeConfig}
onChange={onUpdateKubeconfig}
disabled={isEditingDisabled}
onClick={() => focusInput()}
/>
</Tooltip>
<StyledButton onClick={openFileSelect} disabled={isEditingDisabled}>
Browse
Expand Down
25 changes: 25 additions & 0 deletions src/hooks/useOnClickOutside.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {RefObject, useEffect} from 'react';

type AnyEvent = MouseEvent | TouchEvent;

export const useOnClickOutside = (ref: RefObject<any>, touchEndCb: (event: AnyEvent) => void): void => {
useEffect(() => {
const onTouchEndListener = (event: AnyEvent) => {
const el = ref?.current;

if (!el || el.contains(event.target as Node)) {
return;
}

touchEndCb(event);
};

document.addEventListener(`mouseup`, onTouchEndListener);
document.addEventListener(`touchend`, onTouchEndListener);

return () => {
document.removeEventListener(`mouseup`, onTouchEndListener);
document.removeEventListener(`touchend`, onTouchEndListener);
};
}, [ref, touchEndCb]);
};
4 changes: 3 additions & 1 deletion src/utils/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, {useState, useEffect, useRef} from 'react';
import React, {useEffect, useRef, useState} from 'react';

import {Size} from '@models/window';

import {FormInstance} from 'antd/lib/form';

export function useFocus<T>(): [React.RefObject<T>, () => void] {
Expand Down

0 comments on commit 92831fa

Please sign in to comment.