Skip to content

Commit 8ddd27a

Browse files
authored
feat: Introduce <ToggleIconButton/> for single toggles and replace <ToggleButtonGroup/>. (#191)
1 parent ec35a06 commit 8ddd27a

File tree

3 files changed

+91
-66
lines changed

3 files changed

+91
-66
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {
2+
IconButton,
3+
IconButtonProps,
4+
Tooltip,
5+
} from "@mui/joy";
6+
7+
8+
interface ToggleIconButtonProps extends IconButtonProps {
9+
children: React.ReactNode;
10+
isChecked: boolean;
11+
tooltipTitle: string;
12+
}
13+
14+
/**
15+
* An icon button that can visually show icon pressed status.
16+
*
17+
* @param props
18+
* @param props.children
19+
* @param props.isChecked
20+
* @param props.tooltipTitle
21+
* @param props.rest
22+
* @return
23+
*/
24+
const ToggleIconButton = ({
25+
children,
26+
isChecked,
27+
tooltipTitle,
28+
...rest
29+
}: ToggleIconButtonProps) => {
30+
return (
31+
<Tooltip
32+
title={tooltipTitle}
33+
>
34+
<span>
35+
<IconButton
36+
aria-pressed={isChecked}
37+
{...rest}
38+
>
39+
{children}
40+
</IconButton>
41+
</span>
42+
</Tooltip>
43+
);
44+
};
45+
46+
export default ToggleIconButton;

src/components/CentralContainer/Sidebar/SidebarTabs/SearchTabPanel/index.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
}
1818

1919
.query-option-button {
20+
width: 1.5rem !important;
21+
min-width: 0 !important;
22+
height: 1.5rem !important;
23+
min-height: 0 !important;
24+
2025
font-family: Inter, sans-serif !important;
2126
}
2227

src/components/CentralContainer/Sidebar/SidebarTabs/SearchTabPanel/index.tsx

