index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  2. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  3. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  4. import { createVNode as _createVNode, resolveDirective as _resolveDirective } from "vue";
  5. import { watchEffect, defineComponent, ref, watch, toRaw } from 'vue';
  6. import PropTypes from '../_util/vue-types';
  7. import { getPropsSlot } from '../_util/props-util';
  8. import classNames from '../_util/classNames';
  9. import List from './list';
  10. import Operation from './operation';
  11. import LocaleReceiver from '../locale-provider/LocaleReceiver';
  12. import defaultLocale from '../locale-provider/default';
  13. import { withInstall } from '../_util/type';
  14. import useConfigInject from '../_util/hooks/useConfigInject';
  15. import { useInjectFormItemContext } from '../form/FormItemContext';
  16. export var transferProps = function transferProps() {
  17. return {
  18. id: String,
  19. prefixCls: String,
  20. dataSource: {
  21. type: Array,
  22. default: []
  23. },
  24. disabled: {
  25. type: Boolean,
  26. default: undefined
  27. },
  28. targetKeys: {
  29. type: Array,
  30. default: undefined
  31. },
  32. selectedKeys: {
  33. type: Array,
  34. default: undefined
  35. },
  36. render: {
  37. type: Function
  38. },
  39. listStyle: {
  40. type: [Function, Object],
  41. default: function _default() {
  42. return {};
  43. }
  44. },
  45. operationStyle: {
  46. type: Object,
  47. default: undefined
  48. },
  49. titles: {
  50. type: Array
  51. },
  52. operations: {
  53. type: Array
  54. },
  55. showSearch: {
  56. type: Boolean,
  57. default: false
  58. },
  59. filterOption: {
  60. type: Function
  61. },
  62. searchPlaceholder: String,
  63. notFoundContent: PropTypes.any,
  64. locale: {
  65. type: Object,
  66. default: function _default() {
  67. return {};
  68. }
  69. },
  70. rowKey: {
  71. type: Function
  72. },
  73. showSelectAll: {
  74. type: Boolean,
  75. default: undefined
  76. },
  77. selectAllLabels: {
  78. type: Array
  79. },
  80. children: {
  81. type: Function
  82. },
  83. oneWay: {
  84. type: Boolean,
  85. default: undefined
  86. },
  87. pagination: {
  88. type: [Object, Boolean],
  89. default: undefined
  90. },
  91. onChange: Function,
  92. onSelectChange: Function,
  93. onSearch: Function,
  94. onScroll: Function,
  95. 'onUpdate:targetKeys': Function,
  96. 'onUpdate:selectedKeys': Function
  97. };
  98. };
  99. var Transfer = defineComponent({
  100. compatConfig: {
  101. MODE: 3
  102. },
  103. name: 'ATransfer',
  104. inheritAttrs: false,
  105. props: transferProps(),
  106. slots: ['leftTitle', 'rightTitle', 'children', 'render', 'notFoundContent', 'leftSelectAllLabel', 'rightSelectAllLabel', 'footer'],
  107. // emits: ['update:targetKeys', 'update:selectedKeys', 'change', 'search', 'scroll', 'selectChange'],
  108. setup: function setup(props, _ref) {
  109. var emit = _ref.emit,
  110. attrs = _ref.attrs,
  111. slots = _ref.slots,
  112. expose = _ref.expose;
  113. var _useConfigInject = useConfigInject('transfer', props),
  114. configProvider = _useConfigInject.configProvider,
  115. prefixCls = _useConfigInject.prefixCls,
  116. direction = _useConfigInject.direction;
  117. var sourceSelectedKeys = ref([]);
  118. var targetSelectedKeys = ref([]);
  119. var formItemContext = useInjectFormItemContext();
  120. watch(function () {
  121. return props.selectedKeys;
  122. }, function () {
  123. var _props$selectedKeys, _props$selectedKeys2;
  124. sourceSelectedKeys.value = ((_props$selectedKeys = props.selectedKeys) === null || _props$selectedKeys === void 0 ? void 0 : _props$selectedKeys.filter(function (key) {
  125. return props.targetKeys.indexOf(key) === -1;
  126. })) || [];
  127. targetSelectedKeys.value = ((_props$selectedKeys2 = props.selectedKeys) === null || _props$selectedKeys2 === void 0 ? void 0 : _props$selectedKeys2.filter(function (key) {
  128. return props.targetKeys.indexOf(key) > -1;
  129. })) || [];
  130. }, {
  131. immediate: true
  132. });
  133. var getLocale = function getLocale(transferLocale, renderEmpty) {
  134. // Keep old locale props still working.
  135. var oldLocale = {
  136. notFoundContent: renderEmpty('Transfer')
  137. };
  138. var notFoundContent = getPropsSlot(slots, props, 'notFoundContent');
  139. if (notFoundContent) {
  140. oldLocale.notFoundContent = notFoundContent;
  141. }
  142. if (props.searchPlaceholder !== undefined) {
  143. oldLocale.searchPlaceholder = props.searchPlaceholder;
  144. }
  145. return _objectSpread(_objectSpread(_objectSpread({}, transferLocale), oldLocale), props.locale);
  146. };
  147. var moveTo = function moveTo(direction) {
  148. var _props$targetKeys = props.targetKeys,
  149. targetKeys = _props$targetKeys === void 0 ? [] : _props$targetKeys,
  150. _props$dataSource = props.dataSource,
  151. dataSource = _props$dataSource === void 0 ? [] : _props$dataSource;
  152. var moveKeys = direction === 'right' ? sourceSelectedKeys.value : targetSelectedKeys.value;
  153. // filter the disabled options
  154. var newMoveKeys = moveKeys.filter(function (key) {
  155. return !dataSource.some(function (data) {
  156. return !!(key === data.key && data.disabled);
  157. });
  158. });
  159. // move items to target box
  160. var newTargetKeys = direction === 'right' ? newMoveKeys.concat(targetKeys) : targetKeys.filter(function (targetKey) {
  161. return newMoveKeys.indexOf(targetKey) === -1;
  162. });
  163. // empty checked keys
  164. var oppositeDirection = direction === 'right' ? 'left' : 'right';
  165. direction === 'right' ? sourceSelectedKeys.value = [] : targetSelectedKeys.value = [];
  166. emit('update:targetKeys', newTargetKeys);
  167. handleSelectChange(oppositeDirection, []);
  168. emit('change', newTargetKeys, direction, newMoveKeys);
  169. formItemContext.onFieldChange();
  170. };
  171. var moveToLeft = function moveToLeft() {
  172. moveTo('left');
  173. };
  174. var moveToRight = function moveToRight() {
  175. moveTo('right');
  176. };
  177. var onItemSelectAll = function onItemSelectAll(direction, selectedKeys) {
  178. handleSelectChange(direction, selectedKeys);
  179. };
  180. var onLeftItemSelectAll = function onLeftItemSelectAll(selectedKeys) {
  181. return onItemSelectAll('left', selectedKeys);
  182. };
  183. var onRightItemSelectAll = function onRightItemSelectAll(selectedKeys) {
  184. return onItemSelectAll('right', selectedKeys);
  185. };
  186. var handleSelectChange = function handleSelectChange(direction, holder) {
  187. if (direction === 'left') {
  188. if (!props.selectedKeys) {
  189. sourceSelectedKeys.value = holder;
  190. }
  191. emit('update:selectedKeys', [].concat(_toConsumableArray(holder), _toConsumableArray(targetSelectedKeys.value)));
  192. emit('selectChange', holder, toRaw(targetSelectedKeys.value));
  193. } else {
  194. if (!props.selectedKeys) {
  195. targetSelectedKeys.value = holder;
  196. }
  197. emit('update:selectedKeys', [].concat(_toConsumableArray(holder), _toConsumableArray(sourceSelectedKeys.value)));
  198. emit('selectChange', toRaw(sourceSelectedKeys.value), holder);
  199. }
  200. };
  201. var handleFilter = function handleFilter(direction, e) {
  202. var value = e.target.value;
  203. emit('search', direction, value);
  204. };
  205. var handleLeftFilter = function handleLeftFilter(e) {
  206. handleFilter('left', e);
  207. };
  208. var handleRightFilter = function handleRightFilter(e) {
  209. handleFilter('right', e);
  210. };
  211. var handleClear = function handleClear(direction) {
  212. emit('search', direction, '');
  213. };
  214. var handleLeftClear = function handleLeftClear() {
  215. handleClear('left');
  216. };
  217. var handleRightClear = function handleRightClear() {
  218. handleClear('right');
  219. };
  220. var onItemSelect = function onItemSelect(direction, selectedKey, checked) {
  221. var holder = direction === 'left' ? _toConsumableArray(sourceSelectedKeys.value) : _toConsumableArray(targetSelectedKeys.value);
  222. var index = holder.indexOf(selectedKey);
  223. if (index > -1) {
  224. holder.splice(index, 1);
  225. }
  226. if (checked) {
  227. holder.push(selectedKey);
  228. }
  229. handleSelectChange(direction, holder);
  230. };
  231. var onLeftItemSelect = function onLeftItemSelect(selectedKey, checked) {
  232. return onItemSelect('left', selectedKey, checked);
  233. };
  234. var onRightItemSelect = function onRightItemSelect(selectedKey, checked) {
  235. return onItemSelect('right', selectedKey, checked);
  236. };
  237. var onRightItemRemove = function onRightItemRemove(targetedKeys) {
  238. var _props$targetKeys2 = props.targetKeys,
  239. targetKeys = _props$targetKeys2 === void 0 ? [] : _props$targetKeys2;
  240. var newTargetKeys = targetKeys.filter(function (key) {
  241. return !targetedKeys.includes(key);
  242. });
  243. emit('update:targetKeys', newTargetKeys);
  244. emit('change', newTargetKeys, 'left', _toConsumableArray(targetedKeys));
  245. };
  246. var handleScroll = function handleScroll(direction, e) {
  247. emit('scroll', direction, e);
  248. };
  249. var handleLeftScroll = function handleLeftScroll(e) {
  250. handleScroll('left', e);
  251. };
  252. var handleRightScroll = function handleRightScroll(e) {
  253. handleScroll('right', e);
  254. };
  255. var handleListStyle = function handleListStyle(listStyle, direction) {
  256. if (typeof listStyle === 'function') {
  257. return listStyle({
  258. direction: direction
  259. });
  260. }
  261. return listStyle;
  262. };
  263. var leftDataSource = ref([]);
  264. var rightDataSource = ref([]);
  265. watchEffect(function () {
  266. var dataSource = props.dataSource,
  267. rowKey = props.rowKey,
  268. _props$targetKeys3 = props.targetKeys,
  269. targetKeys = _props$targetKeys3 === void 0 ? [] : _props$targetKeys3;
  270. var ld = [];
  271. var rd = new Array(targetKeys.length);
  272. dataSource.forEach(function (record) {
  273. if (rowKey) {
  274. record.key = rowKey(record);
  275. }
  276. // rightDataSource should be ordered by targetKeys
  277. // leftDataSource should be ordered by dataSource
  278. var indexOfKey = targetKeys.indexOf(record.key);
  279. if (indexOfKey !== -1) {
  280. rd[indexOfKey] = record;
  281. } else {
  282. ld.push(record);
  283. }
  284. });
  285. leftDataSource.value = ld;
  286. rightDataSource.value = rd;
  287. });
  288. expose({
  289. handleSelectChange: handleSelectChange
  290. });
  291. var renderTransfer = function renderTransfer(transferLocale) {
  292. var _classNames, _ref2, _ref3, _slots$leftTitle, _ref4, _ref5, _slots$rightTitle;
  293. var disabled = props.disabled,
  294. _props$operations = props.operations,
  295. operations = _props$operations === void 0 ? [] : _props$operations,
  296. showSearch = props.showSearch,
  297. listStyle = props.listStyle,
  298. operationStyle = props.operationStyle,
  299. filterOption = props.filterOption,
  300. showSelectAll = props.showSelectAll,
  301. _props$selectAllLabel = props.selectAllLabels,
  302. selectAllLabels = _props$selectAllLabel === void 0 ? [] : _props$selectAllLabel,
  303. oneWay = props.oneWay,
  304. pagination = props.pagination,
  305. _props$id = props.id,
  306. id = _props$id === void 0 ? formItemContext.id.value : _props$id;
  307. var className = attrs.class,
  308. style = attrs.style;
  309. var children = slots.children;
  310. var mergedPagination = !children && pagination;
  311. var renderEmpty = configProvider.renderEmpty;
  312. var locale = getLocale(transferLocale, renderEmpty);
  313. var footer = slots.footer;
  314. var renderItem = props.render || slots.render;
  315. var leftActive = targetSelectedKeys.value.length > 0;
  316. var rightActive = sourceSelectedKeys.value.length > 0;
  317. var cls = classNames(prefixCls.value, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls.value, "-disabled"), disabled), _defineProperty(_classNames, "".concat(prefixCls.value, "-customize-list"), !!children), _classNames));
  318. var titles = props.titles;
  319. var leftTitle = (_ref2 = (_ref3 = titles && titles[0]) !== null && _ref3 !== void 0 ? _ref3 : (_slots$leftTitle = slots.leftTitle) === null || _slots$leftTitle === void 0 ? void 0 : _slots$leftTitle.call(slots)) !== null && _ref2 !== void 0 ? _ref2 : (locale.titles || ['', ''])[0];
  320. var rightTitle = (_ref4 = (_ref5 = titles && titles[1]) !== null && _ref5 !== void 0 ? _ref5 : (_slots$rightTitle = slots.rightTitle) === null || _slots$rightTitle === void 0 ? void 0 : _slots$rightTitle.call(slots)) !== null && _ref4 !== void 0 ? _ref4 : (locale.titles || ['', ''])[1];
  321. return _createVNode("div", {
  322. "class": cls,
  323. "style": style,
  324. "id": id
  325. }, [_createVNode(List, _objectSpread({
  326. "key": "leftList",
  327. "prefixCls": "".concat(prefixCls.value, "-list"),
  328. "dataSource": leftDataSource.value,
  329. "filterOption": filterOption,
  330. "style": handleListStyle(listStyle, 'left'),
  331. "checkedKeys": sourceSelectedKeys.value,
  332. "handleFilter": handleLeftFilter,
  333. "handleClear": handleLeftClear,
  334. "onItemSelect": onLeftItemSelect,
  335. "onItemSelectAll": onLeftItemSelectAll,
  336. "renderItem": renderItem,
  337. "showSearch": showSearch,
  338. "renderList": children,
  339. "onScroll": handleLeftScroll,
  340. "disabled": disabled,
  341. "direction": direction.value === 'rtl' ? 'right' : 'left',
  342. "showSelectAll": showSelectAll,
  343. "selectAllLabel": selectAllLabels[0] || slots.leftSelectAllLabel,
  344. "pagination": mergedPagination
  345. }, locale), {
  346. titleText: function titleText() {
  347. return leftTitle;
  348. },
  349. footer: footer
  350. }), _createVNode(Operation, {
  351. "key": "operation",
  352. "class": "".concat(prefixCls.value, "-operation"),
  353. "rightActive": rightActive,
  354. "rightArrowText": operations[0],
  355. "moveToRight": moveToRight,
  356. "leftActive": leftActive,
  357. "leftArrowText": operations[1],
  358. "moveToLeft": moveToLeft,
  359. "style": operationStyle,
  360. "disabled": disabled,
  361. "direction": direction.value,
  362. "oneWay": oneWay
  363. }, null), _createVNode(List, _objectSpread({
  364. "key": "rightList",
  365. "prefixCls": "".concat(prefixCls.value, "-list"),
  366. "dataSource": rightDataSource.value,
  367. "filterOption": filterOption,
  368. "style": handleListStyle(listStyle, 'right'),
  369. "checkedKeys": targetSelectedKeys.value,
  370. "handleFilter": handleRightFilter,
  371. "handleClear": handleRightClear,
  372. "onItemSelect": onRightItemSelect,
  373. "onItemSelectAll": onRightItemSelectAll,
  374. "onItemRemove": onRightItemRemove,
  375. "renderItem": renderItem,
  376. "showSearch": showSearch,
  377. "renderList": children,
  378. "onScroll": handleRightScroll,
  379. "disabled": disabled,
  380. "direction": direction.value === 'rtl' ? 'left' : 'right',
  381. "showSelectAll": showSelectAll,
  382. "selectAllLabel": selectAllLabels[1] || slots.rightSelectAllLabel,
  383. "showRemove": oneWay,
  384. "pagination": mergedPagination
  385. }, locale), {
  386. titleText: function titleText() {
  387. return rightTitle;
  388. },
  389. footer: footer
  390. })]);
  391. };
  392. return function () {
  393. return _createVNode(LocaleReceiver, {
  394. "componentName": "Transfer",
  395. "defaultLocale": defaultLocale.Transfer,
  396. "children": renderTransfer
  397. }, null);
  398. };
  399. }
  400. });
  401. export default withInstall(Transfer);