Skip to content

Commit

Permalink
adjust search box style
Browse files Browse the repository at this point in the history
  • Loading branch information
iamping committed Dec 27, 2024
1 parent 749bb53 commit 7275def
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
23 changes: 12 additions & 11 deletions src/components/app/search-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { PiMagnifyingGlassBold, PiArrowCounterClockwiseDuotone, PiXDuotone } fro
import { InputGroup } from '../ui/input-group';
import { useDebounceCallback, useEventListener, useMediaQuery, useOnClickOutside } from 'usehooks-ts';
import { mobileMediaQuery } from '../../utils/constant';
import { useAtomValue, useSetAtom } from 'jotai';
import { fuzzyListAtom, preFilteredListAtom } from '../../state/atom';
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
import { fuzzyListAtom, preFilteredListAtom, searchBoxOpenAtom } from '../../state/atom';
import { Stock } from '../../models/stock';
import fuzzysort from 'fuzzysort';

export const SearchBox = () => {
// console.log('SearchBox');
const [open, setOpen] = useState(false);
const [open, setOpen] = useAtom(searchBoxOpenAtom);
const [keyword, setKeyword] = useState('');

const preFilteredList = useAtomValue(preFilteredListAtom);
Expand All @@ -21,9 +21,7 @@ export const SearchBox = () => {
const inputRef = useRef<HTMLInputElement>(null);

const isMobile = useMediaQuery(mobileMediaQuery);
const styleForMobile: CSSProperties = isMobile
? { position: 'absolute', left: 78, right: 44, top: 8 }
: { flexGrow: 0, minWidth: 250 };
const styleForMobile: CSSProperties = isMobile ? { flexGrow: 1 } : { flexGrow: 0, minWidth: 250 };

const openSearch = () => {
setOpen((val) => !val);
Expand Down Expand Up @@ -88,6 +86,9 @@ export const SearchBox = () => {
});
}
if (event.key === 'Escape') {
if (keyword.length === 0) {
setOpen(false);
}
setKeyword('');
}
setTimeout(() => {
Expand Down Expand Up @@ -122,9 +123,9 @@ export const SearchBox = () => {

return (
<>
<Show when={!open}>{MagnifyIcon}</Show>
<Show when={open}>
<div ref={divRef}>
<div ref={divRef} style={{ flexGrow: open && isMobile ? 1 : undefined, display: 'flex', alignItems: 'flex-end' }}>
<Show when={!open}>{MagnifyIcon}</Show>
<Show when={open}>
<InputGroup flex="1" style={styleForMobile} endElement={MagnifyIcon} endElementProps={{ padding: 0 }}>
<Input
ref={inputRef}
Expand All @@ -139,8 +140,8 @@ export const SearchBox = () => {
onChange={onInputChange}
/>
</InputGroup>
</div>
</Show>
</Show>
</div>
</>
);
};
19 changes: 11 additions & 8 deletions src/components/app/topbar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Heading, HStack, Separator, Spacer, Text } from '@chakra-ui/react';
import { Heading, HStack, Separator, Show, Spacer, Text } from '@chakra-ui/react';
import { useAtomValue } from 'jotai';
import { rowCountAtom } from '../../state/atom';
import { rowCountAtom, searchBoxOpenAtom } from '../../state/atom';
import { presetOptions, viewOptions } from '../../utils/table.util';
import { Dropdown } from './dropdown';
import { Settings } from './settings';
Expand All @@ -11,6 +11,7 @@ import { mobileMediaQuery } from '../../utils/constant';

export const Topbar = () => {
const rowCount = useAtomValue(rowCountAtom);
const isSearchBoxOpen = useAtomValue(searchBoxOpenAtom);

const isSmallScreen = useMediaQuery(mobileMediaQuery);
const rowCountFormat = useMemo(() => {
Expand Down Expand Up @@ -38,12 +39,14 @@ export const Topbar = () => {
</>
)}
</Heading>
<Separator orientation="vertical" height={5} />
<Dropdown type="Preset" optionList={presetOptions} />
<Separator orientation="vertical" height={5} />
<Dropdown type="View" optionList={viewOptions} />
<Separator orientation="vertical" height={5} />
<Spacer />
<Show when={(!isSearchBoxOpen && isSmallScreen) || !isSmallScreen}>
<Separator orientation="vertical" height={5} />
<Dropdown type="Preset" optionList={presetOptions} />
<Separator orientation="vertical" height={5} />
<Dropdown type="View" optionList={viewOptions} />
<Separator orientation="vertical" height={5} />
<Spacer />
</Show>
<SearchBox />
<Settings />
</HStack>
Expand Down
1 change: 1 addition & 0 deletions src/state/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const dropdownFnAtom = atom<{

export const stockListAtom = atom<Stock[]>([]);
export const fuzzyListAtom = atom<Stock[]>([]);
export const searchBoxOpenAtom = atom(false);

// stock list after exclude (OTC & Biotechnology)
export const preFilteredListAtom = atom((get) => {
Expand Down

0 comments on commit 7275def

Please sign in to comment.