mirror of
https://github.com/Steffo99/todocolors.git
synced 2024-11-22 08:14:18 +00:00
Add toggleStarred
callback to StarredProvider
This commit is contained in:
parent
506a27e989
commit
0d731c6d5a
2 changed files with 19 additions and 1 deletions
|
@ -6,5 +6,6 @@ export interface StarredContextData {
|
|||
setStarred: Dispatch<SetStateAction<string[] | undefined>>
|
||||
addStarred: (key: string) => void,
|
||||
removeStarred: (key: string) => void,
|
||||
toggleStarred: (key: string) => boolean,
|
||||
isStarred: (key: string) => boolean,
|
||||
}
|
||||
|
|
|
@ -32,12 +32,29 @@ export function StarredProvider({children}: {children: ReactNode}) {
|
|||
})
|
||||
}, [])
|
||||
|
||||
const toggleStarred = useCallback((value: string) => {
|
||||
setStarred(prev => {
|
||||
if(!prev) {
|
||||
return [value]
|
||||
}
|
||||
const result = [...prev]
|
||||
const index = result.indexOf(value)
|
||||
if(!index) {
|
||||
result.push(value)
|
||||
}
|
||||
else {
|
||||
result.splice(index, 1)
|
||||
}
|
||||
return result
|
||||
})
|
||||
}, [])
|
||||
|
||||
const isStarred = useCallback((value: string) => {
|
||||
return starred.indexOf(value) >= 0
|
||||
}, [starred])
|
||||
|
||||
return (
|
||||
<StarredContext.Provider value={{starred, setStarred, addStarred, removeStarred, isStarred}}>
|
||||
<StarredContext.Provider value={{starred, setStarred, addStarred, removeStarred, toggleStarred, isStarred}}>
|
||||
{children}
|
||||
</StarredContext.Provider>
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue