ColumnSelection.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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?: (checked: boolean, 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. foundation: TableSelectionCellFoundation;
  50. constructor(props: TableSelectionCellProps) {
  51. super(props);
  52. this.foundation = new TableSelectionCellFoundation(this.adapter);
  53. }
  54. handleChange = (e: CheckboxEvent) => this.foundation.handleChange(e);
  55. render() {
  56. const { selected, getCheckboxProps, indeterminate, disabled, prefixCls, className } = this.props;
  57. const ariaLabel = this.props['aria-label'];
  58. let checkboxProps = {
  59. onChange: this.handleChange,
  60. disabled,
  61. indeterminate,
  62. checked: selected,
  63. };
  64. if (typeof getCheckboxProps === 'function') {
  65. checkboxProps = { ...checkboxProps, ...getCheckboxProps() };
  66. }
  67. const wrapCls = classnames(
  68. `${prefixCls}-selection-wrap`,
  69. {
  70. [`${prefixCls}-selection-disabled`]: disabled,
  71. },
  72. className
  73. );
  74. return (
  75. <span className={wrapCls}>
  76. <Checkbox aria-label={ariaLabel} {...checkboxProps} />
  77. </span>
  78. );
  79. }
  80. }