您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.tsx 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import type { CSSProperties } from 'react'
  2. import React from 'react'
  3. import { type VariantProps, cva } from 'class-variance-authority'
  4. import classNames from '@/utils/classnames'
  5. enum ActionButtonState {
  6. Destructive = 'destructive',
  7. Active = 'active',
  8. Disabled = 'disabled',
  9. Default = '',
  10. Hover = 'hover',
  11. }
  12. const actionButtonVariants = cva(
  13. 'action-btn',
  14. {
  15. variants: {
  16. size: {
  17. xs: 'action-btn-xs',
  18. m: 'action-btn-m',
  19. l: 'action-btn-l',
  20. xl: 'action-btn-xl',
  21. },
  22. },
  23. defaultVariants: {
  24. size: 'm',
  25. },
  26. },
  27. )
  28. export type ActionButtonProps = {
  29. size?: 'xs' | 's' | 'm' | 'l' | 'xl'
  30. state?: ActionButtonState
  31. styleCss?: CSSProperties
  32. ref?: React.Ref<HTMLButtonElement>
  33. } & React.ButtonHTMLAttributes<HTMLButtonElement> & VariantProps<typeof actionButtonVariants>
  34. function getActionButtonState(state: ActionButtonState) {
  35. switch (state) {
  36. case ActionButtonState.Destructive:
  37. return 'action-btn-destructive'
  38. case ActionButtonState.Active:
  39. return 'action-btn-active'
  40. case ActionButtonState.Disabled:
  41. return 'action-btn-disabled'
  42. case ActionButtonState.Hover:
  43. return 'action-btn-hover'
  44. default:
  45. return ''
  46. }
  47. }
  48. const ActionButton = ({ className, size, state = ActionButtonState.Default, styleCss, children, ref, ...props }: ActionButtonProps) => {
  49. return (
  50. <button
  51. type='button'
  52. className={classNames(
  53. actionButtonVariants({ className, size }),
  54. getActionButtonState(state),
  55. )}
  56. ref={ref}
  57. style={styleCss}
  58. {...props}
  59. >
  60. {children}
  61. </button>
  62. )
  63. }
  64. ActionButton.displayName = 'ActionButton'
  65. export default ActionButton
  66. export { ActionButton, ActionButtonState, actionButtonVariants }