import React, { memo, useMemo } from 'react'; import type { Key, ReactNode } from 'react'; import { Select, Switch } from 'antd'; import type { SelectProps, SwitchProps } from 'antd'; export interface StatusConfig { key: T; text: string; } export interface SelectStatusProps extends SelectProps { keys?: T[]; } export interface SwitchStatusProps extends Omit { value?: T; onChange?: (data: T | undefined) => void; keys?: T[]; } export interface ShowStatusProps { value?: T; empty?: ReactNode; } export interface ShowStatusListProps { value?: T[]; empty?: ReactNode; split?: string; } export function status(config: StatusConfig[]) { const data: Map = new Map(); const keys: T[] = []; config .filter((item) => item) .forEach((item) => { keys.push(item.key); data.set(item.key, item.text); }); const getStatusText = (params: { key?: T; empty?: ReactNode }): ReactNode => { if (params.key === undefined || params.key === null) return params.empty || ''; return data.get(params.key) || '未知'; }; const SelectStatus = memo((props: SelectStatusProps) => { return ( ); }); const SwitchStatus = memo((props: SwitchStatusProps) => { const allKeys = useMemo(() => props.keys || keys, [props.keys]); const enableKey = useMemo( () => (allKeys.length > 1 ? allKeys[1] : allKeys[0]), [allKeys] ); const disableKey = useMemo( () => (allKeys.length > 1 ? allKeys[0] : undefined), [allKeys] ); return ( { props.onChange && props.onChange(check ? enableKey : disableKey); }} /> ); }); const ShowStatus = memo((props: ShowStatusProps) => { return <>{getStatusText({ key: props.value, empty: props.empty })}; }); const ShowStatusList = memo((props: ShowStatusListProps) => { return ( <> {props.value ? props.value .map((item) => getStatusText({ key: item, empty: props.empty })) .join(props.split || ',') : props.empty || ''} ); }); return { SelectStatus, SwitchStatus, ShowStatus, ShowStatusList }; }