import React, { useCallback } from "react"; import styled from "styled-components"; import { ComponentProps } from "widgets/BaseComponent"; import { RadioGroup, Radio, Alignment, Classes } from "@blueprintjs/core"; import { TextSize } from "constants/WidgetConstants"; import { BlueprintRadioSwitchGroupTransform } from "constants/DefaultTheme"; import { LabelPosition } from "components/constants"; import { RadioOption } from "../constants"; import LabelWithTooltip, { labelLayoutStyles, LABEL_CONTAINER_CLASS, } from "widgets/components/LabelWithTooltip"; export interface RadioGroupContainerProps { compactMode: boolean; labelPosition?: LabelPosition; } export const RadioGroupContainer = styled.div` ${labelLayoutStyles} & .${LABEL_CONTAINER_CLASS} { align-self: center; ${({ labelPosition }) => labelPosition === LabelPosition.Left && "min-height: 30px"}; } `; export interface StyledRadioGroupProps { alignment: Alignment; compactMode: boolean; height?: number; inline: boolean; labelPosition?: LabelPosition; optionCount: number; accentColor: string; isDynamicHeightEnabled?: boolean; } const StyledRadioGroup = styled(RadioGroup)` ${BlueprintRadioSwitchGroupTransform} .${Classes.CONTROL} { & input:checked ~ .${Classes.CONTROL_INDICATOR} { background: ${({ accentColor }) => `${accentColor}`} !important; border: 1px solid ${({ accentColor }) => `${accentColor}`} !important; } & input:disabled:checked ~ .${Classes.CONTROL_INDICATOR} { &:before { opacity: 1; background-image: radial-gradient( var(--wds-color-bg-disabled-strong), var(--wds-color-bg-disabled-strong) 28%, transparent 32% ); } } } .${Classes.SWITCH} { & input:not(:disabled):active:checked ~ .${Classes.CONTROL_INDICATOR} { background: ${({ accentColor }) => `${accentColor}`}; } } `; function RadioGroupComponent(props: RadioGroupComponentProps) { const { accentColor, alignment, compactMode, disabled, height, inline, isDynamicHeightEnabled, labelAlignment, labelPosition, labelStyle, labelText, labelTextColor, labelTextSize, labelTooltip, labelWidth, loading, onRadioSelectionChange, options, required, selectedOptionValue, } = props; const optionCount = (options || []).length; const handleChange = useCallback( (event: React.FormEvent) => { onRadioSelectionChange(event.currentTarget.value); }, [onRadioSelectionChange], ); return ( {labelText && ( )} {options.map((option, optInd) => { return ( ); })} ); } export interface RadioGroupComponentProps extends ComponentProps { options: RadioOption[]; onRadioSelectionChange: (updatedOptionValue: string) => void; selectedOptionValue: string; disabled: boolean; loading: boolean; isDynamicHeightEnabled?: boolean; inline: boolean; alignment: Alignment; compactMode: boolean; labelText: string; labelPosition?: LabelPosition; labelAlignment?: Alignment; labelTextColor?: string; labelTextSize?: TextSize; labelStyle?: string; labelWidth?: number; labelTooltip?: string; widgetId: string; height?: number; accentColor: string; required?: boolean; } export default RadioGroupComponent;