import React from 'react' import styled from 'styled-components' import { margin, MarginProps } from 'styled-system' import { PolyFCWithRef } from '../helpers/asProps' import { isIE } from '../helpers/constants' import { flexItem, FlexItemProps } from '../helpers/flexItem' import generateId from '../helpers/generateId' import { omitProps } from '../helpers/omit' import { useBox } from '../helpers/react' import { styledWithClass } from '../helpers/styled' import { border, boxShadow, radius, textStyle } from '../helpers/theme' import { FlexGroup, GroupProps, makeGroup } from './Group' import { CheckableProps, WrapperLabel } from './Wrapper' type Checkable = 'checkbox' | 'radio' interface BaseCardProps extends CheckableProps, FlexItemProps { focusRef?: React.RefObject<{ focus: () => void }> children?: React.ReactNode } type InternalProps = BaseCardProps & { type: Checkable } type ToggleCardProps = BaseCardProps & { ref?: React.Ref } export interface ToggleCardComponent extends React.FC { Group: React.FC Inverse: PolyFCWithRef<{}, 'span'> // eslint-disable-line @typescript-eslint/ban-types } interface ToggleBox { id: string focusRef?: React.RefObject<{ focus: () => void }> onChange?: React.ChangeEventHandler focusOnChecked: React.ChangeEventHandler emulateLabel: React.MouseEventHandler } const CLICK_ELEMENTS = 'a[href],input,button,select,textarea' const initHandlers = () => { let timeoutId: any = 0 const box: ToggleBox = { id: generateId('toggle-card'), focusOnChecked: (e: React.ChangeEvent) => { const { onChange, focusRef } = box onChange?.(e) if (e.target.checked) { focusRef?.current?.focus() } }, emulateLabel: (e: React.MouseEvent) => { const input = e.currentTarget.querySelector('input') const target = e.target as HTMLElement const isLeftClick = e.button == 0 // Automatically stop for elements with "default" onClick behavior; // otherwise you'd need to call `preventDefault` or `stopPropagation`. if (input && input !== target && isLeftClick && !e.isDefaultPrevented()) { if (!timeoutId && !(input.checked && target.matches(CLICK_ELEMENTS))) { input.focus() input.click() // Disable further interaction until `