Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions components/doc/common/apidoc/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -25096,6 +25096,14 @@
"default": "50",
"description": "Width of the image thumbnail in pixels."
},
{
"name": "filenameTruncateLength",
"optional": true,
"readonly": false,
"type": "number",
"default": "40",
"description": "Length of the maximum file name characters that can be displayed."
},
{
"name": "progressBarTemplate",
"optional": true,
Expand Down
37 changes: 32 additions & 5 deletions components/lib/fileupload/FileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ProgressBar } from '../progressbar/ProgressBar';
import { Ripple } from '../ripple/Ripple';
import { classNames, DomHandler, IconUtils, ObjectUtils } from '../utils/Utils';
import { FileUploadBase } from './FileUploadBase';
import { Tooltip } from '../tooltip/Tooltip';

export const FileUpload = React.memo(
React.forwardRef((inProps, ref) => {
Expand Down Expand Up @@ -41,6 +42,7 @@ export const FileUpload = React.memo(
const fileInputRef = React.useRef(null);
const messagesRef = React.useRef(null);
const contentRef = React.useRef(null);
const fileNameRef = React.useRef(null);
const uploadedFileCount = React.useRef(0);
const hasFiles = ObjectUtils.isNotEmpty(filesState);
const hasUploadedFiles = ObjectUtils.isNotEmpty(uploadedFilesState);
Expand Down Expand Up @@ -423,6 +425,12 @@ export const FileUpload = React.memo(
}
};

const truncateFilename = (filename) => {
const maxLength = props.filenameTruncateLength;

return filename.length > maxLength ? `${filename.substring(0, maxLength / 2)}...${filename.substring(filename.length - maxLength / 2)}` : filename;
};

const createFile = (file, index, badgeOptions) => {
const key = file.name + file.type + file.size;
const thumbnailProps = mergeProps(
Expand All @@ -439,19 +447,27 @@ export const FileUpload = React.memo(
const fileSizeProps = mergeProps(ptm('fileSize'));
const fileNameProps = mergeProps(
{
className: cx('fileName')
className: cx('fileName'),
ref: fileNameRef,
style: {
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
maxWidth: '100%'
}
},
ptm('fileName')
);
const actionsProps = mergeProps(ptm('actions'));
const fileName = <div {...fileNameProps}>{file.name}</div>;
const fileName = <div {...fileNameProps}>{truncateFilename(file.name)}</div>;
const size = <div {...fileSizeProps}>{formatSize(file.size)}</div>;

const contentBody = (
<div {...detailsProps}>
<div {...fileNameProps}> {file.name}</div>
<div {...fileNameProps}> {truncateFilename(file.name)}</div>
<span {...fileSizeProps}>{formatSize(file.size)}</span>
<Badge className="p-fileupload-file-badge" value={badgeOptions.value} severity={badgeOptions.severity} pt={ptm('badge')} __parentMetadata={{ parent: metaData }} />
<Tooltip target={fileNameRef} content={file.name} pt={ptm('tooltip')} />
</div>
);
const removeButton = (
Expand Down Expand Up @@ -685,12 +701,22 @@ export const FileUpload = React.memo(
const chooseOptions = props.chooseOptions;
const labelProps = mergeProps(
{
className: cx('label')
className: cx('label'),
ref: fileNameRef,
style: hasFiles
? {
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
maxWidth: '100%'
}
: undefined
},
ptm('label')
);
const chooseLabel = chooseOptions.iconOnly ? <span {...labelProps} dangerouslySetInnerHTML={{ __html: '&nbsp;' }} /> : <span {...labelProps}>{chooseButtonLabel}</span>;
const label = props.auto ? chooseLabel : <span {...labelProps}>{hasFiles ? props.selectedFileLabel || filesState[0].name : chooseLabel}</span>;
const label = props.auto ? chooseLabel : <span {...labelProps}>{hasFiles ? truncateFilename(props.selectedFileLabel || filesState[0].name) : chooseLabel}</span>;
const tooltip = hasFiles ? <Tooltip target={fileNameRef} content={filesState[0].name} pt={ptm('tooltip')} /> : null;
const chooseIconProps = mergeProps(
{
className: cx('chooseIcon', { iconOnly: chooseOptions.iconOnly })
Expand Down Expand Up @@ -741,6 +767,7 @@ export const FileUpload = React.memo(
{chooseIcon}
{label}
{input}
{tooltip}
<Ripple />
</span>
</div>
Expand Down
1 change: 1 addition & 0 deletions components/lib/fileupload/FileUploadBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const FileUploadBase = ComponentBase.extend({
className: null,
withCredentials: false,
previewWidth: 50,
filenameTruncateLength: 40,
chooseLabel: null,
selectedFileLabel: null,
uploadLabel: null,
Expand Down
5 changes: 5 additions & 0 deletions components/lib/fileupload/fileupload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,11 @@ interface FileUploadProps {
* @defaultValue 50
*/
previewWidth?: number | undefined;
/**
* Length of the maximum file name characters that can be displayed.
* @defaultValue 40
*/
filenameTruncateLength?: number | undefined;
/**
* Label of the choose button. Defaults to global value in Locale configuration.
*/
Expand Down
Loading