// https://github.com/jb-1980/usa-map-react // MIT License import clsx from 'clsx' import { DATA } from './data' import { USAState } from './usa-state' export type ClickHandler = ( e: React.MouseEvent ) => R export type GetClickHandler = (stateKey: string) => ClickHandler | undefined export type CustomizeObj = { fill?: string clickHandler?: ClickHandler } export interface Customize { [key: string]: CustomizeObj } export type StatesProps = { hideStateTitle?: boolean fillStateColor: (stateKey: string) => string stateClickHandler: GetClickHandler } const States = ({ hideStateTitle, fillStateColor, stateClickHandler, }: StatesProps) => Object.entries(DATA).map(([stateKey, data]) => ( )) type USAMapPropTypes = { onClick?: ClickHandler width?: number height?: number title?: string defaultFill?: string customize?: Customize hideStateTitle?: boolean className?: string } export const USAMap = ({ onClick = (e) => { console.log(e.currentTarget.dataset.name) }, title = 'US states map', defaultFill = '#d3d3d3', customize, hideStateTitle, className, }: USAMapPropTypes) => { const fillStateColor = (state: string) => customize?.[state]?.fill ? (customize[state].fill as string) : defaultFill const stateClickHandler = (state: string) => customize?.[state]?.clickHandler return ( {title} {States({ hideStateTitle, fillStateColor, stateClickHandler, })} ) }