segmented.tsx


Overview

The segmented.tsx file defines a reusable React functional component named Segmented. This component renders a segmented control UI element, which allows users to select one option from a set of choices displayed as adjacent segments (similar to a tab or toggle group). Each segment represents an option, which may be a simple string/number or an object with additional metadata such as labels, disabled state, and custom CSS classes.

This component manages internal selection state and supports controlled and uncontrolled modes via props. It also provides styling hooks to customize the active segment appearance.


Detailed Explanation

Types and Interfaces

SegmentedValue

export declare type SegmentedValue = string | number;

SegmentedRawOption

export declare type SegmentedRawOption = SegmentedValue;

SegmentedLabeledOption

export interface SegmentedLabeledOption {
  className?: string;
  disabled?: boolean;
  label: React.ReactNode;
  value: SegmentedRawOption;
  title?: string;
}

SegmentedOptions

type SegmentedOptions = (SegmentedRawOption | SegmentedLabeledOption)[];

SegmentedProps

export interface SegmentedProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onChange'> {
  options: SegmentedOptions;
  defaultValue?: SegmentedValue;
  value?: SegmentedValue;
  onChange?: (value: SegmentedValue) => void;
  disabled?: boolean;
  prefixCls?: string;
  direction?: 'ltr' | 'rtl';
  motionName?: string;
  activeClassName?: string;
}

Segmented React Component

export function Segmented({
  options,
  value,
  onChange,
  className,
  activeClassName,
}: SegmentedProps) {
  const [selectedValue, setSelectedValue] = React.useState<
    SegmentedValue | undefined
  >(value);

  const handleOnChange = (e: SegmentedValue) => {
    if (onChange) {
      onChange(e);
    }
    setSelectedValue(e);
  };

  return (
    <div
      className={cn(
        'flex items-center rounded-3xl p-1 gap-2 bg-bg-card px-5 py-2.5',
        className,
      )}
    >
      {options.map((option) => {
        const isObject = typeof option === 'object';
        const actualValue = isObject ? option.value : option;

        return (
          <div
            key={actualValue}
            className={cn(
              'inline-flex items-center px-6 py-2 text-base font-normal rounded-3xl cursor-pointer',
              {
                'text-bg-base bg-metallic-gradient border-b-[#00BEB4] border-b-2':
                  selectedValue === actualValue,
              },
              activeClassName && selectedValue === actualValue
                ? activeClassName
                : '',
            )}
            onClick={() => handleOnChange(actualValue)}
          >
            {isObject ? option.label : option}
          </div>
        );
      })}
    </div>
  );
}

Parameters

Returns

Behavior and Implementation Details

Usage Example

import { Segmented } from './segmented';

const options = [
  { label: 'Option 1', value: 'opt1' },
  'Option 2',
  { label: <b>Option 3</b>, value: 'opt3', className: 'custom-class' },
];

function App() {
  const [selected, setSelected] = React.useState('opt1');

  return (
    <Segmented
      options={options}
      value={selected}
      onChange={setSelected}
      className="my-segmented"
      activeClassName="active-segment"
    />
  );
}

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram: Component Structure and Workflow

componentDiagram
    Segmented <|-- React.Component

    Segmented : +props: SegmentedProps
    Segmented : +state: selectedValue
    Segmented : +handleOnChange(value: SegmentedValue)
    Segmented : +render()

    Segmented --> "Segment[]" : options prop

    note right of Segmented
      - Renders segmented control UI
      - Manages selection state
      - Calls onChange callback on selection
    end note

Summary

The segmented.tsx file provides a versatile, styled segmented control component in React, supporting flexible options and selection management. While simple and clean in design, it currently lacks some accessibility features and complete prop handling for disabled state and controlled mode updates. It fits into a UI library or component set where segmented selection is required, leveraging Tailwind CSS and a utility for class name management.


If you need additional details such as how to extend this component with accessibility or animations, or integration examples within a larger app, please ask!