import { CSSProperties } from 'react'; import { MouseEventHandler, ReactEventHandler, useCallback, useState, } from 'react'; export type AvailabilityKind = 'going' | 'interested' | 'not-interested'; const availabilityNames: Record = { going: 'Going', interested: 'Interested', 'not-interested': 'Not interested', }; const optionStyle: CSSProperties = { height: '3rem', backgroundColor: '#e0e0e0', display: 'flex', flexDirection: 'row', justifyContent: 'center', alignItems: 'center', cursor: 'pointer', transition: 'background-color 100ms cubic-bezier', userSelect: 'none', position: 'relative', fontWeight: 'normal', textTransform: 'uppercase', }; const selectedOptionStyle = { ...optionStyle, fontWeight: 600, }; function Option({ bind, current, onSelected, }: { bind: AvailabilityKind; current: AvailabilityKind; onSelected: (kind: AvailabilityKind) => void; }) { const selected = current === bind; const select: MouseEventHandler = useCallback( (event) => { onSelected(bind); }, [onSelected, bind] ); return (
{availabilityNames[bind]}
); } // eslint-disable-next-line function Availability__old({ selected, onSelected: onSelectedInner, }: { selected: AvailabilityKind; onSelected: (kind: AvailabilityKind) => void; }) { const [focused, setFocused] = useState(false); const onSelected = useCallback( (kind: AvailabilityKind) => { setFocused(false); onSelectedInner(kind); }, [onSelectedInner] ); return (
setFocused(false)} > {focused ? ( <>
); } export default function Availability({ selected, onSelected: onSelectedInner, }: { selected: AvailabilityKind; onSelected: (kind: AvailabilityKind) => void; }) { const onSelected: ReactEventHandler = useCallback( (event) => { onSelectedInner( // @ts-ignore event.target.value ); event.preventDefault(); }, [onSelectedInner] ); return ( ); }