|
1 | 1 | import PropTypes, { withUndefined } from '../_util/vue-types';
|
2 | 2 | import classNames from '../_util/classNames';
|
| 3 | +import { TransferLocale } from '.'; |
| 4 | +import DeleteOutlined from '@ant-design/icons-vue/DeleteOutlined'; |
| 5 | +import defaultLocale from '../locale/default'; |
3 | 6 | import Lazyload from '../vc-lazy-load';
|
4 | 7 | import Checkbox from '../checkbox';
|
5 |
| -import { defineComponent } from 'vue'; |
| 8 | +import TransButton from '../_util/transButton'; |
| 9 | +import LocaleReceiver from '../locale-provider/LocaleReceiver'; |
| 10 | +import { defineComponent, ExtractPropTypes } from 'vue'; |
6 | 11 |
|
7 | 12 | function noop() {}
|
8 | 13 |
|
| 14 | +export const transferListItemProps = { |
| 15 | + renderedText: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), |
| 16 | + renderedEl: PropTypes.any, |
| 17 | + item: PropTypes.any, |
| 18 | + lazy: withUndefined(PropTypes.oneOfType([PropTypes.looseBool, PropTypes.object])), |
| 19 | + checked: PropTypes.looseBool, |
| 20 | + prefixCls: PropTypes.string, |
| 21 | + disabled: PropTypes.looseBool, |
| 22 | + showRemove: PropTypes.looseBool, |
| 23 | + onClick: PropTypes.func, |
| 24 | + onRemove: PropTypes.func, |
| 25 | +}; |
| 26 | + |
| 27 | +export type TransferListItemProps = Partial<ExtractPropTypes<typeof transferListItemProps>>; |
| 28 | + |
9 | 29 | export default defineComponent({
|
10 | 30 | name: 'ListItem',
|
11 | 31 | inheritAttrs: false,
|
12 |
| - props: { |
13 |
| - renderedText: PropTypes.any, |
14 |
| - renderedEl: PropTypes.any, |
15 |
| - item: PropTypes.any, |
16 |
| - lazy: withUndefined(PropTypes.oneOfType([PropTypes.looseBool, PropTypes.object])), |
17 |
| - checked: PropTypes.looseBool, |
18 |
| - prefixCls: PropTypes.string, |
19 |
| - disabled: PropTypes.looseBool, |
20 |
| - onClick: PropTypes.func, |
21 |
| - }, |
22 |
| - render() { |
23 |
| - const { renderedText, renderedEl, item, lazy, checked, disabled, prefixCls } = this.$props; |
| 32 | + props: transferListItemProps, |
| 33 | + emits: ['click'], |
| 34 | + setup(props, { emit }) { |
| 35 | + return () => { |
| 36 | + const { |
| 37 | + renderedText, |
| 38 | + renderedEl, |
| 39 | + item, |
| 40 | + lazy, |
| 41 | + checked, |
| 42 | + disabled, |
| 43 | + prefixCls, |
| 44 | + showRemove, |
| 45 | + onRemove, |
| 46 | + } = props; |
| 47 | + const className = classNames({ |
| 48 | + [`${prefixCls}-content-item`]: true, |
| 49 | + [`${prefixCls}-content-item-disabled`]: disabled || item.disabled, |
| 50 | + }); |
24 | 51 |
|
25 |
| - const className = classNames({ |
26 |
| - [`${prefixCls}-content-item`]: true, |
27 |
| - [`${prefixCls}-content-item-disabled`]: disabled || item.disabled, |
28 |
| - }); |
| 52 | + let title: string; |
| 53 | + if (typeof renderedText === 'string' || typeof renderedText === 'number') { |
| 54 | + title = String(renderedText); |
| 55 | + } |
29 | 56 |
|
30 |
| - let title; |
31 |
| - if (typeof renderedText === 'string' || typeof renderedText === 'number') { |
32 |
| - title = String(renderedText); |
33 |
| - } |
| 57 | + const listItem = ( |
| 58 | + <LocaleReceiver componentName="Transfer" defaultLocale={defaultLocale.Transfer}> |
| 59 | + {(transferLocale: TransferLocale) => { |
| 60 | + const labelNode = <span class={`${prefixCls}-content-item-text`}>{renderedEl}</span>; |
| 61 | + if (showRemove) { |
| 62 | + return ( |
| 63 | + <li |
| 64 | + class={className} |
| 65 | + title={title} |
| 66 | + onClick={() => { |
| 67 | + onRemove?.(item); |
| 68 | + }} |
| 69 | + > |
| 70 | + {labelNode} |
| 71 | + <TransButton |
| 72 | + disabled={disabled || item.disabled} |
| 73 | + class={`${prefixCls}-content-item-remove`} |
| 74 | + aria-label={transferLocale.remove} |
| 75 | + > |
| 76 | + <DeleteOutlined /> |
| 77 | + </TransButton> |
| 78 | + </li> |
| 79 | + ); |
| 80 | + } |
34 | 81 |
|
35 |
| - const listItem = ( |
36 |
| - <li |
37 |
| - class={className} |
38 |
| - title={title} |
39 |
| - onClick={ |
40 |
| - disabled || item.disabled |
41 |
| - ? noop |
42 |
| - : () => { |
43 |
| - this.$emit('click', item); |
44 |
| - } |
45 |
| - } |
46 |
| - > |
47 |
| - <Checkbox checked={checked} disabled={disabled || item.disabled} /> |
48 |
| - <span class={`${prefixCls}-content-item-text`}>{renderedEl}</span> |
49 |
| - </li> |
50 |
| - ); |
51 |
| - let children = null; |
52 |
| - if (lazy) { |
53 |
| - const lazyProps = { |
54 |
| - height: 32, |
55 |
| - offset: 500, |
56 |
| - throttle: 0, |
57 |
| - debounce: false, |
58 |
| - ...(lazy as any), |
59 |
| - }; |
60 |
| - children = <Lazyload {...lazyProps}>{listItem}</Lazyload>; |
61 |
| - } else { |
62 |
| - children = listItem; |
63 |
| - } |
64 |
| - return children; |
| 82 | + return ( |
| 83 | + <li |
| 84 | + class={className} |
| 85 | + title={title} |
| 86 | + onClick={ |
| 87 | + disabled || item.disabled |
| 88 | + ? noop |
| 89 | + : () => { |
| 90 | + emit('click', item); |
| 91 | + } |
| 92 | + } |
| 93 | + > |
| 94 | + <Checkbox checked={checked} disabled={disabled || item.disabled} /> |
| 95 | + {labelNode} |
| 96 | + </li> |
| 97 | + ); |
| 98 | + }} |
| 99 | + </LocaleReceiver> |
| 100 | + ); |
| 101 | + let children = null; |
| 102 | + if (lazy) { |
| 103 | + const lazyProps = { |
| 104 | + height: 32, |
| 105 | + offset: 500, |
| 106 | + throttle: 0, |
| 107 | + debounce: false, |
| 108 | + ...(lazy as any), |
| 109 | + }; |
| 110 | + children = <Lazyload {...lazyProps}>{listItem}</Lazyload>; |
| 111 | + } else { |
| 112 | + children = listItem; |
| 113 | + } |
| 114 | + return children; |
| 115 | + }; |
65 | 116 | },
|
66 | 117 | });
|
0 commit comments