Skip to content

Commit

Permalink
Allow to clear input
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed May 27, 2024
1 parent 7991d3d commit 30b7c0c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 69 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
ignorePatterns: ['*.test.ts', '*.spec.tsx','zustand.ts'],
ignorePatterns: ['*.test.ts', '*.spec.tsx', 'zustand.ts'],
settings: {
'import/resolver': {
node: {
Expand Down Expand Up @@ -28,6 +28,7 @@ module.exports = {
{ unnamedComponents: 'arrow-function', namedComponents: 'arrow-function' },
],
'array-callback-return': ['error', { checkForEach: true }],
'react/no-unstable-nested-components': 'warn',
'react/no-unstable-nested-components': ['warn', { allowAsProps: true }],
'react/display-name': 'off',
},
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@types/react-dom": "^18.3.0",
"@types/recharts": "1.8.29",
"@types/node": "^20.12.12",
"@typescript-eslint/eslint-plugin": "^7.10.0",
"@typescript-eslint/eslint-plugin": "^7.11.0",
"autoprefixer": "^10.4.19",
"eslint": "^8.57.0",
"eslint-config-unosquare": "0.13.0",
Expand All @@ -41,7 +41,7 @@
"husky": "^9.0.11",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"lint-staged": "^15.2.4",
"lint-staged": "^15.2.5",
"postcss-loader": "^8.1.1",
"prettier": "^3.2.5",
"tailwindcss": "^3.4.3",
Expand Down
102 changes: 48 additions & 54 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions src/SearchBox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Search12Regular } from '@fluentui/react-icons';
import { Dismiss24Regular, Search24Regular } from '@fluentui/react-icons';
import { TextInput } from '@tremor/react';
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useMemo, useRef } from 'react';
import { useDebounce } from '../hooks';

export type SearchBoxSettings = {
Expand All @@ -11,6 +11,9 @@ export type SearchBoxSettings = {
initialValue?: string;
};

export const SearchOrClearButton = ({ hasValue = false, onClick }: { hasValue: boolean; onClick: () => void }) =>
hasValue ? <Dismiss24Regular className='cursor-pointer' onClick={() => onClick()} /> : <Search24Regular />;

export const SearchBox = ({
search,
placeholder = 'Search',
Expand All @@ -26,15 +29,22 @@ export const SearchBox = ({
if (focus && ref.current) setTimeout(() => ref.current?.focus(), 2000);
}, [focus]);

const onChange = ({ target }: React.ChangeEvent<HTMLInputElement>) => {
setValue(target.value);
const setSearchValue = (x = '') => {
setValue(x);
debounceCall();
};

const onChange = ({ target }: React.ChangeEvent<HTMLInputElement>) => setSearchValue(target.value);

const icon = useMemo(
() => () => <SearchOrClearButton hasValue={value.length > 0} onClick={setSearchValue} />,
[value],
);

return (
<TextInput
className='max-w-[220px]'
icon={Search12Regular}
className='max-w-[220px] [&_input]:px-[10px] [&_svg]:ml-2'
icon={icon}
ref={ref}
value={value}
onChange={onChange}
Expand Down
Loading

0 comments on commit 30b7c0c

Please sign in to comment.