| 
									
										
										
										
											2024-07-24 18:09:44 +08:00
										 |  |  | import type { CSSProperties } from 'react' | 
					
						
							|  |  |  | import React from 'react' | 
					
						
							|  |  |  | import { type VariantProps, cva } from 'class-variance-authority' | 
					
						
							|  |  |  | import classNames from '@/utils/classnames' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum ActionButtonState { | 
					
						
							|  |  |  |   Destructive = 'destructive', | 
					
						
							|  |  |  |   Active = 'active', | 
					
						
							|  |  |  |   Disabled = 'disabled', | 
					
						
							|  |  |  |   Default = '', | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |   Hover = 'hover', | 
					
						
							| 
									
										
										
										
											2024-07-24 18:09:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const actionButtonVariants = cva( | 
					
						
							|  |  |  |   'action-btn', | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     variants: { | 
					
						
							|  |  |  |       size: { | 
					
						
							|  |  |  |         xs: 'action-btn-xs', | 
					
						
							|  |  |  |         m: 'action-btn-m', | 
					
						
							|  |  |  |         l: 'action-btn-l', | 
					
						
							|  |  |  |         xl: 'action-btn-xl', | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     defaultVariants: { | 
					
						
							|  |  |  |       size: 'm', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export type ActionButtonProps = { | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |   size?: 'xs' | 's' | 'm' | 'l' | 'xl' | 
					
						
							| 
									
										
										
										
											2024-07-24 18:09:44 +08:00
										 |  |  |   state?: ActionButtonState | 
					
						
							|  |  |  |   styleCss?: CSSProperties | 
					
						
							|  |  |  | } & React.ButtonHTMLAttributes<HTMLButtonElement> & VariantProps<typeof actionButtonVariants> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getActionButtonState(state: ActionButtonState) { | 
					
						
							|  |  |  |   switch (state) { | 
					
						
							|  |  |  |     case ActionButtonState.Destructive: | 
					
						
							|  |  |  |       return 'action-btn-destructive' | 
					
						
							|  |  |  |     case ActionButtonState.Active: | 
					
						
							|  |  |  |       return 'action-btn-active' | 
					
						
							|  |  |  |     case ActionButtonState.Disabled: | 
					
						
							|  |  |  |       return 'action-btn-disabled' | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |     case ActionButtonState.Hover: | 
					
						
							|  |  |  |       return 'action-btn-hover' | 
					
						
							| 
									
										
										
										
											2024-07-24 18:09:44 +08:00
										 |  |  |     default: | 
					
						
							|  |  |  |       return '' | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ActionButton = React.forwardRef<HTMLButtonElement, ActionButtonProps>( | 
					
						
							|  |  |  |   ({ className, size, state = ActionButtonState.Default, styleCss, children, ...props }, ref) => { | 
					
						
							|  |  |  |     return ( | 
					
						
							|  |  |  |       <button | 
					
						
							|  |  |  |         type='button' | 
					
						
							|  |  |  |         className={classNames( | 
					
						
							|  |  |  |           actionButtonVariants({ className, size }), | 
					
						
							|  |  |  |           getActionButtonState(state), | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |         ref={ref} | 
					
						
							|  |  |  |         style={styleCss} | 
					
						
							|  |  |  |         {...props} | 
					
						
							|  |  |  |       > | 
					
						
							|  |  |  |         {children} | 
					
						
							|  |  |  |       </button> | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | ActionButton.displayName = 'ActionButton' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default ActionButton | 
					
						
							|  |  |  | export { ActionButton, ActionButtonState, actionButtonVariants } |