ant-design/components/transfer/ListItem.tsx

89 lines
2.5 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2016-11-04 16:52:57 +08:00
import classNames from 'classnames';
import DeleteOutlined from '@ant-design/icons/DeleteOutlined';
import type { KeyWiseTransferItem, TransferLocale } from '.';
import defaultLocale from '../locale/default';
2016-11-04 16:52:57 +08:00
import Checkbox from '../checkbox';
import TransButton from '../_util/transButton';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
2016-11-04 16:52:57 +08:00
type ListItemProps<RecordType> = {
renderedText?: string | number;
renderedEl: React.ReactNode;
disabled?: boolean;
checked?: boolean;
prefixCls: string;
onClick: (item: RecordType) => 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;
2019-08-05 18:38:10 +08:00
const className = classNames({
[`${prefixCls}-content-item`]: true,
[`${prefixCls}-content-item-disabled`]: disabled || item.disabled,
[`${prefixCls}-content-item-checked`]: checked,
});
2016-11-04 16:52:57 +08:00
let title: string | undefined;
if (typeof renderedText === 'string' || typeof renderedText === 'number') {
title = String(renderedText);
}
2016-11-04 16:52:57 +08:00
return (
<LocaleReceiver componentName="Transfer" defaultLocale={defaultLocale.Transfer}>
{(transferLocale: TransferLocale) => {
const liProps: React.HTMLAttributes<HTMLLIElement> = { className, title };
const labelNode = <span className={`${prefixCls}-content-item-text`}>{renderedEl}</span>;
// Show remove
if (showRemove) {
return (
<li {...liProps}>
{labelNode}
<TransButton
disabled={disabled || item.disabled}
className={`${prefixCls}-content-item-remove`}
aria-label={transferLocale.remove}
onClick={() => {
onRemove?.(item);
}}
>
<DeleteOutlined />
</TransButton>
</li>
);
}
// Default click to select
liProps.onClick = disabled || item.disabled ? undefined : () => onClick(item);
return (
<li {...liProps}>
<Checkbox
className={`${prefixCls}-checkbox`}
checked={checked}
disabled={disabled || item.disabled}
/>
{labelNode}
</li>
);
}}
</LocaleReceiver>
);
};
export default React.memo(ListItem);