|
@@ -1,7 +1,9 @@
|
|
-import React from 'react';
|
|
|
|
|
|
+import React, { useRef } from 'react';
|
|
import { useDebouncedCallback } from 'use-debounce';
|
|
import { useDebouncedCallback } from 'use-debounce';
|
|
import Input from 'components/common/Input/Input';
|
|
import Input from 'components/common/Input/Input';
|
|
import { useSearchParams } from 'react-router-dom';
|
|
import { useSearchParams } from 'react-router-dom';
|
|
|
|
+import CloseIcon from 'components/common/Icons/CloseIcon';
|
|
|
|
+import styled from 'styled-components';
|
|
|
|
|
|
interface SearchProps {
|
|
interface SearchProps {
|
|
placeholder?: string;
|
|
placeholder?: string;
|
|
@@ -10,6 +12,16 @@ interface SearchProps {
|
|
value?: string;
|
|
value?: string;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+const IconButtonWrapper = styled.span.attrs(() => ({
|
|
|
|
+ role: 'button',
|
|
|
|
+ tabIndex: '0',
|
|
|
|
+}))`
|
|
|
|
+ height: 16px !important;
|
|
|
|
+ display: inline-block;
|
|
|
|
+ &:hover {
|
|
|
|
+ cursor: pointer;
|
|
|
|
+ }
|
|
|
|
+`;
|
|
const Search: React.FC<SearchProps> = ({
|
|
const Search: React.FC<SearchProps> = ({
|
|
placeholder = 'Search',
|
|
placeholder = 'Search',
|
|
disabled = false,
|
|
disabled = false,
|
|
@@ -17,7 +29,11 @@ const Search: React.FC<SearchProps> = ({
|
|
onChange,
|
|
onChange,
|
|
}) => {
|
|
}) => {
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
|
|
+ const ref = useRef<HTMLInputElement>(null);
|
|
const handleChange = useDebouncedCallback((e) => {
|
|
const handleChange = useDebouncedCallback((e) => {
|
|
|
|
+ if (ref.current != null) {
|
|
|
|
+ ref.current.value = e.target.value;
|
|
|
|
+ }
|
|
if (onChange) {
|
|
if (onChange) {
|
|
onChange(e.target.value);
|
|
onChange(e.target.value);
|
|
} else {
|
|
} else {
|
|
@@ -28,6 +44,15 @@ const Search: React.FC<SearchProps> = ({
|
|
setSearchParams(searchParams);
|
|
setSearchParams(searchParams);
|
|
}
|
|
}
|
|
}, 500);
|
|
}, 500);
|
|
|
|
+ const clearSearchValue = () => {
|
|
|
|
+ if (searchParams.get('q')) {
|
|
|
|
+ searchParams.set('q', '');
|
|
|
|
+ setSearchParams(searchParams);
|
|
|
|
+ }
|
|
|
|
+ if (ref.current != null) {
|
|
|
|
+ ref.current.value = '';
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
|
|
return (
|
|
return (
|
|
<Input
|
|
<Input
|
|
@@ -37,7 +62,13 @@ const Search: React.FC<SearchProps> = ({
|
|
defaultValue={value || searchParams.get('q') || ''}
|
|
defaultValue={value || searchParams.get('q') || ''}
|
|
inputSize="M"
|
|
inputSize="M"
|
|
disabled={disabled}
|
|
disabled={disabled}
|
|
|
|
+ ref={ref}
|
|
search
|
|
search
|
|
|
|
+ clearIcon={
|
|
|
|
+ <IconButtonWrapper onClick={clearSearchValue}>
|
|
|
|
+ <CloseIcon />
|
|
|
|
+ </IconButtonWrapper>
|
|
|
|
+ }
|
|
/>
|
|
/>
|
|
);
|
|
);
|
|
};
|
|
};
|