sort-transfer.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 }
  14. const Item = ({ value, append, checked, onChange }: ItemProps) => {
  15. return (
  16. <li className={style['sort-item'] + ' ' + (append ? style["sort-bar-parent"] : '')}>
  17. <Checkbox checked={checked} onChange={(e) => onChange(e.target.checked)}>
  18. {value.tagTitle}
  19. </Checkbox>
  20. { append && <span className={style['sort-bar']} children={append} /> }
  21. </li>
  22. )
  23. }
  24. const SortLayout = SortableContainer(Layout)
  25. const SortDrag = SortableHandle(() => <i className="iconfont icon-order" />);
  26. const SortItem = SortableElement((props: Omit<ItemProps, 'append'>) => <Item {...props} append={<SortDrag />} />)
  27. interface DataType {
  28. key: string;
  29. data: Tagging
  30. }
  31. interface TableTransferProps extends TransferProps<TransferItem & DataType> {
  32. dataSource: DataType[];
  33. onChangeSort?: (data: DataType[]) => void
  34. }
  35. // Customize Table Transfer
  36. export const SortTransfer = ({ onChangeSort, ...restProps }: TableTransferProps) => (
  37. <Transfer {...restProps}>
  38. {({
  39. direction,
  40. filteredItems,
  41. onItemSelect,
  42. selectedKeys: listSelectedKeys,
  43. }) => {
  44. const SLayout = direction === 'left' ? Layout : SortLayout
  45. const SItem = direction === 'left' ? Item : SortItem
  46. return (
  47. <SLayout
  48. onSortEnd={(data) => onChangeSort && onChangeSort(arrayMove(filteredItems, data.oldIndex, data.newIndex))}
  49. useDragHandle
  50. children={
  51. filteredItems.map((item, index) => (
  52. <SItem
  53. value={item.data}
  54. key={item.data.tagId}
  55. index={index}
  56. checked={listSelectedKeys.some(key => key === item.key)}
  57. onChange={(check) => onItemSelect(item.key, check)}
  58. />
  59. ))
  60. }
  61. />
  62. )
  63. }}
  64. </Transfer>
  65. );