Skip to content

Commit

Permalink
feat: search-select survey bar
Browse files Browse the repository at this point in the history
  • Loading branch information
zrll12 committed Sep 1, 2024
1 parent 44d7d17 commit 70dda43
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 34 deletions.
133 changes: 133 additions & 0 deletions app/(root)/backstage/judge/components/AsyncPagedSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import {
Code,
Combobox,
Group,
InputBase,
ScrollArea,
Stack,
Text,
Input,
useCombobox,
CloseButton,
} from '@mantine/core';
import { useEffect, useState } from 'react';
import { notifications } from '@mantine/notifications';
import SurveyApi, { SurveyInfo } from '@/api/SurveyApi';

function Options(props: SurveyInfo) {
return (
<Group>
<Code>
{props.id}
</Code>
<Stack gap="xs">
<Text fz="sm" fw={500}>{props.title}</Text>
<Text fz="xs" opacity={0.6}>{props.description}</Text>
</Stack>
</Group>
);
}

export default function AsyncPagedSelect(props: SelectProps) {
const [currentPage, setCurrentPage] = useState(0);
const [surveys, setSurveys] = useState<SurveyInfo[]>([]);
const [fullLoaded, setFullLoaded] = useState(false);
const [search, setSearch] = useState('');

const combobox = useCombobox({
onDropdownClose: () => combobox.resetSelectedOption(),
});

// const shouldFilterOptions = !surveys.some((item: SurveyInfo) => item.id.toString() === value);
const filteredOptions = surveys.filter((item: SurveyInfo) =>
item.title.toLowerCase().includes(search.toLowerCase().trim()));

const options = filteredOptions.map((item: SurveyInfo) => (
<Combobox.Option value={item.id.toString()} key={item.id}>
<Options {...item} />
</Combobox.Option>
));

function loadMore() {
if (fullLoaded) {
notifications.show({
message: '没有更多了',
color: 'red',
});
return;
}

SurveyApi.getList(currentPage, 10, '').then((res) => {
setSurveys([...surveys, ...res]);
setCurrentPage(currentPage + 1);
if (res.length === 0) {
setFullLoaded(true);
}

notifications.show({
message: '获取到一批新的',
color: 'green',
});
});
}

useEffect(() => {
loadMore();
}, []);

return (
<Combobox
onOptionSubmit={(optionValue) => {
props.onChange(optionValue);
combobox.closeDropdown();
}}
store={combobox}
withinPortal={false}
>
<Combobox.Target>
<InputBase
component="button"
type="button"
pointer
rightSection={
props.value !== '' ? (
<CloseButton
size="sm"
onMouseDown={(event) => event.preventDefault()}
onClick={() => props.onChange('')}
aria-label="Clear value"
/>
) : (
<Combobox.Chevron />
)
}
onClick={() => combobox.toggleDropdown()}
rightSectionPointerEvents={props.value === '' ? 'none' : 'all'}
w={300}
>
{props.value || <Input.Placeholder>Pick value</Input.Placeholder>}
</InputBase>
</Combobox.Target>

<Combobox.Dropdown>
<Combobox.Search
value={search}
onChange={(event) => setSearch(event.currentTarget.value)}
placeholder="搜索标题"
/>

<Combobox.Options>
<ScrollArea.Autosize onBottomReached={loadMore}>
{options.length === 0 ?
<Combobox.Empty>没有可选目标</Combobox.Empty> : options}
</ScrollArea.Autosize>
</Combobox.Options>
</Combobox.Dropdown>
</Combobox>
);
}

