ColumnSelection.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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-es';
  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. }
  21. /**
  22. * render selection cell
  23. */
  24. export default class TableSelectionCell extends BaseComponent<TableSelectionCellProps, Record<string, any>> {
  25. static propTypes = {
  26. columnTitle: PropTypes.string,
  27. getCheckboxProps: PropTypes.func,
  28. type: PropTypes.string,
  29. onChange: PropTypes.func,
  30. selected: PropTypes.bool,
  31. disabled: PropTypes.bool,
  32. indeterminate: PropTypes.bool,
  33. prefixCls: PropTypes.string,
  34. className: PropTypes.string,
  35. };
  36. static defaultProps = {
  37. disabled: false,
  38. onChange: noop,
  39. prefixCls: cssClasses.PREFIX,
  40. };
  41. get adapter(): TableSelectionCellAdapter {
  42. return {
  43. ...super.adapter,
  44. notifyChange: (...args) => this.props.onChange(...args),
  45. };
  46. }
  47. constructor(props: TableSelectionCellProps) {
  48. super(props);
  49. this.foundation = new TableSelectionCellFoundation(this.adapter);
  50. }
  51. handleChange = (e: CheckboxEvent) => this.foundation.handleChange(e);
  52. render() {
  53. const { selected, getCheckboxProps, indeterminate, disabled, prefixCls, className } = this.props;
  54. let checkboxProps = {
  55. onChange: this.handleChange,
  56. disabled,
  57. indeterminate,
  58. checked: selected,
  59. };
  60. if (typeof getCheckboxProps === 'function') {
  61. checkboxProps = { ...checkboxProps, ...getCheckboxProps() };
  62. }
  63. const wrapCls = classnames(
  64. `${prefixCls}-selection-wrap`,
  65. {
  66. [`${prefixCls}-selection-disabled`]: disabled,
  67. },
  68. className
  69. );
  70. return (
  71. <span className={wrapCls}>
  72. <Checkbox {...checkboxProps} />
  73. </span>
  74. );
  75. }
  76. }