-
-
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.
♻️ refactor Nordigen and Category autocomplete usage (#784)
The final `Autocomplete` refactors. After this is merged what's remaining is to do extensive testing and address the bugs in actualbudget/actual#773 This PR moves `Nordigen` autocomplete to the new one without using a feature flag. IMO this is a safe change given the simple nature of the Nordigen autocomplete component.
- Loading branch information
1 parent
c554f6f
commit 237c994
Showing
12 changed files
with
238 additions
and
135 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
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
91 changes: 91 additions & 0 deletions
91
packages/loot-design/src/components/CategoryAutocomplete.js
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,91 @@ | ||
import React, { useMemo } from 'react'; | ||
import { components as SelectComponents } from 'react-select'; | ||
|
||
import { colors } from '../style'; | ||
import Split from '../svg/v0/Split'; | ||
|
||
import { View } from './common'; | ||
import Autocomplete from './NewAutocomplete'; | ||
|
||
const SPLIT_TRANSACTION_KEY = 'split'; | ||
|
||
export default function CategoryAutocomplete({ | ||
value, | ||
categoryGroups, | ||
showSplitOption = false, | ||
multi = false, | ||
onSplit, | ||
...props | ||
}) { | ||
const options = useMemo(() => { | ||
const suggestions = categoryGroups.map(group => ({ | ||
label: group.name, | ||
options: group.categories.map(categ => ({ | ||
value: categ.id, | ||
label: categ.name, | ||
})), | ||
})); | ||
|
||
if (showSplitOption) { | ||
suggestions.unshift({ | ||
value: SPLIT_TRANSACTION_KEY, | ||
label: SPLIT_TRANSACTION_KEY, | ||
}); | ||
} | ||
|
||
return suggestions; | ||
}, [categoryGroups, showSplitOption]); | ||
|
||
const allOptions = useMemo( | ||
() => | ||
options.reduce( | ||
(carry, { options }) => [...carry, ...(options || [])], | ||
[], | ||
), | ||
[options], | ||
); | ||
|
||
return ( | ||
<Autocomplete | ||
options={options} | ||
value={ | ||
multi | ||
? allOptions.filter(item => value.includes(item.value)) | ||
: allOptions.find(item => item.value === value) | ||
} | ||
isMulti={multi} | ||
components={{ | ||
Option, | ||
}} | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
function Option(props) { | ||
if (props.value === SPLIT_TRANSACTION_KEY) { | ||
return ( | ||
<SelectComponents.Option {...props}> | ||
<View | ||
style={{ | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
fontSize: 11, | ||
color: colors.g8, | ||
marginLeft: -12, | ||
padding: '4px 0', | ||
}} | ||
data-testid="split-transaction-button" | ||
> | ||
<Split | ||
width={10} | ||
height={10} | ||
style={{ marginRight: 5, color: 'inherit' }} | ||
/> | ||
Split Transaction | ||
</View> | ||
</SelectComponents.Option> | ||
); | ||
} | ||
return <SelectComponents.Option {...props} />; | ||
} |
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
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
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.