export interface SelectProps {
value: string;
onChange: (value: string) => void;
}
33 changes: 6 additions & 27 deletions app/(root)/backstage/judge/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Center,
Checkbox,
Group,
Input,
LoadingOverlay,
Pagination,
Space,
Expand All @@ -17,10 +16,11 @@ import {
import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import AnswerApi, { AnswerInfo } from '@/api/AnswerApi';
import AsyncPagedSelect from '@/app/(root)/backstage/judge/components/AsyncPagedSelect';

export default function SurveyPage() {
const [surveySearch, setSurveySearch] = useState<string | null>(null);
const [unconfirmedOnly, setUnconfirmedOnly] = useState(true);
const [surveySearch, setSurveySearch] = useState<string>('');
const [unconfirmedOnly, setUnconfirmedOnly] = useState(false);
const [surveysLoading, setSurveysLoading] = useState(true);
const [page, setPage] = useState(0);
const [maxPage, setMaxPage] = useState(1);
Expand Down Expand Up @@ -57,17 +57,10 @@ export default function SurveyPage() {
<Text>
筛选问卷
</Text>
{/*<Select*/}
{/* limit={10}*/}
{/* data={surveys.map(data => data.title)}*/}
{/* clearable*/}
{/*/>*/}
{/* TODO: Surveys selection */}
<Input
value={surveySearch == null ? '' : surveySearch}
onChange={(e) => setSurveySearch(e.target.value)} />
<AsyncPagedSelect
value={surveySearch}
onChange={setSurveySearch} />
<Checkbox
defaultChecked
onChange={(e) =>
setUnconfirmedOnly(e.currentTarget.checked)}
label="仅显示未确认的问卷"
Expand Down Expand Up @@ -122,20 +115,6 @@ export default function SurveyPage() {
</Box>
</Center>
</Stack>
{/*<Input.Wrapper label="请输入 Answer ID 以跳转">*/}
{/* <Input*/}
{/* onChange={(e) => setAnswerId(parseInt(e.currentTarget.value, 10))}*/}
{/* />*/}
{/*</Input.Wrapper>*/}
{/*<Center>*/}
{/* <Button*/}
{/* onClick={() => {*/}
{/* router.push(`/backstage/judge/${answerId}`);*/}
{/* }}*/}
{/* >*/}
{/* 确认跳转至 {answerId}*/}
{/* </Button>*/}
{/*</Center>*/}
</Stack>
</Center>
</Stack>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"dependencies": {
"@floating-ui/utils": "^0.2.7",
"@mantine/core": "7.12.1",
"@mantine/core": "^7.12.2",
"@mantine/dates": "^7.12.1",
"@mantine/hooks": "7.12.1",
"@mantine/notifications": "^7.12.1",
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1080,9 +1080,9 @@ __metadata:
languageName: node
linkType: hard

"@mantine/core@npm:7.12.1":
version: 7.12.1
resolution: "@mantine/core@npm:7.12.1"
"@mantine/core@npm:^7.12.2":
version: 7.12.2
resolution: "@mantine/core@npm:7.12.2"
dependencies:
"@floating-ui/react": "npm:^0.26.9"
clsx: "npm:^2.1.1"
Expand All @@ -1091,10 +1091,10 @@ __metadata:
react-textarea-autosize: "npm:8.5.3"
type-fest: "npm:^4.12.0"
peerDependencies:
"@mantine/hooks": 7.12.1
"@mantine/hooks": 7.12.2
react: ^18.2.0
react-dom: ^18.2.0
checksum: 10c0/26880601bbce7f5ccac8d72fd367c096e6a5e40a43b4e1718484297e5e9bd7b24472933bf8b8078e275530ebf08c7cd7e98f1fd7358a1b12287f5470829491b1
checksum: 10c0/75643e8e6f33564e98b68259aedbc9ef543832e6466e93686b832587fcd187314d493ff4cfdb143acd3d21d835b4efe1bee1da853702ab9dbd6a9a758c2c1ce8
languageName: node
linkType: hard

Expand Down Expand Up @@ -6269,7 +6269,7 @@ __metadata:
dependencies:
"@babel/core": "npm:^7.24.7"
"@floating-ui/utils": "npm:^0.2.7"
"@mantine/core": "npm:7.12.1"
"@mantine/core": "npm:^7.12.2"
"@mantine/dates": "npm:^7.12.1"
"@mantine/hooks": "npm:7.12.1"
"@mantine/notifications": "npm:^7.12.1"
Expand Down

0 comments on commit 70dda43

Please sign in to comment.