buttonGroup.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import React, { isValidElement, cloneElement } from 'react';
  2. import BaseComponent, { BaseProps } from '../_base/baseComponent';
  3. import PropTypes from 'prop-types';
  4. import { cssClasses, strings } from '@douyinfe/semi-foundation/button/constants';
  5. import { Type, Size } from './Button';
  6. import '@douyinfe/semi-foundation/button/button.scss';
  7. export type Theme = 'solid' | 'borderless' | 'light';
  8. export interface ButtonGroupProps extends BaseProps {
  9. disabled?: boolean;
  10. type?: Type;
  11. size?: Size;
  12. theme?: Theme;
  13. 'aria-label'?: React.AriaAttributes['aria-label'];
  14. }
  15. const prefixCls = cssClasses.PREFIX;
  16. const btnSizes = strings.sizes;
  17. export default class ButtonGroup extends BaseComponent<ButtonGroupProps> {
  18. static propTypes = {
  19. children: PropTypes.node,
  20. disabled: PropTypes.bool,
  21. type: PropTypes.string,
  22. size: PropTypes.oneOf(btnSizes),
  23. theme: PropTypes.oneOf(strings.themes),
  24. 'aria-label': PropTypes.string,
  25. };
  26. static defaultProps = {
  27. size: 'default',
  28. type: 'primary',
  29. theme: 'light',
  30. };
  31. render() {
  32. const { children, disabled, size, type, 'aria-label': ariaLabel, ...rest } = this.props;
  33. let inner;
  34. if (children) {
  35. inner = ((Array.isArray(children) ? children : [children])).map((itm, index) => (
  36. isValidElement(itm)
  37. ? cloneElement(itm, { disabled, size, type, ...itm.props, ...rest, key: index })
  38. : itm
  39. ));
  40. }
  41. return <div className={`${prefixCls}-group`} role="group" aria-label={ariaLabel}>{inner}</div>;
  42. }
  43. }