ColumnSelection.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import React from 'react';
  2. import BaseComponent from '../_base/baseComponent';
  3. import classnames from 'classnames';
  4. import PropTypes from 'prop-types';
  5. import { noop } from 'lodash';
  6. import { cssClasses } from '@douyinfe/semi-foundation/table/constants';
  7. import TableSelectionCellFoundation, { TableSelectionCellAdapter, TableSelectionCellEvent } from '@douyinfe/semi-foundation/table/tableSelectionCellFoundation';
  8. import { Checkbox, CheckboxEvent, CheckboxProps } from '../checkbox';
  9. export interface TableSelectionCellProps {
  10. columnTitle?: string; // TODO: future api
  11. getCheckboxProps?: () => CheckboxProps;
  12. type?: string; // TODO: future api
  13. onChange?: (checked: boolean, e: TableSelectionCellEvent) => void;
  14. selected?: boolean;
  15. disabled?: boolean;
  16. indeterminate?: boolean; // Intermediate state, shown as a solid horizontal line
  17. prefixCls?: string;
  18. className?: string;
  19. 'aria-label'?: React.AriaAttributes['aria-label']
  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. 'aria-label': PropTypes.string,
  36. };
  37. static defaultProps = {
  38. disabled: false,
  39. onChange: noop,
  40. prefixCls: cssClasses.PREFIX,
  41. };
  42. get adapter(): TableSelectionCellAdapter {
  43. return {
  44. ...super.adapter,
  45. notifyChange: (...args) => this.props.onChange(...args),
  46. };
  47. }
  48. foundation: TableSelectionCellFoundation;
  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. }