{"version":3,"file":"SegmentedControl.mjs","names":[],"sources":["../../../src/segmented-control/SegmentedControl.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport classNames from 'classnames';\nimport React, { useContext } from 'react';\n\nimport {\n  type PolymorphicCommonProps,\n  type TooltipObjectProps,\n} from '../_common/types';\nimport { Tooltip } from '../tooltip/Tooltip';\n\ntype SegmentedControlID = string | number;\n\n/**\n * @remarks\n * Where type `SegmentedControlID` is defined as: `string | number`.\n */\ntype SegmentedControlProps<T extends SegmentedControlID> = {\n  /** Additional class name */\n  className?: string;\n  /** Size of the segmented control */\n  size?: 'small' | 'medium' | 'large';\n  /** If segmented control items will include only icons (makes controls rectangular) */\n  hasOnlyIcons?: boolean;\n  /** The callback function triggered when the segmented control value changes */\n  onChange: (event: T) => void | ((event: React.SetStateAction<T>) => void);\n  /** The currently selected option */\n  selected: T;\n  /** The children component of the segmented control */\n  children: React.ReactNode;\n  /** Whether the segmented control has a box shadow applied */\n  isFloating?: boolean;\n};\n\ntype SegmentedControlContext<T extends SegmentedControlID> = Required<\n  Pick<SegmentedControlProps<T>, 'onChange' | 'selected' | 'hasOnlyIcons'>\n>;\n\n// eslint-disable-next-line no-redeclare\nconst SegmentedControlContext =\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  React.createContext<SegmentedControlContext<any> | null>(null);\n\nconst useSelectorContext = () => {\n  const context = useContext(SegmentedControlContext);\n\n  if (context === null) {\n    throw new Error('SegmentedControl used without context');\n  }\n\n  return context;\n};\n\nconst SegmentedControlComponent = <\n  T extends SegmentedControlID,\n  U extends React.ElementType = 'div',\n>({\n  size = 'medium',\n  hasOnlyIcons = false,\n  children,\n  selected,\n  onChange,\n  as,\n  className,\n  style,\n  isFloating = false,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: PolymorphicCommonProps<U, SegmentedControlProps<T>>) => {\n  const classes = classNames('ndl-segmented-control', className, {\n    'ndl-floating': isFloating,\n    'ndl-large': size === 'large',\n    'ndl-medium': size === 'medium',\n    'ndl-small': size === 'small',\n  });\n\n  const Component = as ?? 'div';\n\n  return (\n    <Component\n      className={classes}\n      style={style}\n      role=\"group\"\n      ref={ref}\n      {...restProps}\n      {...htmlAttributes}\n    >\n      <SegmentedControlContext.Provider\n        value={{ hasOnlyIcons, onChange, selected }}\n      >\n        {children}\n      </SegmentedControlContext.Provider>\n    </Component>\n  );\n};\n\nSegmentedControlComponent.displayName = 'SegmentedControl';\n\n/**\n * @remarks\n * When the `description` prop is set, hovering over the segmented control item will show a tooltip with the text provided in the `description` prop. `tooltipProps` can be used to customize the tooltip appearance. The `TooltipObjectProps` type is defined as: `{ root?: Partial<React.ComponentProps<typeof Tooltip>>; trigger?: Partial<React.ComponentProps<typeof Tooltip.Trigger>>; content?: Partial<React.ComponentProps<typeof Tooltip.Content>>; }`, see the Tooltip component for more details.\n */\ntype SegmentedControlItemProps = {\n  /** Additional class name */\n  className?: string;\n  /** The children component of the segmented control item */\n  children?: React.ReactNode;\n  /** The value of the segmented control item */\n  value: string;\n  /** The description of the segmented control item, will be displayed in the tooltip */\n  description?: string;\n  /** The tooltip props that will be applied if tooltip is set */\n  tooltipProps?: TooltipObjectProps;\n  /** Whether the segmented control item is disabled */\n  isDisabled?: boolean;\n};\n\nconst SegmentedControlItem = <T extends React.ElementType = 'button'>({\n  as,\n  children,\n  className,\n  style,\n  value,\n  htmlAttributes,\n  description,\n  tooltipProps,\n  isDisabled = false,\n  ref,\n  ...restProps\n}: PolymorphicCommonProps<T, SegmentedControlItemProps>) => {\n  const { onChange, selected, hasOnlyIcons } = useSelectorContext();\n\n  const isSelected = value === selected;\n\n  const baseClasses = classNames(className, {\n    'ndl-current': isSelected,\n    'ndl-segment-icon': hasOnlyIcons,\n    'ndl-segment-item': !hasOnlyIcons,\n  });\n\n  const Component: React.ElementType = as ?? 'button';\n\n  return (\n    <Tooltip\n      isPortaled={false}\n      isDisabled={Boolean(description) === false}\n      {...tooltipProps?.root}\n      type=\"simple\"\n    >\n      <Tooltip.Trigger {...tooltipProps?.trigger} hasButtonWrapper>\n        <Component\n          aria-pressed={isSelected}\n          onClick={() => !isSelected && onChange(value)}\n          className={baseClasses}\n          style={style}\n          ref={ref}\n          aria-label={description}\n          disabled={isDisabled}\n          {...restProps}\n          {...htmlAttributes}\n        >\n          {children}\n        </Component>\n      </Tooltip.Trigger>\n      <Tooltip.Content {...tooltipProps?.content}>\n        <Tooltip.Body>{description}</Tooltip.Body>\n      </Tooltip.Content>\n    </Tooltip>\n  );\n};\n\nSegmentedControlItem.displayName = 'SegmentedControl.Item';\n\nconst SegmentedControl = Object.assign(SegmentedControlComponent, {\n  Item: SegmentedControlItem,\n});\n\nexport { SegmentedControl };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2EE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;CAoDA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;AAzFF,MAAM,0BAEJ,MAAM,cAAmD,IAAI;AAE/D,MAAM,2BAA2B;CAC/B,MAAM,UAAU,WAAW,uBAAuB;CAElD,IAAI,YAAY,MACd,MAAM,IAAI,MAAM,uCAAuC;CAGzD,OAAO;AACT;AAEA,MAAM,6BAGJ,SAayD;CAbzD,IAAA,EACA,OAAO,UACP,eAAe,OACf,UACA,UACA,UACA,IACA,WACA,OACA,aAAa,OACb,gBACA,QAAA,MAC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