Skip to content

Commit

Permalink
feat(components): searchbar -> searchfield
Browse files Browse the repository at this point in the history
  • Loading branch information
purfectliterature committed Jan 17, 2023
1 parent f0cece6 commit ddec2c8
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'types/course/announcements';
import { Operation } from 'types/store';

import SearchBar from 'lib/components/core/fields/SearchBar';
import SearchField from 'lib/components/core/fields/SearchField';
import Pagination from 'lib/components/core/layouts/Pagination';

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

const handleSearchBarChange = (
event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>,
): void => {
if (event.target.value.trim() === '') {
const handleSearchBarChange = (keyword: string): void => {
if (keyword === '') {
setShavedAnnouncements(announcements);
} else {
setShavedAnnouncements(
announcements.filter(
(announcement: AnnouncementMiniEntity) =>
announcement.title
.toLowerCase()
.includes(event.target.value.trim().toLowerCase()) ||
announcement.content
.toLowerCase()
.includes(event.target.value.trim().toLowerCase()),
announcement.title.toLowerCase().includes(keyword.toLowerCase()) ||
announcement.content.toLowerCase().includes(keyword.toLowerCase()),
),
);
}
Expand All @@ -88,12 +82,12 @@ const AnnouncementsDisplay: FC<Props> = (props) => {
xs={1}
>
<div style={{ paddingTop: 7, paddingBottom: 5 }}>
<SearchBar
onChange={handleSearchBarChange}
<SearchField
className="w-[350px]"
onChangeKeyword={handleSearchBarChange}
placeholder={intl.formatMessage(
translations.searchBarPlaceholder,
)}
width={350}
/>
</div>
</Grid>
Expand Down
26 changes: 10 additions & 16 deletions client/app/bundles/course/courses/components/misc/CourseDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineMessages, injectIntl, WrappedComponentProps } from 'react-intl';
import { Grid } from '@mui/material';
import { CourseMiniEntity } from 'types/course/courses';

import SearchBar from 'lib/components/core/fields/SearchBar';
import SearchField from 'lib/components/core/fields/SearchField';
import Pagination from 'lib/components/core/layouts/Pagination';
import Note from 'lib/components/core/Note';

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

const handleSearchBarChange = (
event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>,
): void => {
if (event.target.value === '') {
const handleSearchBarChange = (keyword: string): void => {
if (keyword === '') {
setShavedCourses(courses);
} else {
setShavedCourses(
courses.filter((course: CourseMiniEntity) =>
course.title
.toLowerCase()
.includes(event.target.value.trim().toLowerCase()),
course.title.toLowerCase().includes(keyword.trim().toLowerCase()),
),
);
}
Expand All @@ -64,17 +60,15 @@ const CourseDisplay: FC<Props> = (props) => {
style={{
display: 'flex',
justifyContent: 'left',
paddingTop: 16,
paddingBottom: 16,
}}
xs={1}
>
<div style={{ paddingTop: 16, paddingBottom: 16 }}>
<SearchBar
onChange={handleSearchBarChange}
placeholder={intl.formatMessage(
translations.searchBarPlaceholder,
)}
/>
</div>
<SearchField
onChangeKeyword={handleSearchBarChange}
placeholder={intl.formatMessage(translations.searchBarPlaceholder)}
/>
</Grid>
<Grid item xs={1}>
<Pagination
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import LoadingIndicator from 'lib/components/core/LoadingIndicator';
interface SearchFieldProps {
onChangeKeyword?: (keyword: string) => void;
placeholder?: string;
className?: string;
}

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

return (
<TextField
className={props.className}
fullWidth
hiddenLabel
InputProps={{
Expand Down
48 changes: 0 additions & 48 deletions client/app/lib/components/core/fields/SearchBar.tsx

This file was deleted.

61 changes: 61 additions & 0 deletions client/app/lib/components/core/fields/SearchField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { useState, useTransition } from 'react';
import { Clear, Search } from '@mui/icons-material';
import { IconButton, InputAdornment } from '@mui/material';

import TextField from 'lib/components/core/fields/TextField';
import LoadingIndicator from 'lib/components/core/LoadingIndicator';

interface SearchFieldProps {
onChangeKeyword?: (keyword: string) => void;
placeholder?: string;
className?: string;
}

const SearchField = (props: SearchFieldProps): JSX.Element => {
const [keyword, setKeyword] = useState('');
const [isPending, startTransition] = useTransition();

const changeKeyword = (newKeyword: string): void => {
setKeyword(newKeyword);
startTransition(() => props.onChangeKeyword?.(newKeyword));
};

const clearKeyword = (): void => {
setKeyword('');
props.onChangeKeyword?.('');
};

return (
<TextField
className={props.className}
fullWidth
hiddenLabel
InputProps={{
startAdornment: (
<InputAdornment position="start">
<Search color={keyword ? 'primary' : undefined} />
</InputAdornment>
),
endAdornment: (
<InputAdornment position="end">
{isPending && <LoadingIndicator bare size={20} />}

{keyword && (
<IconButton edge="end" onClick={clearKeyword}>
<Clear />
</IconButton>
)}
</InputAdornment>
),
}}
onChange={(e): void => changeKeyword(e.target.value)}
placeholder={props.placeholder}
size="small"
trims
value={keyword}
variant="filled"
/>
);
};

export default SearchField;

0 comments on commit ddec2c8

Please sign in to comment.