@progress/sitefinity-nextjs-sdk
Version:
Provides OOB widgets developed using the Next.js framework, which includes an abstraction layer for Sitefinity communication. Additionally, it offers an expanded API, typings, and tools for further development and integration.
68 lines (67 loc) • 2.99 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { htmlAttributes } from '../../../editor/widget-framework/attributes';
import { getMinimumWidgetContext } from '../../../editor/widget-framework/widget-context';
import { Tracer } from '@progress/sitefinity-nextjs-sdk/diagnostics/empty';
import { RenderView } from '../../common/render-view';
import { FileUploadDefaultView } from './file-upload.view';
const predefinedAcceptValues = {
'Audio': ['.mp3', '.ogg', '.wav', '.wma'],
'Video': ['.avi', '.mpg', '.mpeg', '.mov', '.mp4', '.wmv'],
'Image': ['.jpg', '.jpeg', '.png', '.gif', '.bmp'],
'Document': ['.pdf', '.doc', '.docx', '.ppt', '.pptx', '.pps', '.ppsx', '.xls', '.xlsx']
};
const getAcceptedFileTypes = (entity) => {
const parsedArray = [];
const fileTypes = entity.FileTypes;
if (!fileTypes || !fileTypes.Type) {
return null;
}
const types = fileTypes.Type.split(',').map(x => x.trim());
for (const type of types) {
if (type === 'All') {
return null;
}
if (predefinedAcceptValues[type]) {
parsedArray.push(...predefinedAcceptValues[type]);
}
if (type === 'Other') {
const fileTypesSplit = fileTypes.Other?.split(',')
.map(t => t.trim().toLowerCase())
.map(t => t.startsWith('.') ? t : '.' + t);
if (fileTypesSplit) {
parsedArray.push(...fileTypesSplit);
}
}
}
return parsedArray;
};
export function FileUpload(props) {
const { span } = Tracer.traceWidget(props, false);
const entity = props.model.Properties;
const allowedFileTypes = getAcceptedFileTypes(entity);
const viewProps = {
allowMultipleFiles: entity.AllowMultipleFiles,
cssClass: entity.CssClass || '',
fieldName: entity.SfFieldName,
fileSizeViolationMessage: entity.FileSizeViolationMessage,
fileTypeViolationMessage: entity.FileTypeViolationMessage,
instructionalText: entity.InstructionalText || '',
label: entity.Label,
required: entity.Required,
requiredErrorMessage: entity.RequiredErrorMessage || '',
minFileSizeInMb: entity.Range?.Min || 0,
maxFileSizeInMb: entity.Range?.Max || 0,
allowedFileTypes: allowedFileTypes || [],
validationAttributes: entity.Required ? { 'required': 'required' } : {},
violationRestrictionsJson: {
maxSize: entity.Range?.Max,
minSize: entity.Range?.Min,
required: entity.Required,
allowMultiple: entity.AllowMultipleFiles,
allowedFileTypes: allowedFileTypes
},
attributes: htmlAttributes(props),
widgetContext: getMinimumWidgetContext(props)
};
return (_jsx(RenderView, { viewName: props.model.Properties.SfViewName, widgetKey: props.model.Name, traceSpan: span, viewProps: viewProps, children: _jsx(FileUploadDefaultView, { ...viewProps }) }));
}