| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import { |
| 3 |
RichText, |
| 4 |
BlockIcon, |
| 5 |
useBlockProps, |
| 6 |
__experimentalGetElementClassName, |
| 7 |
useInnerBlocksProps, |
| 8 |
} from '@wordpress/block-editor'; |
| 9 |
import { blockTable as icon } from '@wordpress/icons'; |
| 10 |
import { Button, Placeholder } from '@wordpress/components'; |
| 11 |
|
| 12 |
export default function Edit( { |
| 13 |
attributes, |
| 14 |
setAttributes, |
| 15 |
clientId, |
| 16 |
advancedControl, |
| 17 |
} ) { |
| 18 |
const { |
| 19 |
caption, |
| 20 |
rowCount, |
| 21 |
columnCount, |
| 22 |
headers, |
| 23 |
footers, |
| 24 |
hasHeader, |
| 25 |
hasFooter, |
| 26 |
isEmpty, |
| 27 |
} = attributes; |
| 28 |
const { GkitStyle, GkitNumber, GkitSwitcher } = window.gutenkit.components; |
| 29 |
const { useDeviceType } = window.gutenkit.helpers; |
| 30 |
const device = useDeviceType(); |
| 31 |
const template = Array.from( { length: rowCount }, () => [ |
| 32 |
'gutenkit/table-builder-row', |
| 33 |
] ); |
| 34 |
const blockProps = useBlockProps(); |
| 35 |
const innerBlocksProps = useInnerBlocksProps( |
| 36 |
{ |
| 37 |
className: 'gkit-table__body', |
| 38 |
}, |
| 39 |
{ |
| 40 |
allowedBlocks: [ 'gutenkit/table-builder-row' ], |
| 41 |
renderAppender: false, |
| 42 |
template, |
| 43 |
} |
| 44 |
); |
| 45 |
|
| 46 |
return ( |
| 47 |
<> |
| 48 |
<figure { ...blockProps }> |
| 49 |
{ ! isEmpty ? ( |
| 50 |
<> |
| 51 |
<table className="gkit-table"> |
| 52 |
{ hasHeader && ( |
| 53 |
<thead className="gkit-table__header"> |
| 54 |
<tr>{ Headers }</tr> |
| 55 |
</thead> |
| 56 |
) } |
| 57 |
<tbody { ...innerBlocksProps }></tbody> |
| 58 |
{ hasFooter && ( |
| 59 |
<tfoot className="gkit-table__footer"> |
| 60 |
<tr>{ Footers }</tr> |
| 61 |
</tfoot> |
| 62 |
) } |
| 63 |
</table> |
| 64 |
<RichText |
| 65 |
identifier="caption" |
| 66 |
tagName="figcaption" |
| 67 |
className="gkit-table__caption" |
| 68 |
value={ caption } |
| 69 |
onChange={ ( value ) => |
| 70 |
setAttributes( { caption: value } ) |
| 71 |
} |
| 72 |
placeholder="Caption for Table" |
| 73 |
/> |
| 74 |
</> |
| 75 |
) : ( |
| 76 |
<Placeholder |
| 77 |
label={ __( 'Table' ) } |
| 78 |
icon={ <BlockIcon icon={ icon } showColors /> } |
| 79 |
instructions={ __( |
| 80 |
'Insert a table for sharing data.' |
| 81 |
) } |
| 82 |
> |
| 83 |
<form |
| 84 |
className="blocks-table__placeholder-form" |
| 85 |
onSubmit={ () => |
| 86 |
setAttributes( { isEmpty: false } ) |
| 87 |
} |
| 88 |
> |
| 89 |
</form> |
| 90 |
</Placeholder> |
| 91 |
) } |
| 92 |
</figure> |
| 93 |
</> |
| 94 |
); |
| 95 |
} |
| 96 |
|