Created
April 20, 2021 13:05
-
-
Save jacques-blom/e937d357a4c1ce99343ff1391a62a316 to your computer and use it in GitHub Desktop.
Atom Effects 2
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
import {Button} from '@chakra-ui/button' | |
import {Input} from '@chakra-ui/input' | |
import {Box, Divider, Heading, VStack} from '@chakra-ui/layout' | |
import React, {useState} from 'react' | |
import {atom, atomFamily, useRecoilCallback, useRecoilState, useRecoilValue, useResetRecoilState} from 'recoil' | |
type ItemType = { | |
label: string | |
checked: boolean | |
} | |
const idsState = atom<number[]>({ | |
key: 'ids', | |
default: [], | |
}) | |
const itemState = atomFamily<ItemType, number>({ | |
key: 'item', | |
default: {label: '', checked: false}, | |
}) | |
export const AtomEffects = () => { | |
const ids = useRecoilValue(idsState) | |
const resetList = useResetRecoilState(idsState) | |
const nextId = ids.length | |
const insertItem = useRecoilCallback(({set}) => (label: string) => { | |
set(idsState, [...ids, nextId]) | |
set(itemState(nextId), {label, checked: false}) | |
}) | |
return ( | |
<Container onClear={() => resetList()}> | |
{ids.map((id) => ( | |
<Item key={id} id={id} /> | |
))} | |
<NewItemInput | |
onInsert={(label) => { | |
insertItem(label) | |
}} | |
/> | |
</Container> | |
) | |
} | |
const Container: React.FC<{onClear: () => void}> = ({children, onClear}) => { | |
return ( | |
<Box display="flex" flexDir="column" alignItems="center" pt={10}> | |
<Box width="400px" backgroundColor="yellow.100" p={5} borderRadius="lg"> | |
<Heading size="lg" mb={4}> | |
Shopping List | |
</Heading> | |
<VStack spacing={3} divider={<Divider borderColor="rgba(86, 0, 0, 0.48)" />}> | |
{children} | |
</VStack> | |
</Box> | |
<Button variant="link" mt={3} onClick={onClear}> | |
Clear list | |
</Button> | |
</Box> | |
) | |
} | |
type ItemProps = { | |
id: number | |
} | |
const Item = ({id}: ItemProps) => { | |
const [item, setItem] = useRecoilState(itemState(id)) | |
return ( | |
<Box | |
rounded="md" | |
textDecoration={item.checked ? 'line-through' : ''} | |
opacity={item.checked ? 0.5 : 1} | |
_hover={{textDecoration: 'line-through'}} | |
cursor="pointer" | |
width="100%" | |
onClick={() => setItem({...item, checked: !item.checked})} | |
> | |
{item.label} | |
</Box> | |
) | |
} | |
const NewItemInput = ({onInsert}: {onInsert: (label: string) => void}) => { | |
const [value, setValue] = useState('') | |
return ( | |
<Input | |
value={value} | |
placeholder="New item" | |
padding={0} | |
height="auto" | |
border="none" | |
_focus={{border: 'none'}} | |
_placeholder={{color: 'rgba(86, 0, 0, 0.48)'}} | |
onChange={(e) => { | |
setValue(e.currentTarget.value) | |
}} | |
onKeyPress={({key}) => { | |
if (key === 'Enter') { | |
onInsert(value) | |
setValue('') | |
} | |
}} | |
/> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment