sort-transfer.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { Checkbox, Transfer } from 'antd';
  2. import {
  3. SortableContainer,
  4. SortableElement,
  5. SortableHandle,
  6. arrayMove,
  7. } from 'react-sortable-hoc';
  8. import style from './style.module.scss'
  9. import type { Tagging } from 'api';
  10. import type { TransferItem, TransferProps } from 'antd/es/transfer';
  11. import type { ReactElement } from 'react';
  12. const Layout = ({children}: {children: any}) => <ul className={style['tagging-transfer']}>{children}</ul>;
  13. type ItemProps = { value: Tagging, checked: boolean, onChange: (checked: boolean) => void, append?: ReactElement, showIndex: number }
  14. const Item = ({ value, append, checked, onChange, showIndex }: ItemProps) => {
  15. return (
  16. <li className={style['sort-item'] + ' ' + (append ? style["sort-bar-parent"] : '')}>
  17. <span>{ showIndex + 1 }. </span>
  18. <Checkbox checked={checked} onChange={(e) => onChange(e.target.checked)}>
  19. {value.tagTitle}
  20. </Checkbox>
  21. { append && <span className={style['sort-bar']} children={append} /> }
  22. </li>
  23. )
  24. }
  25. const SortLayout = SortableContainer(Layout)
  26. const SortDrag = SortableHandle(() => <i className="iconfont icon-order" />);
  27. const SortItem = SortableElement((props: Omit<ItemProps, 'append'>) => <Item {...props} append={<SortDrag />} />)
  28. interface DataType {
  29. key: string;
  30. data: Tagging
  31. }
  32. interface TableTransferProps extends TransferProps<TransferItem & DataType> {
  33. dataSource: DataType[];
  34. onChangeSort?: (data: DataType[]) => void
  35. }
  36. // Customize Table Transfer
  37. export const SortTransfer = ({ onChangeSort, ...restProps }: TableTransferProps) => (
  38. <Transfer {...restProps}>
  39. {({
  40. direction,
  41. filteredItems,
  42. onItemSelect,
  43. selectedKeys: listSelectedKeys,
  44. }) => {
  45. const SLayout = direction === 'left' ? Layout : SortLayout
  46. const SItem = direction === 'left' ? Item : SortItem
  47. return (
  48. <SLayout
  49. onSortEnd={(data) => onChangeSort && onChangeSort(arrayMove(filteredItems, data.oldIndex, data.newIndex))}
  50. useDragHandle
  51. children={
  52. filteredItems.map((item, index) => (
  53. <SItem
  54. value={item.data}
  55. key={item.data.tagId}
  56. index={index}
  57. showIndex={index}
  58. checked={listSelectedKeys.some(key => key === item.key)}
  59. onChange={(check) => onItemSelect(item.key, check)}
  60. />
  61. ))
  62. }
  63. />
  64. )
  65. }}
  66. </Transfer>
  67. );