ColumnSelection.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. ariaLabel?: string;
  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. ariaLabel: 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, ariaLabel } = this.props;
  56. let checkboxProps = {
  57. onChange: this.handleChange,
  58. disabled,
  59. indeterminate,
  60. checked: selected,
  61. };
  62. if (typeof getCheckboxProps === 'function') {
  63. checkboxProps = { ...checkboxProps, ...getCheckboxProps() };
  64. }
  65. const wrapCls = classnames(
  66. `${prefixCls}-selection-wrap`,
  67. {
  68. [`${prefixCls}-selection-disabled`]: disabled,
  69. },
  70. className
  71. );
  72. return (
  73. <span className={wrapCls}>
  74. <Checkbox ariaLabel={ariaLabel} {...checkboxProps} />
  75. </span>
  76. );
  77. }
  78. }