-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsplit-button.tsx
More file actions
235 lines (212 loc) · 7.43 KB
/
Copy pathsplit-button.tsx
File metadata and controls
235 lines (212 loc) · 7.43 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { useRef, forwardRef, useImperativeHandle, Ref, useEffect, useMemo, ButtonHTMLAttributes } from 'react';
import { Button, Position, Color, Size, Variant, IButton } from '@syncfusion/react-buttons';
import { preRender, useProviderContext } from '@syncfusion/react-base';
import { DropDownButton, IDropDownButton, ButtonSelectEvent, ItemModel, PopupSettings } from '../dropdown-button/dropdown-button';
import * as React from 'react';
/**
* Split Button properties used to customize its behavior and appearance.
*/
export interface SplitButtonProps {
/**
* Specifies an icon for the button, which can be defined using a CSS class name for custom styling or an SVG element for rendering.
*
* @default -
*/
icon?: React.ReactNode;
/**
* Specifies the position of the icon relative to the Dropdown Button text. Options include placing the icon at the left, right, top, or bottom of the button content.
*
* @default 'Left'
*/
iconPosition?: Position;
/**
* Specifies action items with its properties which will be rendered as Split Button secondary button popup.
*
* @default []
*/
items?: ItemModel[];
/**
* Specifies the popup element creation on open.
*
* @default false
*/
lazyOpen?: boolean;
/**
* Provides a template for displaying content within the dropdown items.
*
* @default -
*/
itemTemplate?: (item: ItemModel) => React.ReactNode;
/**
* Specifies the popup settings including position, offset, collision handling, animation, and target element configuration.
*
* @default {}
*/
popupSettings?: PopupSettings;
/**
* Specifies the color style of the Split Button. Options include 'Primary', 'Secondary', 'Warning', 'Success', 'Error' and 'Info'.
*
* @default Color.Primary
*/
color?: Color;
/**
* Specifies the variant style of the Split Button. Options include 'Outlined', 'Filled' and 'Standard'.
*
* @default Variant.Filled
*/
variant?: Variant;
/**
* Specifies the size style of the Split Button. Options include 'Small', 'Medium' and 'Large'.
*
* @default Size.Medium
*/
size?: Size;
/**
* Event triggered while closing the Split Button popup.
*
* @event onClose
*/
onClose?: (event?: React.SyntheticEvent) => void;
/**
* Event triggered while opening the Split Button popup.
*
* @event onOpen
*/
onOpen?: (event?: React.SyntheticEvent) => void;
/**
* Event triggered while selecting an action item in Split Button popup.
*
* @event onSelect
*/
onSelect?: (event: ButtonSelectEvent) => void;
/**
* Specifies additional properties to apply to the trailing dropdown button element.
* User-provided props will override internal default values.
*
* @default -
*/
trailingButtonProps?: Partial<IDropDownButton & React.ButtonHTMLAttributes<HTMLButtonElement>>;
}
/**
* Represents the methods of the Split Button component.
*/
export interface ISplitButton extends SplitButtonProps {
/**
* This is Split Button component element.
*
* @private
* @default null
*/
element?: HTMLElement | null;
/**
* To open/close Split Button popup based on current state of the Split Button.
*
* @public
* @returns {void}
*/
toggle?(): void;
}
type ISplitButtonProps = ISplitButton & Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'onSelect'>;
/**
* The Split Button component provides a combination of a default button action and a Dropdown Button, enabling users to quickly access additional options or actions in a compact interface.
*
* ```typescript
* import { SplitButton } from "@syncfusion/react-splitbuttons";
*
* const menuItems = [{ text: 'Cut' }, { text: 'Copy' }, { text: 'Paste' }];
* <SplitButton items={menuItems}>Default Action</SplitButton>
* ```
*/
export const SplitButton: React.ForwardRefExoticComponent<ISplitButtonProps & React.RefAttributes<ISplitButton>> =
forwardRef<ISplitButton, ISplitButtonProps>((props: ISplitButtonProps, ref: Ref<ISplitButton>) => {
const {
className = '',
icon,
iconPosition = Position.Left,
items = [],
disabled = false,
lazyOpen = false,
children,
itemTemplate,
popupSettings = {},
color,
variant,
size = Size.Medium,
onOpen,
onClose,
onSelect,
trailingButtonProps,
...domProps
} = props;
const buttonRef: React.RefObject<IButton | null> = useRef<IButton>(null);
const dropDownRef: React.RefObject<IDropDownButton | null> = useRef<IDropDownButton>(null);
const wrapperRef: React.RefObject<HTMLDivElement | null> = useRef<HTMLDivElement | null>(null);
const { dir } = useProviderContext();
const publicAPI: Partial<ISplitButton> = useMemo(() => ({
iconPosition,
icon,
items,
lazyOpen,
itemTemplate,
popupSettings,
color,
variant,
size
}), [iconPosition, icon, items, lazyOpen, itemTemplate, popupSettings, color, variant, size]);
useEffect(() => {
preRender('splitButton');
}, []);
useImperativeHandle(ref, () => ({
...publicAPI as ISplitButton,
toggle: () => {
if (dropDownRef.current && dropDownRef.current.toggle) {
dropDownRef.current.toggle();
}
},
element: wrapperRef.current
}), [publicAPI]);
const wrapperClassName: string = [
'sf-split-btn-wrapper',
size ? `sf-split-btn-${String(size).toLowerCase().substring(0, 2)}` : '',
variant ? `sf-${String(variant).toLowerCase()}` : '',
dir === 'rtl' ? 'sf-rtl' : ''
].filter(Boolean).join(' ');
return (
<div ref={wrapperRef} className={wrapperClassName}>
<Button
ref={buttonRef}
className={`${className} ${(dir === 'rtl') ? 'sf-rtl' : ''} sf-control sf-btn sf-split-btn`}
type="button"
icon={icon}
color={color}
variant={variant}
size={size}
iconPosition={iconPosition}
disabled={disabled}
{...domProps}
>
{children}
</Button>
<DropDownButton
ref={dropDownRef}
className={`${className} ${(dir === 'rtl') ? 'sf-rtl' : ''} sf-icon-btn sf-control sf-dropdown-btn sf-btn`}
items={items}
color={color}
variant={variant}
size={size}
itemTemplate={itemTemplate}
disabled={disabled}
lazyOpen={lazyOpen}
popupSettings={{
...popupSettings
}}
onOpen={onOpen}
onClose={onClose}
onSelect={onSelect}
{...trailingButtonProps}
>
</DropDownButton>
</div>
);
});
export default SplitButton;