Skip to content

Commit ddec2c8

Browse files
feat(components): searchbar -> searchfield
1 parent f0cece6 commit ddec2c8

File tree

5 files changed

+81
-78
lines changed

5 files changed

+81
-78
lines changed

client/app/bundles/course/announcements/components/misc/AnnouncementsDisplay.tsx

+8-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from 'types/course/announcements';
1010
import { Operation } from 'types/store';
1111

12-
import SearchBar from 'lib/components/core/fields/SearchBar';
12+
import SearchField from 'lib/components/core/fields/SearchField';
1313
import Pagination from 'lib/components/core/layouts/Pagination';
1414

1515
import AnnouncementCard from './AnnouncementCard';
@@ -56,21 +56,15 @@ const AnnouncementsDisplay: FC<Props> = (props) => {
5656
setShavedAnnouncements(announcements);
5757
}, [announcements]);
5858

59-
const handleSearchBarChange = (
60-
event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>,
61-
): void => {
62-
if (event.target.value.trim() === '') {
59+
const handleSearchBarChange = (keyword: string): void => {
60+
if (keyword === '') {
6361
setShavedAnnouncements(announcements);
6462
} else {
6563
setShavedAnnouncements(
6664
announcements.filter(
6765
(announcement: AnnouncementMiniEntity) =>
68-
announcement.title
69-
.toLowerCase()
70-
.includes(event.target.value.trim().toLowerCase()) ||
71-
announcement.content
72-
.toLowerCase()
73-
.includes(event.target.value.trim().toLowerCase()),
66+
announcement.title.toLowerCase().includes(keyword.toLowerCase()) ||
67+
announcement.content.toLowerCase().includes(keyword.toLowerCase()),
7468
),
7569
);
7670
}
@@ -88,12 +82,12 @@ const AnnouncementsDisplay: FC<Props> = (props) => {
8882
xs={1}
8983
>
9084
<div style={{ paddingTop: 7, paddingBottom: 5 }}>
91-
<SearchBar
92-
onChange={handleSearchBarChange}
85+
<SearchField
86+
className="w-[350px]"
87+
onChangeKeyword={handleSearchBarChange}
9388
placeholder={intl.formatMessage(
9489
translations.searchBarPlaceholder,
9590
)}
96-
width={350}
9791
/>
9892
</div>
9993
</Grid>

client/app/bundles/course/courses/components/misc/CourseDisplay.tsx

+10-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { defineMessages, injectIntl, WrappedComponentProps } from 'react-intl';
33
import { Grid } from '@mui/material';
44
import { CourseMiniEntity } from 'types/course/courses';
55

6-
import SearchBar from 'lib/components/core/fields/SearchBar';
6+
import SearchField from 'lib/components/core/fields/SearchField';
77
import Pagination from 'lib/components/core/layouts/Pagination';
88
import Note from 'lib/components/core/Note';
99

@@ -40,17 +40,13 @@ const CourseDisplay: FC<Props> = (props) => {
4040
return <Note message={intl.formatMessage(translations.noCourse)} />;
4141
}
4242

43-
const handleSearchBarChange = (
44-
event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>,
45-
): void => {
46-
if (event.target.value === '') {
43+
const handleSearchBarChange = (keyword: string): void => {
44+
if (keyword === '') {
4745
setShavedCourses(courses);
4846
} else {
4947
setShavedCourses(
5048
courses.filter((course: CourseMiniEntity) =>
51-
course.title
52-
.toLowerCase()
53-
.includes(event.target.value.trim().toLowerCase()),
49+
course.title.toLowerCase().includes(keyword.trim().toLowerCase()),
5450
),
5551
);
5652
}
@@ -64,17 +60,15 @@ const CourseDisplay: FC<Props> = (props) => {
6460
style={{
6561
display: 'flex',
6662
justifyContent: 'left',
63+
paddingTop: 16,
64+
paddingBottom: 16,
6765
}}
6866
xs={1}
6967
>
70-
<div style={{ paddingTop: 16, paddingBottom: 16 }}>
71-
<SearchBar
72-
onChange={handleSearchBarChange}
73-
placeholder={intl.formatMessage(
74-
translations.searchBarPlaceholder,
75-
)}
76-
/>
77-
</div>
68+
<SearchField
69+
onChangeKeyword={handleSearchBarChange}
70+
placeholder={intl.formatMessage(translations.searchBarPlaceholder)}
71+
/>
7872
</Grid>
7973
<Grid item xs={1}>
8074
<Pagination

client/app/bundles/course/reference-timelines/components/SearchField.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import LoadingIndicator from 'lib/components/core/LoadingIndicator';
88
interface SearchFieldProps {
99
onChangeKeyword?: (keyword: string) => void;
1010
placeholder?: string;
11+
className?: string;
1112
}
1213

1314
const SearchField = (props: SearchFieldProps): JSX.Element => {
@@ -26,6 +27,7 @@ const SearchField = (props: SearchFieldProps): JSX.Element => {
2627

2728
return (
2829
<TextField
30+
className={props.className}
2931
fullWidth
3032
hiddenLabel
3133
InputProps={{

client/app/lib/components/core/fields/SearchBar.tsx

-48
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { useState, useTransition } from 'react';
2+
import { Clear, Search } from '@mui/icons-material';
3+
import { IconButton, InputAdornment } from '@mui/material';
4+
5+
import TextField from 'lib/components/core/fields/TextField';
6+
import LoadingIndicator from 'lib/components/core/LoadingIndicator';
7+
8+
interface SearchFieldProps {
9+
onChangeKeyword?: (keyword: string) => void;
10+
placeholder?: string;
11+
className?: string;
12+
}
13+
14+
const SearchField = (props: SearchFieldProps): JSX.Element => {
15+
const [keyword, setKeyword] = useState('');
16+
const [isPending, startTransition] = useTransition();
17+
18+
const changeKeyword = (newKeyword: string): void => {
19+
setKeyword(newKeyword);
20+
startTransition(() => props.onChangeKeyword?.(newKeyword));
21+
};
22+
23+
const clearKeyword = (): void => {
24+
setKeyword('');
25+
props.onChangeKeyword?.('');
26+
};
27+
28+
return (
29+
<TextField
30+
className={props.className}
31+
fullWidth
32+
hiddenLabel
33+
InputProps={{
34+
startAdornment: (
35+
<InputAdornment position="start">
36+
<Search color={keyword ? 'primary' : undefined} />
37+
</InputAdornment>
38+
),
39+
endAdornment: (
40+
<InputAdornment position="end">
41+
{isPending && <LoadingIndicator bare size={20} />}
42+
43+
{keyword && (
44+
<IconButton edge="end" onClick={clearKeyword}>
45+
<Clear />
46+
</IconButton>
47+
)}
48+
</InputAdornment>
49+
),
50+
}}
51+
onChange={(e): void => changeKeyword(e.target.value)}
52+
placeholder={props.placeholder}
53+
size="small"
54+
trims
55+
value={keyword}
56+
variant="filled"
57+
/>
58+
);
59+
};
60+
61+
export default SearchField;

0 commit comments

Comments
 (0)