ColumnSelection.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* eslint-disable max-len */
  2. import React from 'react';
  3. import BaseComponent from '../_base/baseComponent';
  4. import classnames from 'classnames';
  5. import PropTypes from 'prop-types';
  6. import { noop } from 'lodash';
  7. import { cssClasses } from '@douyinfe/semi-foundation/table/constants';
  8. import TableSelectionCellFoundation, { TableSelectionCellAdapter, TableSelectionCellEvent } from '@douyinfe/semi-foundation/table/tableSelectionCellFoundation';
  9. import { Checkbox, CheckboxEvent, CheckboxProps } from '../checkbox';
  10. export interface TableSelectionCellProps {
  11. columnTitle?: string; // TODO: future api
  12. getCheckboxProps?: () => CheckboxProps;
  13. type?: string; // TODO: future api
  14. onChange?: (value: any, e: TableSelectionCellEvent) => void;
  15. selected?: boolean;
  16. disabled?: boolean;
  17. indeterminate?: boolean; // Intermediate state, shown as a solid horizontal line
  18. prefixCls?: string;
  19. className?: string;
  20. 'aria-label'?: React.AriaAttributes['aria-label'];
  21. }
  22. /**
  23. * render selection cell
  24. */
  25. export default class TableSelectionCell extends BaseComponent<TableSelectionCellProps, Record<string, any>> {
  26. static propTypes = {
  27. columnTitle: PropTypes.string,
  28. getCheckboxProps: PropTypes.func,
  29. type: PropTypes.string,
  30. onChange: PropTypes.func,
  31. selected: PropTypes.bool,
  32. disabled: PropTypes.bool,
  33. indeterminate: PropTypes.bool,
  34. prefixCls: PropTypes.string,
  35. className: PropTypes.string,
  36. 'aria-label': PropTypes.string,
  37. };
  38. static defaultProps = {
  39. disabled: false,
  40. onChange: noop,
  41. prefixCls: cssClasses.PREFIX,
  42. };
  43. get adapter(): TableSelectionCellAdapter {
  44. return {
  45. ...super.adapter,
  46. notifyChange: (...args) => this.props.onChange(...args),
  47. };
  48. }
  49. constructor(props: TableSelectionCellProps) {
  50. super(props);
  51. this.foundation = new TableSelectionCellFoundation(this.adapter);
  52. }
  53. handleChange = (e: CheckboxEvent) => this.foundation.handleChange(e);
  54. render() {
  55. const { selected, getCheckboxProps, indeterminate, disabled, prefixCls, className } = this.props;
  56. const ariaLabel = this.props['aria-label'];
  57. let checkboxProps = {
  58. onChange: this.handleChange,
  59. disabled,
  60. indeterminate,
  61. checked: selected,
  62. };
  63. if (typeof getCheckboxProps === 'function') {
  64. checkboxProps = { ...checkboxProps, ...getCheckboxProps() };
  65. }
  66. const wrapCls = classnames(
  67. `${prefixCls}-selection-wrap`,
  68. {
  69. [`${prefixCls}-selection-disabled`]: disabled,
  70. },
  71. className
  72. );
  73. return (
  74. <span className={wrapCls}>
  75. <Checkbox aria-label={ariaLabel} {...checkboxProps} />
  76. </span>
  77. );
  78. }
  79. }