import React from "react"; import classNames from "classnames"; import { ControlledProps, useDefaultValue } from "../form/controlled"; import { StyledProps } from "../_type"; import { Button } from "../button"; import { SegmentOption } from "./SegmentOption"; import { Bubble } from "../bubble"; import { useConfig } from "../_util/config-context"; import { SegmentGroup, SegmentGroupItem } from "./SegmentGroup"; import { forwardRefWithStatics } from "../_util/forward-ref-with-statics"; import { mergeEventProps } from "../_util/merge-event-props"; export interface SegmentProps extends ControlledProps, StyledProps { /** * Segment 中选项 */ options: SegmentOption[]; /** * 分组 */ groups?: { [groupKey: string]: React.ReactNode; }; /** * 是否禁用所有选择 * @default false * @since 2.2.0 */ disabled?: boolean; /** * 是否为无边框样式 * @default false */ rimless?: boolean; /** * 包含分组时,外层分组容器自定义类名 */ groupClassName?: string; /** * 包含分组时,外层分组容器自定义样式 */ groupStyle?: React.CSSProperties; } export const Segment = forwardRefWithStatics( function Segment(props: SegmentProps, ref: React.Ref) { const { value, onChange, options, groups, groupClassName, groupStyle, } = useDefaultValue(props, ""); if (groups) { return ( {Object.entries(groups).map(([key, name]) => { const subOptions = options.filter(i => i.groupKey === key); return ( ); })} ); } return ( ); }, { defaultLabelAlign: "middle", } ); Segment.displayName = "Segment"; const SegmentMain = React.forwardRef(function SegmentMain( { value, onChange, options, disabled, rimless, className, style, ...props }: SegmentProps, ref: React.Ref ) { const { classPrefix } = useConfig(); return (
{options.map(option => { const button = ( ); if (option.bubble) { return ( {button} ); } return button; })}
); }); SegmentMain.displayName = "SegmentMain";