Skip to content

Commit 9f6fe64

Browse files
authored
fix: recover array-base actors icon button wrap (#30)
1 parent 29fa45a commit 9f6fe64

File tree

1 file changed

+73
-30
lines changed

1 file changed

+73
-30
lines changed

packages/components/src/array-base/index.tsx

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
PlusOutlined,
77
UpOutlined,
88
} from '@ant-design/icons'
9-
import { AntdIconProps } from '@ant-design/icons/lib/components/AntdIcon'
109
import { ArrayField } from '@formily/core'
1110
import {
1211
ReactFC,
@@ -16,7 +15,7 @@ import {
1615
useField,
1716
useFieldSchema,
1817
} from '@formily/react'
19-
import { clone, isValid } from '@formily/shared'
18+
import { clone, isUndef, isValid } from '@formily/shared'
2019
import { Button, ButtonProps } from 'antd'
2120
import cls from 'classnames'
2221
import React, { createContext, forwardRef, useContext } from 'react'
@@ -40,7 +39,7 @@ export interface IArrayBaseItemProps {
4039
record: ((index: number) => Record<string, any>) | Record<string, any>
4140
}
4241

43-
type CommonProps = AntdIconProps & {
42+
type CommonProps = ButtonProps & {
4443
index?: number
4544
}
4645

@@ -198,8 +197,7 @@ const Addition: ReactFC<IArrayBaseAdditionProps> = (props) => {
198197
</Button>
199198
)
200199
}
201-
202-
const Copy = forwardRef<HTMLSpanElement, CommonProps>((props, ref) => {
200+
const Copy = forwardRef<HTMLButtonElement, CommonProps>((props, ref) => {
203201
const self = useField()
204202
const array = useArray()
205203
const index = useIndex(props.index) || 0
@@ -208,8 +206,16 @@ const Copy = forwardRef<HTMLSpanElement, CommonProps>((props, ref) => {
208206
if (!array) return null
209207
if (array.field?.pattern !== 'editable') return null
210208
return wrapSSR(
211-
<CopyOutlined
209+
<Button
210+
type="ghost"
212211
{...props}
212+
style={{
213+
padding: '0 0 0 6px',
214+
width: 'auto',
215+
height: 'auto',
216+
...props.style,
217+
}}
218+
disabled={self?.disabled}
213219
className={cls(
214220
`${prefixCls}-copy`,
215221
hashId,
@@ -221,29 +227,42 @@ const Copy = forwardRef<HTMLSpanElement, CommonProps>((props, ref) => {
221227
if (self?.disabled) return
222228
e.stopPropagation()
223229
if (array.props?.disabled) return
230+
if (props.onClick) {
231+
props.onClick(e)
232+
if (e.defaultPrevented) return
233+
}
224234
const value = clone(array?.field?.value[index])
225235
const distIndex = index + 1
226236
array.field?.insert?.(distIndex, value)
227237
array.props?.onCopy?.(distIndex)
228-
if (props.onClick) {
229-
props.onClick(e)
230-
}
231238
}}
232-
/>
239+
icon={isUndef(props.icon) ? <CopyOutlined /> : props.icon}
240+
>
241+
{props.title || self.title}
242+
</Button>
233243
)
234244
})
235245

236246
const Remove = forwardRef<HTMLSpanElement, CommonProps>((props, ref) => {
237-
const index = useIndex(props.index) || 0
247+
const index = useIndex(props.index)
238248
const self = useField()
239249
const array = useArray()
240250
const prefixCls = usePrefixCls('formily-array-base')
241251
const [wrapSSR, hashId] = useStyle(prefixCls)
252+
242253
if (!array) return null
243254
if (array.field?.pattern !== 'editable') return null
244255
return wrapSSR(
245-
<DeleteOutlined
256+
<Button
257+
type="ghost"
246258
{...props}
259+
style={{
260+
padding: '0 0 0 6px',
261+
width: 'auto',
262+
height: 'auto',
263+
...props.style,
264+
}}
265+
disabled={self?.disabled}
247266
className={cls(
248267
`${prefixCls}-remove`,
249268
hashId,
@@ -254,44 +273,58 @@ const Remove = forwardRef<HTMLSpanElement, CommonProps>((props, ref) => {
254273
onClick={(e) => {
255274
if (self?.disabled) return
256275
e.stopPropagation()
257-
array.field?.remove?.(index)
258-
array.props?.onRemove?.(index)
259276
if (props.onClick) {
260277
props.onClick(e)
278+
if (e.defaultPrevented) return
261279
}
280+
array.field?.remove?.(index)
281+
array.props?.onRemove?.(index)
262282
}}
263-
/>
283+
icon={isUndef(props.icon) ? <DeleteOutlined /> : props.icon}
284+
>
285+
{props.title || self.title}
286+
</Button>
264287
)
265288
})
266289

267290
const MoveDown = forwardRef<HTMLSpanElement, CommonProps>((props, ref) => {
268-
const index = useIndex(props.index) || 0
291+
const index = useIndex(props.index)
269292
const self = useField()
270293
const array = useArray()
271294
const prefixCls = usePrefixCls('formily-array-base')
272-
const [wrapSSR, hashId] = useStyle(prefixCls)
273295
if (!array) return null
274296
if (array.field?.pattern !== 'editable') return null
275-
return wrapSSR(
276-
<DownOutlined
297+
return (
298+
<Button
299+
type="ghost"
277300
{...props}
301+
style={{
302+
padding: '0 0 0 6px',
303+
width: 'auto',
304+
height: 'auto',
305+
...props.style,
306+
}}
307+
disabled={self?.disabled}
278308
className={cls(
279309
`${prefixCls}-move-down`,
280-
hashId,
281310
self?.disabled ? `${prefixCls}-move-down-disabled` : '',
282311
props.className
283312
)}
284313
ref={ref}
285314
onClick={(e) => {
286315
if (self?.disabled) return
287316
e.stopPropagation()
288-
array.field?.moveDown?.(index)
289-
array.props?.onMoveDown?.(index)
290317
if (props.onClick) {
291318
props.onClick(e)
319+
if (e.defaultPrevented) return
292320
}
321+
array.field?.moveDown?.(index)
322+
array.props?.onMoveDown?.(index)
293323
}}
294-
/>
324+
icon={isUndef(props.icon) ? <DownOutlined /> : props.icon}
325+
>
326+
{props.title || self.title}
327+
</Button>
295328
)
296329
})
297330

@@ -300,29 +333,39 @@ const MoveUp = forwardRef<HTMLSpanElement, CommonProps>((props, ref) => {
300333
const self = useField()
301334
const array = useArray()
302335
const prefixCls = usePrefixCls('formily-array-base')
303-
const [wrapSSR, hashId] = useStyle(prefixCls)
304336
if (!array) return null
305337
if (array.field?.pattern !== 'editable') return null
306-
return wrapSSR(
307-
<UpOutlined
338+
return (
339+
<Button
340+
type="ghost"
308341
{...props}
342+
style={{
343+
padding: '0 0 0 6px',
344+
width: 'auto',
345+
height: 'auto',
346+
...props.style,
347+
}}
348+
disabled={self?.disabled}
309349
className={cls(
310350
`${prefixCls}-move-up`,
311-
hashId,
312351
self?.disabled ? `${prefixCls}-move-up-disabled` : '',
313352
props.className
314353
)}
315354
ref={ref}
316355
onClick={(e) => {
317356
if (self?.disabled) return
318357
e.stopPropagation()
319-
array?.field?.moveUp(index)
320-
array?.props?.onMoveUp?.(index)
321358
if (props.onClick) {
322359
props.onClick(e)
360+
if (e.defaultPrevented) return
323361
}
362+
array?.field?.moveUp(index)
363+
array?.props?.onMoveUp?.(index)
324364
}}
325-
/>
365+
icon={isUndef(props.icon) ? <UpOutlined /> : props.icon}
366+
>
367+
{props.title || self.title}
368+
</Button>
326369
)
327370
})
328371

0 commit comments

Comments
 (0)