Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | 2x 2x 2x 2x 2x 2x 36x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 4x 4x 4x 1x 1x 4x 12x 12x 12x 12x 12x 12x 2x | import React from "react"; import { ToggleButtonGroupProps, ToggleButtonStyleGroupProps, } from "./ToggleButtonGroup.types"; import styled from "styled-components"; import { useTheme } from "../../ThemeProvider/ThemeProvider"; import colorTokens from "../../../tokens/colors.json"; import { getColorPalette } from "../../../helpers/helpers"; const getBorder = ( color: ToggleButtonStyleGroupProps["$color"], colorPalette: ToggleButtonStyleGroupProps["$colorPalette"] ) => { return { default: `1px solid ${colorPalette[color].accentScale[8]}`, hover: `1px solid ${colorPalette[color].accentScale[9]}`, active: `1px solid ${colorPalette[color].accentScale[9]}`, }; }; const StyledToggleBtnGroup = styled.div<ToggleButtonStyleGroupProps>` display: flex; flex-direction: row; justify-content: center; align-items: center; width: ${(props) => props.$width}; & > :first-child { border-top-left-radius: 4px; border-bottom-left-radius: 4px; } & > :not(:first-child):not(:last-child) { border-radius: 0; } & > :not(:last-child) { border-left: ${(props) => getBorder(props.$color, props.$colorPalette).default}; border-top: ${(props) => getBorder(props.$color, props.$colorPalette).default}; border-bottom: ${(props) => getBorder(props.$color, props.$colorPalette).default}; border-right: 0; &:hover { border-left: ${(props) => getBorder(props.$color, props.$colorPalette).hover}; border-top: ${(props) => getBorder(props.$color, props.$colorPalette).hover}; border-bottom: ${(props) => getBorder(props.$color, props.$colorPalette).hover}; } &:active { border-left: ${(props) => getBorder(props.$color, props.$colorPalette).active}; border-top: ${(props) => getBorder(props.$color, props.$colorPalette).active}; border-bottom: ${(props) => getBorder(props.$color, props.$colorPalette).active}; } } & > :last-child { border-top-right-radius: 4px; border-bottom-right-radius: 4px; border: ${(props) => getBorder(props.$color, props.$colorPalette).default}; &:hover { border: ${(props) => getBorder(props.$color, props.$colorPalette).hover}; } &:active { border: ${(props) => getBorder(props.$color, props.$colorPalette).active}; } } `; const getIndexFromValue = (value: string, children: React.ReactNode) => { const childrenArray = React.Children.toArray(children); return childrenArray.findIndex((child: any) => child.props.value === value); }; const ToggleButtonGroup = ({ children, value, color = colorTokens.default.primary.main, className, size, style, width = "fit-content", //Events onClick, "data-testid": dataTestId, ...props }: ToggleButtonGroupProps) => { const [selectedIndex, setSelectedIndex] = React.useState<number | null>( value ? getIndexFromValue(value, children) : null ); React.useEffect(() => { setSelectedIndex(value ? getIndexFromValue(value, children) : null); }, [value, children]); const theme = useTheme().theme; const colorPalette = getColorPalette(theme,color); const _onClick = (e: React.MouseEvent, value?: string, index?: number) => { setSelectedIndex(index ?? null); onClick && onClick(e, value); }; return ( <StyledToggleBtnGroup $width={width} $color={color} $colorPalette={colorPalette} className={className} style={style} data-testid={dataTestId} {...props} > {React.Children.map(children, (child, index) => { if (React.isValidElement(child) && child.type) { const childElement = child as React.ReactElement; const childType = childElement.type; Iif (typeof childType === "string") return child; /* causing an issue in production build as they define a custom name for the component */ if ((childType as any).displayName === "ToggleButton") { return React.cloneElement(childElement, { index: index, selected: selectedIndex === index, ...(!childElement.props.color && color && { color }), ...(!childElement.props.size && size && { size }), onClick: _onClick, }); } } return child; })} </StyledToggleBtnGroup> ); }; export default ToggleButtonGroup; |