import React from "react"; import classNames from "classnames"; import { ControlledProps, useDefaultValue } from "../form/controlled"; import { SegmentOption } from "./SegmentOption"; import { StyledProps } from "../_type"; import { Button } from "../button"; 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 SegmentMultipleProps extends ControlledProps, StyledProps { options?: SegmentOption[]; /** * 分组 */ groups?: { [groupKey: string]: React.ReactNode; }; /** * 是否为无边框样式 * @default false */ rimless?: boolean; /** * 包含分组时,外层分组容器自定义类名 * @since 2.6.11 */ groupClassName?: string; /** * 包含分组时,外层分组容器自定义样式 * @since 2.6.11 */ groupStyle?: React.CSSProperties; } export const SegmentMultiple = forwardRefWithStatics( function SegmentMultiple( props: SegmentMultipleProps, ref: React.Ref ) { const { value, onChange, groups, groupClassName, groupStyle, options, } = useDefaultValue(props); const valueSet = new Set(value); if (groups) { return ( {Object.entries(groups).map(([key, name]) => { const subOptions = options.filter(i => i.groupKey === key); return ( ); })} ); } return ( ); }, { defaultLabelAlign: "middle", } ); SegmentMultiple.displayName = "SegmentMultiple"; const SegmentMain = React.forwardRef(function SegmentMain( { onChange, rimless, options, style, className, // 兼容分组 valueSet, allOptions = options, ...props }: SegmentMultipleProps & { valueSet: Set; allOptions?: SegmentMultipleProps["options"]; }, ref: React.Ref ) { const { classPrefix } = useConfig(); return (
{options.map(option => ( ))}
); }); SegmentMain.displayName = "SegmentMain";