赞
踩
类似designable-antd平台的这个切换功能:
polyInput.tsx:
import React, { useEffect, useState, useMemo } from 'react'; import { Button, Row, Tooltip } from 'antd'; import { useField } from '@formily/react'; import { Field } from '@formily/core'; export interface IInput { value: any; onChange: (value: any) => void; } export interface IPolyType { type: string; icon: JSX.Element; component?: any; checker: (value: any) => boolean; } export type PolyTypes = IPolyType[]; export function createPolyInput(polyTypes: PolyTypes = []): React.FC<IInput> { return ({ value, ...props }) => { const [curType, setCurType] = useState(polyTypes[0]?.type); const field: Field = useField(); const curTypeItem = polyTypes.filter((i) => i.type === curType)[0]; useEffect(() => { // 设置默认类型 polyTypes?.forEach(({ checker, type }) => { if (checker(value)) { // here setCurType(type); } }); }, []); const getNextType = () => { const currentIndex = polyTypes?.findIndex(({ type }) => type === curType); const nextIndex = currentIndex + 1 > polyTypes?.length - 1 ? 0 : currentIndex + 1; return polyTypes[nextIndex]; }; const renderContent = useMemo(() => { return ( <> {curTypeItem?.component && ( <div style={{ flex: 1, marginRight: 5 }}> {/* @ts-ignore */} {React.createElement(curTypeItem?.component, { ...props, // here value, })} </div> )} <Tooltip title={curTypeItem.type}> <Button icon={curTypeItem.icon} onClick={() => { const nextType = getNextType(); // here if (nextType.type === curType) { return; } setCurType(nextType?.type); field?.setValue(undefined); // 切换类型的时候,赋空值 }} /> </Tooltip> </> ); }, [curTypeItem]); return <Row justify="start">{renderContent}</Row>; }; }
使用:
import { FieldStringOutlined, SwitcherOutlined } from '@ant-design/icons'; import { isObject } from 'lodash'; import { Switch } from '@formily/antd'; import LanguageTextRule, { LanguageComponentType } from '../LanguageTextRule'; import { createPolyInput } from '../PolyInput'; export enum ValidatorType { BOOLEAN = 'boolean', LANGUAGE_MESSAGE = 'message', } export const isBoolean = (value: any) => typeof value === 'boolean'; export const isObj = (value: any) => isObject(value); export const ValidatorTypesMap = [ { type: ValidatorType.BOOLEAN, icon: <SwitcherOutlined />, component: Switch, checker: isBoolean, }, { type: ValidatorType.LANGUAGE_MESSAGE, icon: <FieldStringOutlined />, component: (props: any) => { return ( <LanguageTextRule title="Error Message" componentType={LanguageComponentType.TEXT} buttonText="+ Error Message" isModal {...props} /> ); }, checker: isObj, }, ]; export default createPolyInput(ValidatorTypesMap);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。