/* @flow */ import React, {useRef} from 'react' import classnames from 'classnames' import { globalProps, GlobalProps } from '../utilities/globalProps' import { buildAriaProps, buildCss, buildDataProps, noop, } from '../utilities/props' import Icon from '../pb_icon/_icon' import Checkbox from '../pb_checkbox/_checkbox' import Card from '../pb_card/_card' import Flex from '../pb_flex/_flex' import Radio from '../pb_radio/_radio' type SelectableCardProps = { aria?: { [key: string]: string }, checked?: boolean, children?: React.ReactChild[] | React.ReactChild, className?: string, customIcon?: {[key: string] :SVGElement}, dark?: boolean, data?: { [key: string]: string }, disabled?: boolean, error?: boolean, icon?: boolean, id?: string, inputId?: string, multi?: boolean, name?: string, onChange: (event: React.FormEvent) => void, text?: string, value?: string, variant?: string, } & GlobalProps const SelectableCard = (props: SelectableCardProps) => { const { aria = {}, checked = false, className, customIcon, dark = false, data = {}, disabled = false, error = false, icon = false, inputId = null, multi = true, name, onChange = noop, text, value, variant = 'default', } = props const ariaProps = buildAriaProps(aria) const dataProps = buildDataProps(data) const classes = classnames(buildCss('pb_selectable_card_kit', { 'checked': checked, 'disabled': disabled, 'enabled': !disabled, }), { error }, dark ? 'dark' : '', className ) const displayIcon = () => { if (icon === true) { return (
) } } const inputRef = useRef(null) // Delegate clicks to hidden input from visible one const handleClick = () => { inputRef.current.click() } const inputType = multi ? 'checkbox' : 'radio' const inputIdPresent = inputId !== null ? inputId : name const Input = multi ? Checkbox : Radio const filteredProps = {...props} delete filteredProps?.inputId delete filteredProps?.children delete filteredProps?.icon delete filteredProps?.error delete filteredProps?.dark delete filteredProps?.multi const labelProps: GlobalProps = variant === 'displayInput' ? { ...filteredProps, padding: 'none' } : { ...filteredProps } return (