/** * JavaScript code for the "Export Screen" component. * * @package TablePress * @subpackage Export Screen * @author Tobias Bäthge * @since 2.2.0 */ /** * WordPress dependencies. */ import { useEffect, useRef, useState } from 'react'; import { Button, CheckboxControl, __experimentalHStack as HStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis Icon, SelectControl, __experimentalVStack as VStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis } from '@wordpress/components'; import { info } from '@wordpress/icons'; import { __, _x, sprintf } from '@wordpress/i18n'; // Number of tables. const tablesCount = Object.keys( tp.export.tables ).length; // Show at least one empty row in the select, and between 3 and 12 total rows. let exportTablesSelectSize = tablesCount + 1; const maxExportTablesSelectSize = 12; exportTablesSelectSize = Math.max( exportTablesSelectSize, 3 ); exportTablesSelectSize = Math.min( exportTablesSelectSize, maxExportTablesSelectSize ); const exportTablesSelectMultiple = tp.export.zipSupportAvailable; const tablesSelectOptions = Object.entries( tp.export.tables ).map( ( [ tableId, tableName ] ) => { if ( '' === tableName.trim() ) { tableName = __( '(no name)', 'tablepress' ); } const optionText = sprintf( __( 'ID %1$s: %2$s', 'tablepress' ), tableId, tableName ); return { value: tableId, label: optionText }; } ); if ( ! exportTablesSelectMultiple ) { tablesSelectOptions.unshift( { value: '', label: __( '— Select —', 'tablepress' ), disabled: true } ); } const exportFormatsSelectOptions = Object.entries( tp.export.exportFormats ).map( ( [ exportFormat, exportFormatName ] ) => ( { value: exportFormat, label: exportFormatName } ) ); const csvDelimitersSelectOptions = Object.entries( tp.export.csvDelimiters ).map( ( [ csvDelimiter, csvDelimiterName ] ) => ( { value: csvDelimiter, label: csvDelimiterName } ) ); /** * Returns the "Export Screen" component's JSX markup. * * @return {Object} Export Screen component. */ const Screen = () => { const tablesExportListSelect = useRef( null ); const [ screenData, setScreenData ] = useState( { selectedTables: tp.export.selectedTables, exportFormat: tp.export.exportFormat, csvDelimiter: tp.export.csvDelimiter, createZipFile: false, reverseList: false, } ); // If more than one table is selected, force the ZIP file checkbox to checked. const zipFileRequired = screenData.selectedTables.length > 1; /** * Handles screen data state changes. * * @param {Object} updatedScreenData Data in the screen data state that should be updated. */ const updateScreenData = ( updatedScreenData ) => { setScreenData( ( currentScreenData ) => ( { ...currentScreenData, ...updatedScreenData, } ) ); }; /* * Set the size of the export tables dropdown to the number of tables, if ZIP file support is available. * `SelectControl` does not support the HTML `size` attribute, so that this has to be done manually with an effect. */ useEffect( () => { tablesExportListSelect.current.size = exportTablesSelectMultiple ? exportTablesSelectSize : 1; }, [] ); return (
|
|
|
|---|---|
|
|
|
|
|
|
| { __( 'ZIP file', 'tablepress' ) }: |
{ tp.export.zipSupportAvailable &&
|