1
0

buttonGroup.tsx 1.8 KB

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