-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathLineItemRemoveLink.tsx
More file actions
55 lines (51 loc) · 1.67 KB
/
LineItemRemoveLink.tsx
File metadata and controls
55 lines (51 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { useContext, type PropsWithoutRef, type JSX } from 'react';
import LineItemChildrenContext from '#context/LineItemChildrenContext'
import LineItemContext from '#context/LineItemContext'
import Parent from '#components/utils/Parent'
import type { ChildrenFunction } from '#typings/index'
import useCustomContext from '#utils/hooks/useCustomContext'
import type { LineItem } from '@commercelayer/sdk'
interface ChildrenProps extends Omit<Props, 'children'> {
handleRemove: (event: React.MouseEvent<HTMLAnchorElement>) => void
label?: string
lineItem?: LineItem
}
interface Props
extends PropsWithoutRef<Omit<JSX.IntrinsicElements['button'], 'children'>> {
children?: ChildrenFunction<ChildrenProps>
label?: string
}
export function LineItemRemoveLink(props: Props): JSX.Element {
const { label = 'Remove', onClick } = props
const { lineItem } = useCustomContext({
context: LineItemChildrenContext,
contextComponentName: 'LineItem',
currentComponentName: 'LineItemRemoveLink',
key: 'lineItem'
})
const { deleteLineItem } = useContext(LineItemContext)
const handleRemove = (e: React.MouseEvent<HTMLButtonElement>): void => {
e.preventDefault()
if (deleteLineItem != null && lineItem != null)
deleteLineItem(lineItem.id)
if (onClick != null) onClick(e)
}
const parentProps = {
handleRemove,
lineItem,
...props
}
return props.children ? (
<Parent {...parentProps}>{props.children}</Parent>
) : (
<button
data-testid={`line-item-remove-link-${lineItem?.sku_code ?? ''}`}
{...props}
type='button'
onClick={handleRemove}
>
{label}
</button>
)
}
export default LineItemRemoveLink