diff --git a/packages/components/src/array-base/index.tsx b/packages/components/src/array-base/index.tsx index 1ec27fe..67be42e 100644 --- a/packages/components/src/array-base/index.tsx +++ b/packages/components/src/array-base/index.tsx @@ -6,7 +6,6 @@ import { PlusOutlined, UpOutlined, } from '@ant-design/icons' -import { AntdIconProps } from '@ant-design/icons/lib/components/AntdIcon' import { ArrayField } from '@formily/core' import { ReactFC, @@ -16,7 +15,7 @@ import { useField, useFieldSchema, } from '@formily/react' -import { clone, isValid } from '@formily/shared' +import { clone, isUndef, isValid } from '@formily/shared' import { Button, ButtonProps } from 'antd' import cls from 'classnames' import React, { createContext, forwardRef, useContext } from 'react' @@ -40,7 +39,7 @@ export interface IArrayBaseItemProps { record: ((index: number) => Record) | Record } -type CommonProps = AntdIconProps & { +type CommonProps = ButtonProps & { index?: number } @@ -198,8 +197,7 @@ const Addition: ReactFC = (props) => { ) } - -const Copy = forwardRef((props, ref) => { +const Copy = forwardRef((props, ref) => { const self = useField() const array = useArray() const index = useIndex(props.index) || 0 @@ -208,8 +206,16 @@ const Copy = forwardRef((props, ref) => { if (!array) return null if (array.field?.pattern !== 'editable') return null return wrapSSR( - ((props, ref) => { if (self?.disabled) return e.stopPropagation() if (array.props?.disabled) return + if (props.onClick) { + props.onClick(e) + if (e.defaultPrevented) return + } const value = clone(array?.field?.value[index]) const distIndex = index + 1 array.field?.insert?.(distIndex, value) array.props?.onCopy?.(distIndex) - if (props.onClick) { - props.onClick(e) - } }} - /> + icon={isUndef(props.icon) ? : props.icon} + > + {props.title || self.title} + ) }) const Remove = forwardRef((props, ref) => { - const index = useIndex(props.index) || 0 + const index = useIndex(props.index) const self = useField() const array = useArray() const prefixCls = usePrefixCls('formily-array-base') const [wrapSSR, hashId] = useStyle(prefixCls) + if (!array) return null if (array.field?.pattern !== 'editable') return null return wrapSSR( - ((props, ref) => { onClick={(e) => { if (self?.disabled) return e.stopPropagation() - array.field?.remove?.(index) - array.props?.onRemove?.(index) if (props.onClick) { props.onClick(e) + if (e.defaultPrevented) return } + array.field?.remove?.(index) + array.props?.onRemove?.(index) }} - /> + icon={isUndef(props.icon) ? : props.icon} + > + {props.title || self.title} + ) }) const MoveDown = forwardRef((props, ref) => { - const index = useIndex(props.index) || 0 + const index = useIndex(props.index) const self = useField() const array = useArray() const prefixCls = usePrefixCls('formily-array-base') - const [wrapSSR, hashId] = useStyle(prefixCls) if (!array) return null if (array.field?.pattern !== 'editable') return null - return wrapSSR( - ((props, ref) => { onClick={(e) => { if (self?.disabled) return e.stopPropagation() - array.field?.moveDown?.(index) - array.props?.onMoveDown?.(index) if (props.onClick) { props.onClick(e) + if (e.defaultPrevented) return } + array.field?.moveDown?.(index) + array.props?.onMoveDown?.(index) }} - /> + icon={isUndef(props.icon) ? : props.icon} + > + {props.title || self.title} + ) }) @@ -300,15 +333,21 @@ const MoveUp = forwardRef((props, ref) => { const self = useField() const array = useArray() const prefixCls = usePrefixCls('formily-array-base') - const [wrapSSR, hashId] = useStyle(prefixCls) if (!array) return null if (array.field?.pattern !== 'editable') return null - return wrapSSR( - ((props, ref) => { onClick={(e) => { if (self?.disabled) return e.stopPropagation() - array?.field?.moveUp(index) - array?.props?.onMoveUp?.(index) if (props.onClick) { props.onClick(e) + if (e.defaultPrevented) return } + array?.field?.moveUp(index) + array?.props?.onMoveUp?.(index) }} - /> + icon={isUndef(props.icon) ? : props.icon} + > + {props.title || self.title} + ) })