{"version":3,"file":"PromoCardGroup.mjs","sources":["../../src/promoCard/PromoCardGroup.tsx"],"sourcesContent":["import { clsx } from 'clsx';\nimport React, { FunctionComponent, ReactNode, useState, useEffect, useMemo } from 'react';\n\nimport { PromoCardProps } from './PromoCard';\nimport PromoCardContext from './PromoCardContext';\n\nexport type AriaRoleRadioGroup = 'radiogroup';\n\nexport interface PromoCardGroupProps {\n  /**\n   * Optional class name(s) to add to the group container.\n   */\n  className?: string;\n  /**\n   * The PromoCard components to display inside the group.\n   */\n  children: ReactNode;\n  /**\n   * The default checked for the group.\n   */\n  defaultChecked?: string;\n  /**\n   * Optional ID to add to the group container.\n   */\n  id?: string;\n  /**\n   * Whether the group is disabled or not.\n   */\n  isDisabled?: boolean;\n  /**\n   * Optional label to display above the group.\n   */\n  label?: string;\n  /**\n   * Optional function to call when the group value changes.\n   */\n  onChange?: (value: string) => void;\n  /**\n   * Optional ID to add to the group container for testing purposes.\n   */\n  testId?: string;\n}\n\n/**\n * PromoCardGroup component.\n *\n * A PromoCardGroup is a container for PromoCard components that allows the user to select one or more\n * cards from a group. It can be used to display a set of related options, and can be customized with\n * various props to suit different use cases.\n *\n * @param {ReactNode} children - The PromoCard components to display inside the group.\n * @param {string} className - Optional class name(s) to add to the group container.\n * @param {string} defaultChecked - The default value for the group.\n * @param {string} id - Optional ID to add to the group container.\n * @param {boolean} isDisabled=false - Whether the group is disabled or not.\n * @param {string} label - Optional label to display above the group.\n * @param {Function} onChange - Optional function to call when the group value changes.\n * @param {string} testId - Optional ID to add to the group container for testing purposes.\n * @returns {React.JSX.Element} - The PromoCardGroup component.\n */\nconst PromoCardGroup: FunctionComponent<PromoCardGroupProps> = ({\n  children,\n  className,\n  defaultChecked = '',\n  id,\n  isDisabled = false,\n  onChange = () => {},\n  testId,\n}) => {\n  const [state, setState] = useState<string>(defaultChecked);\n  const [containerRole, setContainerRole] = useState<string | null>(null);\n\n  /**\n   * The context value for the PromoCardGroup.\n   *\n   * The context value is an object that contains the current state of the\n   * group, whether the group is disabled or not, and a function to call when\n   * the group value changes. This value is used to provide context to child\n   * PromoCard components, allowing them to interact with the group and update\n   * its state.\n   */\n  const contextValue = useMemo(() => {\n    const handleOnChange = (value: string) => {\n      setState(value);\n      onChange(value);\n    };\n\n    return { state, isDisabled, onChange: handleOnChange };\n  }, [state, isDisabled, onChange]);\n\n  const commonClasses = clsx(\n    {\n      'np-CardGroup': true,\n      'is-disabled': isDisabled,\n    },\n    className,\n  );\n\n  const commonProps = {\n    className: commonClasses,\n    id,\n    'data-testid': testId,\n    role: containerRole as AriaRoleRadioGroup | undefined, // Add the role attribute here\n  };\n\n  useEffect(() => {\n    setState(defaultChecked);\n\n    // Collect an array of types from the children PromoCard components\n    const types =\n      React.Children.map(children, (child) => {\n        if (React.isValidElement<PromoCardProps>(child) && child.props.type) {\n          return child.props.type;\n        }\n        return null;\n      })?.filter((type): type is 'radio' | 'checkbox' => type !== null && type !== undefined) ?? [];\n\n    // Check if all types are the same\n    const allTypesAreTheSame = types.every((type) => type === types[0]);\n\n    // If all types are the same and the type is 'radio', set the container role\n    setContainerRole(allTypesAreTheSame && types[0] === 'radio' ? 'radiogroup' : null);\n  }, [defaultChecked, children]);\n\n  return (\n    <PromoCardContext.Provider value={contextValue}>\n      <div {...commonProps}>{children}</div>\n    </PromoCardContext.Provider>\n  );\n};\n\nexport default React.memo(PromoCardGroup);\n"],"names":["PromoCardGroup","children","className","defaultChecked","id","isDisabled","onChange","testId","state","setState","useState","containerRole","setContainerRole","contextValue","useMemo","handleOnChange","value","commonClasses","clsx","commonProps","role","useEffect","types","React","Children","map","child","isValidElement","props","type","filter","undefined","allTypesAreTheSame","every","_jsx","PromoCardContext","Provider","memo"],"mappings":";;;;;AA4DA,MAAMA,cAAc,GAA2CA,CAAC;EAC9DC,QAAQ;EACRC,SAAS;AACTC,EAAAA,cAAc,GAAG,EAAE;EACnBC,EAAE;AACFC,EAAAA,UAAU,GAAG,KAAK;AAClBC,EAAAA,QAAQ,GAAGA,M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