UNPKG

@pnp/spfx-property-controls

Version:

Reusable property pane controls for SharePoint Framework solutions

84 lines 4.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const React = tslib_1.__importStar(require("react")); const GeneralHelper_1 = require("../../../../helpers/GeneralHelper"); const react_1 = require("@fluentui/react"); const strings = tslib_1.__importStar(require("PropertyControlStrings")); const UploadFilePickerTab_module_scss_1 = tslib_1.__importDefault(require("./UploadFilePickerTab.module.scss")); class UploadFilePickerTab extends React.Component { constructor(props) { super(props); /** * Gets called when a file is uploaded */ this._handleFileUpload = (event) => { if (!event.target.files || event.target.files.length < 1) { return; } // Get the files that were uploaded const files = event.target.files; // Grab the first file -- there should always only be one const file = files[0]; const filePickerResult = { fileAbsoluteUrl: null, fileName: file.name, fileNameWithoutExtension: GeneralHelper_1.GeneralHelper.getFileNameWithoutExtension(file.name), downloadFileContent: () => { return Promise.resolve(file); } }; if (GeneralHelper_1.GeneralHelper.isImage(file.name)) { // Convert to base64 image const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => { this.setState({ filePreview: reader.result }); }; } this.setState({ filePickerResult, filePreview: undefined }); }; /** * Saves base64 encoded image back to property pane file picker */ this._handleSave = () => { this.props.onSave(this.state.filePickerResult); }; /** * Closes tab without saving */ this._handleClose = () => { this.props.onClose(); }; this.state = { filePickerResult: undefined }; } render() { const { filePickerResult, filePreview } = this.state; const fileName = filePickerResult ? filePickerResult.fileName : null; const acceptedFilesExtensions = this.props.accepts ? this.props.accepts.join(",") : null; return (React.createElement("div", { className: UploadFilePickerTab_module_scss_1.default.tabContainer }, React.createElement("div", { className: UploadFilePickerTab_module_scss_1.default.tabHeaderContainer }, React.createElement("h2", { className: UploadFilePickerTab_module_scss_1.default.tabHeader }, strings.UploadFileHeader)), React.createElement("div", { className: (0, react_1.css)(UploadFilePickerTab_module_scss_1.default.tab, UploadFilePickerTab_module_scss_1.default.tabOffset) }, React.createElement("input", { className: UploadFilePickerTab_module_scss_1.default.localTabInput, type: "file", id: "fileInput", accept: acceptedFilesExtensions, multiple: false, onChange: (event) => this._handleFileUpload(event) }), fileName && filePreview && /** Display image preview */ React.createElement("div", { className: UploadFilePickerTab_module_scss_1.default.localTabSinglePreview }, React.createElement("img", { className: UploadFilePickerTab_module_scss_1.default.localTabSinglePreviewImage, src: filePreview, alt: filePickerResult.fileName }), React.createElement("span", null, fileName)), React.createElement("div", null, React.createElement("label", { className: UploadFilePickerTab_module_scss_1.default.localTabFilename }, (!filePreview && fileName ? fileName : ""))), React.createElement("label", { className: UploadFilePickerTab_module_scss_1.default.localTabLabel, htmlFor: "fileInput" }, (fileName ? strings.ChangeFileLinkLabel : strings.ChooseFileLinkLabel))), React.createElement("div", { className: UploadFilePickerTab_module_scss_1.default.actionButtonsContainer }, React.createElement("div", { className: UploadFilePickerTab_module_scss_1.default.actionButtons }, React.createElement(react_1.PrimaryButton, { disabled: !filePickerResult, onClick: () => this._handleSave(), className: UploadFilePickerTab_module_scss_1.default.actionButton }, strings.AddFileButtonLabel), React.createElement(react_1.DefaultButton, { onClick: () => this._handleClose(), className: UploadFilePickerTab_module_scss_1.default.actionButton }, strings.CancelButtonLabel))))); } } exports.default = UploadFilePickerTab; //# sourceMappingURL=UploadFilePickerTab.js.map