-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from dsaltares/feat/26
feat: 26 - use autocomplete for categories and accounts in report settings dialog
- Loading branch information
Showing
7 changed files
with
309 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { useCallback, useMemo } from 'react'; | ||
import Checkbox from '@mui/material/Checkbox'; | ||
import FormControl from '@mui/material/FormControl'; | ||
import Stack from '@mui/material/Stack'; | ||
import Autocomplete from '@mui/material/Autocomplete'; | ||
import SelectAllIcon from '@mui/icons-material/SelectAll'; | ||
import DeselectIcon from '@mui/icons-material/Deselect'; | ||
import TextField from '@mui/material/TextField'; | ||
import IconButton from '@mui/material/IconButton'; | ||
import type { Account } from '@server/accounts/types'; | ||
import { isOptionEqualToValue } from '@lib/autoCompleteOptions'; | ||
|
||
type Props = { | ||
accounts: Account[]; | ||
selected: number[]; | ||
onChange: (selected: number[]) => void; | ||
}; | ||
|
||
const id = 'account-autocomplete'; | ||
|
||
export default function AccountAutocomplete({ | ||
accounts, | ||
selected, | ||
onChange, | ||
}: Props) { | ||
const accountsById = useMemo( | ||
() => | ||
accounts.reduce<Record<string, Account>>( | ||
(acc, account) => ({ ...acc, [account.id]: account }), | ||
{}, | ||
), | ||
[accounts], | ||
); | ||
const value = useMemo( | ||
() => | ||
selected.map((id) => { | ||
const category = accountsById[id]; | ||
return { | ||
id: category.id.toString(), | ||
label: category.name, | ||
}; | ||
}), | ||
[selected, accountsById], | ||
); | ||
const accountOptions = useMemo( | ||
() => accounts.map(({ id, name }) => ({ id: id.toString(), label: name })), | ||
[accounts], | ||
); | ||
const allSelected = selected.length === accounts.length; | ||
const selectAll = useCallback( | ||
() => onChange(accounts.map((account) => account.id)), | ||
[accounts, onChange], | ||
); | ||
const deselectAll = useCallback(() => onChange([]), [onChange]); | ||
|
||
return ( | ||
<FormControl fullWidth> | ||
<Stack direction="row" spacing={1} alignItems="center"> | ||
<Autocomplete | ||
multiple | ||
fullWidth | ||
disableCloseOnSelect | ||
id={id} | ||
value={value} | ||
options={accountOptions} | ||
isOptionEqualToValue={isOptionEqualToValue} | ||
getOptionLabel={(option) => option.label} | ||
onChange={(_event, newValue) => | ||
onChange(newValue.map((option) => Number(option.id))) | ||
} | ||
renderOption={(props, option, { selected }) => { | ||
const { key, ...optionProps } = props; | ||
return ( | ||
<li key={key} {...optionProps}> | ||
<Checkbox style={{ marginRight: 8 }} checked={selected} /> | ||
{option.label} | ||
</li> | ||
); | ||
}} | ||
renderInput={(params) => <TextField {...params} label="Accounts" />} | ||
/> | ||
<div> | ||
<IconButton onClick={allSelected ? deselectAll : selectAll}> | ||
{allSelected ? <DeselectIcon /> : <SelectAllIcon />} | ||
</IconButton> | ||
</div> | ||
</Stack> | ||
</FormControl> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { useCallback, useMemo } from 'react'; | ||
import Checkbox from '@mui/material/Checkbox'; | ||
import FormControl from '@mui/material/FormControl'; | ||
import Autocomplete from '@mui/material/Autocomplete'; | ||
import TextField from '@mui/material/TextField'; | ||
import IconButton from '@mui/material/IconButton'; | ||
import SelectAllIcon from '@mui/icons-material/SelectAll'; | ||
import DeselectIcon from '@mui/icons-material/Deselect'; | ||
import Stack from '@mui/material/Stack'; | ||
import { isOptionEqualToValue } from '@lib/autoCompleteOptions'; | ||
import type { Category } from '@server/category/types'; | ||
|
||
type Props = { | ||
categories: Category[]; | ||
selected: number[]; | ||
onChange: (selected: number[]) => void; | ||
}; | ||
|
||
const id = 'category-autocomplete'; | ||
|
||
export default function CategoryAutocomplete({ | ||
categories, | ||
selected, | ||
onChange, | ||
}: Props) { | ||
const categoriesById = useMemo( | ||
() => | ||
categories.reduce<Record<string, Category>>( | ||
(acc, category) => ({ ...acc, [category.id]: category }), | ||
{}, | ||
), | ||
[categories], | ||
); | ||
const value = useMemo( | ||
() => | ||
selected.map((id) => { | ||
const category = categoriesById[id]; | ||
return { | ||
id: category.id.toString(), | ||
label: category.name, | ||
}; | ||
}), | ||
[selected, categoriesById], | ||
); | ||
const categoryOptions = useMemo( | ||
() => | ||
categories.map(({ id, name }) => ({ id: id.toString(), label: name })), | ||
[categories], | ||
); | ||
const allSelected = selected.length === categories.length; | ||
const selectAll = useCallback( | ||
() => onChange(categories.map((category) => category.id)), | ||
[categories, onChange], | ||
); | ||
const deselectAll = useCallback(() => onChange([]), [onChange]); | ||
return ( | ||
<FormControl fullWidth> | ||
<Stack direction="row" spacing={1} alignItems="center"> | ||
<Autocomplete | ||
multiple | ||
fullWidth | ||
disableCloseOnSelect | ||
id={id} | ||
value={value} | ||
options={categoryOptions} | ||
isOptionEqualToValue={isOptionEqualToValue} | ||
getOptionLabel={(option) => option.label} | ||
onChange={(_event, newValue) => | ||
onChange(newValue.map((option) => Number(option.id))) | ||
} | ||
renderOption={(props, option, { selected }) => { | ||
const { key, ...optionProps } = props; | ||
return ( | ||
<li key={key} {...optionProps}> | ||
<Checkbox style={{ marginRight: 8 }} checked={selected} /> | ||
{option.label} | ||
</li> | ||
); | ||
}} | ||
renderInput={(params) => <TextField {...params} label="Categories" />} | ||
/> | ||
<div> | ||
<IconButton onClick={allSelected ? deselectAll : selectAll}> | ||
{allSelected ? <DeselectIcon /> : <SelectAllIcon />} | ||
</IconButton> | ||
</div> | ||
</Stack> | ||
</FormControl> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.