Lines changed: 40 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import React, {
66
import {
77
AccordionGroup,
88
Box,
9-
IconButton,
109
LinearProgress,
10+
Stack,
1111
Textarea,
12-
ToggleButtonGroup,
13-
Tooltip,
1412
} from "@mui/joy";
1513

1614
import UnfoldLessIcon from "@mui/icons-material/UnfoldLess";
@@ -30,33 +28,11 @@ import {isDisabled} from "../../../../../utils/states";
3028
import CustomTabPanel from "../CustomTabPanel";
3129
import PanelTitleButton from "../PanelTitleButton";
3230
import ResultsGroup from "./ResultsGroup";
31+
import ToggleIconButton from "./ToggleIconButton";
3332

3433
import "./index.css";
3534

3635

37-
enum QUERY_OPTION {
38-
IS_CASE_SENSITIVE = "isCaseSensitive",
39-
IS_REGEX = "isRegex",
40-
}
41-
42-
/**
43-
* Determines if the query is case-sensitive based on the provided query options.
44-
*
45-
* @param queryOptions
46-
* @return True if the query is case-sensitive.
47-
*/
48-
const getIsCaseSensitive =
49-
(queryOptions: QUERY_OPTION[]) => queryOptions.includes(QUERY_OPTION.IS_CASE_SENSITIVE);
50-
51-
/**
52-
* Determines if the query is a regular expression based on the provided query options.
53-
*
54-
* @param queryOptions
55-
* @return True if the query is a regular expression.
56-
*/
57-
const getIsRegex =
58-
(queryOptions: QUERY_OPTION[]) => queryOptions.includes(QUERY_OPTION.IS_REGEX);
59-
6036
/**
6137
* Displays a panel for submitting queries and viewing query results.
6238
*
@@ -65,17 +41,18 @@ const getIsRegex =
6541
const SearchTabPanel = () => {
6642
const {queryProgress, queryResults, startQuery, uiState} = useContext(StateContext);
6743
const [isAllExpanded, setIsAllExpanded] = useState<boolean>(true);
68-
const [queryOptions, setQueryOptions] = useState<QUERY_OPTION[]>([]);
6944
const [queryString, setQueryString] = useState<string>("");
45+
const [isCaseSensitive, setIsCaseSensitive] = useState<boolean>(false);
46+
const [isRegex, setIsRegex] = useState<boolean>(false);
7047

7148
const handleCollapseAllButtonClick = () => {
7249
setIsAllExpanded((v) => !v);
7350
};
7451

7552
const handleQuerySubmit = (newArgs: Partial<QueryArgs>) => {
7653
startQuery({
77-
isCaseSensitive: getIsCaseSensitive(queryOptions),
78-
isRegex: getIsRegex(queryOptions),
54+
isCaseSensitive: isCaseSensitive,
55+
isRegex: isRegex,
7956
queryString: queryString,
8057
...newArgs,
8158
});
@@ -86,15 +63,14 @@ const SearchTabPanel = () => {
8663
handleQuerySubmit({queryString: ev.target.value});
8764
};
8865

89-
const handleQueryOptionsChange = (
90-
_: React.MouseEvent<HTMLElement>,
91-
newOptions: QUERY_OPTION[]
92-
) => {
93-
setQueryOptions(newOptions);
94-
handleQuerySubmit({
95-
isCaseSensitive: getIsCaseSensitive(newOptions),
96-
isRegex: getIsRegex(newOptions),
97-
});
66+
const handleCaseSensitivityButtonClick = () => {
67+
handleQuerySubmit({isCaseSensitive: !isCaseSensitive});
68+
setIsCaseSensitive(!isCaseSensitive);
69+
};
70+
71+
const handleRegexButtonClick = () => {
72+
handleQuerySubmit({isRegex: !isRegex});
73+
setIsRegex(!isRegex);
9874
};
9975

10076
const isQueryInputBoxDisabled = isDisabled(uiState, UI_ELEMENT.QUERY_INPUT_BOX);
@@ -124,36 +100,34 @@ const SearchTabPanel = () => {
124100
placeholder={"Search"}
125101
size={"sm"}
126102
endDecorator={
127-
<ToggleButtonGroup
128-
disabled={isQueryInputBoxDisabled}
129-
size={"sm"}
103+
<Stack
104+
direction={"row"}
130105
spacing={0.25}
131-
value={queryOptions}
132-
variant={"plain"}
133-
onChange={handleQueryOptionsChange}
134106
>
135-
<Tooltip title={"Match case"}>
136-
<span>
137-
<IconButton
138-
className={"query-option-button"}
139-
value={QUERY_OPTION.IS_CASE_SENSITIVE}
140-
>
141-
Aa
142-
</IconButton>
143-
</span>
144-
</Tooltip>
145-
146-
<Tooltip title={"Use regular expression"}>
147-
<span>
148-
<IconButton
149-
className={"query-option-button"}
150-
value={QUERY_OPTION.IS_REGEX}
151-
>
152-
.*
153-
</IconButton>
154-
</span>
155-
</Tooltip>
156-
</ToggleButtonGroup>
107+
<ToggleIconButton
108+
className={"query-option-button"}
109+
disabled={isQueryInputBoxDisabled}
110+
isChecked={isCaseSensitive}
111+
size={"sm"}
112+
tooltipTitle={"Match case"}
113+
variant={"plain"}
114+
onClick={handleCaseSensitivityButtonClick}
115+
>
116+
Aa
117+
</ToggleIconButton>
118+
119+
<ToggleIconButton
120+
className={"query-option-button"}
121+
disabled={isQueryInputBoxDisabled}
122+
isChecked={isRegex}
123+
size={"sm"}
124+
tooltipTitle={"Use regular expression"}
125+
variant={"plain"}
126+
onClick={handleRegexButtonClick}
127+
>
128+
.*
129+
</ToggleIconButton>
130+
</Stack>
157131
}
158132
slotProps={{
159133
textarea: {

0 commit comments

Comments
 (0)