-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathactionsheet.tsx
More file actions
112 lines (106 loc) · 3.07 KB
/
actionsheet.tsx
File metadata and controls
112 lines (106 loc) · 3.07 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React, { FunctionComponent } from 'react'
import { Close } from '@nutui/icons-react'
import Popup from '@/packages/popup/index'
import { ComponentDefaults } from '@/utils/typings'
import { mergeProps } from '@/utils/merge-props'
import { ActionSheetOption, WebActionSheetProps } from '@/types'
const defaultProps = {
...ComponentDefaults,
visible: false,
description: '',
options: [],
optionKey: { name: 'name', description: 'description' },
cancelText: '',
position: 'bottom',
onCancel: () => {},
onSelect: () => {},
} as unknown as WebActionSheetProps
export const ActionSheet: FunctionComponent<
Partial<WebActionSheetProps> &
Omit<React.HTMLAttributes<HTMLDivElement>, 'title' | 'onSelect'>
> = (props) => {
const {
children,
cancelText,
optionKey,
title,
description,
options,
onCancel,
onSelect,
visible,
className,
style,
position,
...rest
} = mergeProps(defaultProps, props)
const classPrefix = 'nut-actionsheet'
const cancelActionSheet = () => {
onCancel && onCancel()
}
const chooseItem = (
item: ActionSheetOption<string | boolean>,
index: number
) => {
if (!item.disabled) {
onSelect && onSelect(item, index)
}
}
return (
<Popup
{...rest}
title={title}
round
visible={visible}
position={position}
description={description}
className={`${classPrefix} ${classPrefix}-${position}`}
onClose={() => {
onCancel?.()
}}
closeable={position === 'top'}
closeIcon={<Close className={`${classPrefix}-close-icon`} />}
>
<div className={`${className}`} style={style}>
{/* {title && (
<div className={`${classPrefix}-${position}-title`}>{title}</div>
)} */}
{options.length ? (
<div className={`${classPrefix}-list`}>
{options.map((item, index) => {
const statusClass = `${item.disabled ? `${classPrefix}-item-disabled` : ''} ${item.danger ? `${classPrefix}-item-danger` : ''}`
return (
<div
className={`${classPrefix}-item ${statusClass} ${index !== options.length - 1 ? `${classPrefix}-item-border` : ''}`}
key={index}
onClick={() => chooseItem(item, index)}
>
<div className={`${classPrefix}-item-name ${statusClass}`}>
{item[optionKey.name]}
</div>
<div
className={`${classPrefix}-item-description ${statusClass}`}
>
{item[optionKey.description]}
</div>
</div>
)
})}
</div>
) : (
children
)}
{cancelText && (
<div
className={`${classPrefix}-cancel`}
onClick={() => cancelActionSheet()}
>
{cancelText}
</div>
)}
</div>
<div className={`${classPrefix}-safe-area`} />
</Popup>
)
}
ActionSheet.displayName = 'NutActionSheet'