ant-design/components/transfer/ListItem.tsx
kyriewen 5a45276565
feat: Add useMultipleSelect hooks, transfer supports multiple select by holding down the shift key. (#45285)
* feat: add useMultipleSelect hooks

* docs: update README

* chore: update size limit

* refactor:  prevSelectedIndex ref to state、initial value

* refactor: useMultipleSelect

* refactor: reset last select key after deselect

* chore: useMultipleSelect params type

* refactor: should params key be in useMultipleSelect hooks

* feat: getKey must be required

* chore: useless code

* refactor: remove useless code

* feat: onItemSelect params event must be required

* feat: remove params event

* chore: remove onItemSelect params event; refactor transfer listBody onClick fnName

* revert(modal): revert modal demo dark

* refactor: props onItemSelect

* refactor: onItemSelect fn name

* refactor: transfer children type

* refactor: renderList props

* test: add test case

* chore: args -> arguments

* test(transfer): add test case

---------

Signed-off-by: kyriewen <64134946+zbw-zbw@users.noreply.github.com>
Co-authored-by: zbw01218944 <zbw01218944@alibaba-inc.com>
2023-10-26 11:46:22 +08:00

85 lines
2.2 KiB
TypeScript

import DeleteOutlined from '@ant-design/icons/DeleteOutlined';
import classNames from 'classnames';
import * as React from 'react';
import type { KeyWiseTransferItem } from '.';
import TransButton from '../_util/transButton';
import Checkbox from '../checkbox';
import { useLocale } from '../locale';
import defaultLocale from '../locale/en_US';
type ListItemProps<RecordType> = {
renderedText?: string | number;
renderedEl: React.ReactNode;
disabled?: boolean;
checked?: boolean;
prefixCls: string;
onClick: (item: RecordType, e: React.MouseEvent<HTMLLIElement, MouseEvent>) => void;
onRemove?: (item: RecordType) => void;
item: RecordType;
showRemove?: boolean;
};
const ListItem = <RecordType extends KeyWiseTransferItem>(props: ListItemProps<RecordType>) => {
const {
renderedText,
renderedEl,
item,
checked,
disabled,
prefixCls,
onClick,
onRemove,
showRemove,
} = props;
const className = classNames(`${prefixCls}-content-item`, {
[`${prefixCls}-content-item-disabled`]: disabled || item.disabled,
[`${prefixCls}-content-item-checked`]: checked,
});
let title: string | undefined;
if (typeof renderedText === 'string' || typeof renderedText === 'number') {
title = String(renderedText);
}
const [contextLocale] = useLocale('Transfer', defaultLocale.Transfer);
const liProps: React.HTMLAttributes<HTMLLIElement> = { className, title };
const labelNode = <span className={`${prefixCls}-content-item-text`}>{renderedEl}</span>;
if (showRemove) {
return (
<li {...liProps}>
{labelNode}
<TransButton
disabled={disabled || item.disabled}
className={`${prefixCls}-content-item-remove`}
aria-label={contextLocale?.remove}
onClick={() => {
onRemove?.(item);
}}
>
<DeleteOutlined />
</TransButton>
</li>
);
}
// Default click to select
liProps.onClick = disabled || item.disabled ? undefined : (event) => onClick(item, event);
return (
<li {...liProps}>
<Checkbox
className={`${prefixCls}-checkbox`}
checked={checked}
disabled={disabled || item.disabled}
/>
{labelNode}
</li>
);
};
export default React.memo(ListItem);