/**
* External dependencies
*/
import clsx from 'clsx';
import type { FormEvent } from 'react';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState, createInterpolateElement } from '@wordpress/element';
import { BlockIcon } from '@wordpress/block-editor';
import {
Button,
Placeholder,
TextControl,
ToggleControl,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
__experimentalSpacer as Spacer,
__experimentalText as Text,
} from '@wordpress/components';
import { isAppleOS } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import {
DEFAULT_PREVIEW_ROWS,
DEFAULT_PREVIEW_COLUMNS,
MIN_PREVIEW_TABLE_HEIGHT,
MAX_PREVIEW_TABLE_COL,
MAX_PREVIEW_TABLE_ROW,
THRESHOLD_PREVIEW_TABLE_COL,
THRESHOLD_PREVIEW_TABLE_ROW,
} from '../constants';
import { createTable, toTableAttributes, type VTable } from '../utils/table-state';
import { blockIcon as icon } from '../icons';
import type { BlockAttributes } from '../BlockAttributes';
type Props = {
setAttributes: ( attrs: Partial< BlockAttributes > ) => void;
};
export default function TablePlaceholder( { setAttributes }: Props ) {
const [ rowCount, setRowCount ] = useState< number | undefined >( DEFAULT_PREVIEW_ROWS );
const [ colCount, setColCount ] = useState< number | undefined >( DEFAULT_PREVIEW_COLUMNS );
const [ headerSection, setHeaderSection ] = useState( false );
const [ footerSection, setFooterSection ] = useState( false );
const totalRowCount: number | undefined = rowCount
? rowCount + Number( headerSection ) + Number( footerSection )
: undefined;
const cellHeight: number | undefined = totalRowCount
? Number( MIN_PREVIEW_TABLE_HEIGHT / Math.min( THRESHOLD_PREVIEW_TABLE_ROW, totalRowCount ) )
: undefined;
const onCreateTable = ( event: FormEvent ) => {
event.preventDefault();
if ( ! rowCount || ! colCount ) {
return;
}
const vTable: VTable = createTable( {
rowCount: Math.min( rowCount, MAX_PREVIEW_TABLE_ROW ),
colCount: Math.min( colCount, MAX_PREVIEW_TABLE_COL ),
headerSection,
footerSection,
} );
setAttributes( toTableAttributes( vTable ) );
};
const onChangeColumnCount = ( value: string ) => {
const parsedValue = parseInt( value, 10 );
if ( Number.isNaN( parsedValue ) ) {
setColCount( undefined );
} else {
setColCount( Math.max( 1, Math.min( MAX_PREVIEW_TABLE_COL, parsedValue ) ) );
}
};
const onChangeRowCount = ( value: string ) => {
const parsedValue = parseInt( value, 10 );
if ( Number.isNaN( parsedValue ) ) {
setRowCount( undefined );
} else {
setRowCount( Math.max( 1, Math.min( MAX_PREVIEW_TABLE_ROW, parsedValue ) ) );
}
};
const onToggleHeaderSection = ( section: boolean ) => setHeaderSection( section );
const onToggleFooterSection = ( section: boolean ) => setFooterSection( section );
const tableClasses = clsx( 'ftb-placeholder__table', {
'is-overflow-row': totalRowCount && totalRowCount > THRESHOLD_PREVIEW_TABLE_ROW,
'is-overflow-col': colCount && colCount > THRESHOLD_PREVIEW_TABLE_COL,
} );
return (
Command key to select multiple cells. Hold Shift key to select the range.',
'flexible-table-block'
)
: __(
'Hint: Hold Ctrl key to select multiple cells. Hold Shift key to select the range.',
'flexible-table-block'
),
{ code: }
) }
| ) ) } |
|---|
| ) ) } |
| ) ) } |