/** * JavaScript code for the "Import Screen" component. * * @package TablePress * @subpackage Import Screen * @author Tobias Bäthge * @since 2.2.0 */ /* globals tp */ /** * WordPress dependencies. */ import { useEffect, useRef, useState } from 'react'; import { Icon, } from '@wordpress/components'; import { info } from '@wordpress/icons'; import { __, _n, _x, sprintf } from '@wordpress/i18n'; // Details for the available import sources. const importSources = { 'file-upload': { label: __( 'File Upload', 'tablepress' ), instruction: __( 'Select files', 'tablepress' ), }, url: { label: __( 'URL', 'tablepress' ), instruction: __( 'File URL', 'tablepress' ), }, server: { label: __( 'File on server', 'tablepress' ), instruction: __( 'Server Path to file', 'tablepress' ), }, 'form-field': { label: __( 'Manual Input', 'tablepress' ), instruction: __( 'Import data', 'tablepress' ), }, }; if ( ! tp.import.showImportSourceUrl ) { delete importSources.url; } if ( ! tp.import.showImportSourceServer ) { delete importSources.server; } // Number of tables. const tablesCount = Object.keys( tp.import.tables ).length; // The ; } ); /** * Returns the "Import Screen" component's JSX markup. * * @return {Object} Import Screen component. */ const Screen = () => { const [ screenData, setScreenData ] = useState( { importSource: tp.import.importSource, importType: tp.import.importType, importFileUpload: [], importUrl: tp.import.importUrl, importServer: tp.import.importServer, importFormField: tp.import.importFormField, importExistingTable: tp.import.importExistingTable, validationHighlighting: false, } ); /** * Handles screen data state changes. * * @param {Object} updatedData Data in the screen data state that should be updated. */ const updateScreenData = ( updatedData ) => { const newScreenData = { ...screenData, validationHighlighting: false, // Reset with every UI state change. ...updatedData, }; setScreenData( newScreenData ); }; // References to DOM elements. const importServerInput = useRef( null ); const appendReplaceDropdown = useRef( null ); const fileUploadDropzone = useRef( null ); // Initialize the jSuites dropdown when the component is mounted. useEffect( () => { jSuites.dropdown( appendReplaceDropdown.current, { autocomplete: true, placeholder: __( '— Select or type —', 'tablepress' ), onchange: ( element, index, oldValue, newValue ) => { // Directly update the state with an updater function, as the state is otherwise reset. setScreenData( ( newScreenData ) => ( { ...newScreenData, validationHighlighting: false, importExistingTable: newValue, } ) ); }, } ); }, [] ); // Update the validation highlighting (using APIs and DOM elements outside of the React components) when the state changes. useEffect( () => { document.getElementById( 'tablepress_import-import-form' ).classList.toggle( 'no-validation-highlighting', ! screenData.validationHighlighting ); if ( ! screenData.validationHighlighting ) { importServerInput.current?.setCustomValidity( '' ); appendReplaceDropdown.current.previousElementSibling.querySelector( '.jdropdown-header' )?.setCustomValidity( '' ); } }, [ screenData.validationHighlighting ] ); // Determine calculated state variables to avoid repeating calculations. const fileUploadMultipleFilesChosen = ( 'file-upload' === screenData.importSource && ( 1 < screenData.importFileUpload.length || ( 1 === screenData.importFileUpload.length && screenData.importFileUpload[0].name.endsWith( '.zip' ) ) ) ); const appendReplaceDropdownDisabled = ( 0 === tablesCount || 'add' === screenData.importType || fileUploadMultipleFilesChosen ); // Disable the artificial dropdown (not inserted by React) via a class, as it can not use :disabled. useEffect( () => { appendReplaceDropdown.current.previousElementSibling.classList.toggle( 'disabled', appendReplaceDropdownDisabled ); }, [ appendReplaceDropdownDisabled ] ); return (
{ __( 'Import Source', 'tablepress' ) }: { Object.entries( importSources ) .map( ( [ importSource, importSourceData ] ) => ( ) ) }
{ /* * Always add the "File Upload" UI to the DOM, but hide it using `style="display: none;"` below. * This ensures that the field works, as that is "uncontrolled" in React, and setting its value (files) is not possible. */ }
( event.target.files && updateScreenData( { importFileUpload: event.target.files } ) ) } onDragEnter={ () => fileUploadDropzone.current.classList.add( 'dragover' ) } onDragLeave={ () => fileUploadDropzone.current.classList.remove( 'dragover' ) } />
{ 0 === screenData.importFileUpload.length && __( 'Click to select files, or drag them here.', 'tablepress' ) } { 0 < screenData.importFileUpload.length && sprintf( _n( 'You have selected %1$d file:', 'You have selected %1$d files:', screenData.importFileUpload.length, 'tablepress' ), screenData.importFileUpload.length ) } { [ ...screenData.importFileUpload ].map( ( file ) => { file.name } ) }
{ tp.import.showImportSourceUrl && 'url' === screenData.importSource && updateScreenData( { importUrl: event.target.value } ) } /> } { tp.import.showImportSourceServer && 'server' === screenData.importSource && updateScreenData( { importServer: event.target.value } ) } /> } { 'form-field' === screenData.importSource &&