@@ -21,6 +21,7 @@ import {
2121 __experimentalToggleGroupControl as ToggleGroupControl ,
2222 __experimentalToggleGroupControlOption as ToggleGroupControlOption ,
2323 __experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon ,
24+ __experimentalParseQuantityAndUnitFromRawValue as parseQuantityAndUnitFromRawValue ,
2425} from '@wordpress/components' ;
2526
2627/**
@@ -70,6 +71,8 @@ import {
7071import type { StickyValue , BlockAttributes } from '../BlockAttributes' ;
7172import type { StoreOptions } from '../store' ;
7273
74+ const PERCENTAGE_WIDTHS = [ 25 , 50 , 75 , 100 ] ;
75+
7376type Props = {
7477 attributes : BlockAttributes ;
7578 setAttributes : ( attrs : Partial < BlockAttributes > ) => void ;
@@ -97,6 +100,16 @@ export default function TableSettings( {
97100
98101 const tableWidthUnits = useCustomUnits ( { availableUnits : TABLE_WIDTH_UNITS } ) ;
99102
103+ const [ parsedWidthQuantity , parsedWidthUnit ] = parseQuantityAndUnitFromRawValue (
104+ tableStylesObj ?. width
105+ ) ;
106+ const [ parsedMaxWidthQuantity , parsedMaxWidthUnit ] = parseQuantityAndUnitFromRawValue (
107+ tableStylesObj ?. maxWidth
108+ ) ;
109+ const [ parsedMinWidthQuantity , parsedMinWidthUnit ] = parseQuantityAndUnitFromRawValue (
110+ tableStylesObj ?. minWidth
111+ ) ;
112+
100113 const onChangeHasFixedLayout = ( ) => {
101114 setAttributes ( { hasFixedLayout : ! hasFixedLayout } ) ;
102115 } ;
@@ -331,10 +344,17 @@ export default function TableSettings( {
331344 hideLabelFromVision
332345 label = { __ ( 'Table percentage width' , 'flexible-table-block' ) }
333346 isBlock
334- value = { tableStylesObj ?. width }
347+ value = {
348+ tableStylesObj ?. width === 'auto' ||
349+ ( parsedWidthQuantity &&
350+ PERCENTAGE_WIDTHS . includes ( parsedWidthQuantity ) &&
351+ parsedWidthUnit === '%' )
352+ ? tableStylesObj ?. width
353+ : undefined
354+ }
335355 onChange = { ( value ) => onChangeWidth ( value as Property . Width ) }
336356 >
337- { [ 25 , 50 , 75 , 100 ] . map ( ( perWidth ) => {
357+ { PERCENTAGE_WIDTHS . map ( ( perWidth ) => {
338358 return (
339359 < ToggleGroupControlOption
340360 key = { perWidth }
@@ -369,10 +389,17 @@ export default function TableSettings( {
369389 hideLabelFromVision
370390 label = { __ ( 'Table percentage max width' , 'flexible-table-block' ) }
371391 isBlock
372- value = { tableStylesObj ?. maxWidth }
392+ value = {
393+ tableStylesObj ?. maxWidth === 'none' ||
394+ ( parsedMaxWidthQuantity &&
395+ PERCENTAGE_WIDTHS . includes ( parsedMaxWidthQuantity ) &&
396+ parsedMaxWidthUnit === '%' )
397+ ? tableStylesObj ?. maxWidth
398+ : undefined
399+ }
373400 onChange = { ( value ) => onChangeMaxWidth ( value as Property . MaxWidth ) }
374401 >
375- { [ 25 , 50 , 75 , 100 ] . map ( ( perWidth ) => {
402+ { PERCENTAGE_WIDTHS . map ( ( perWidth ) => {
376403 return (
377404 < ToggleGroupControlOption
378405 key = { perWidth }
@@ -406,10 +433,16 @@ export default function TableSettings( {
406433 hideLabelFromVision
407434 label = { __ ( 'Table percentage min width' , 'flexible-table-block' ) }
408435 isBlock
409- value = { tableStylesObj ?. minWidth }
436+ value = {
437+ parsedMinWidthQuantity &&
438+ PERCENTAGE_WIDTHS . includes ( parsedMinWidthQuantity ) &&
439+ parsedMinWidthUnit === '%'
440+ ? tableStylesObj ?. minWidth
441+ : undefined
442+ }
410443 onChange = { ( value ) => onChangeMinWidth ( value as Property . MinWidth ) }
411444 >
412- { [ 25 , 50 , 75 , 100 ] . map ( ( perWidth ) => {
445+ { PERCENTAGE_WIDTHS . map ( ( perWidth ) => {
413446 return (
414447 < ToggleGroupControlOption
415448 key = { perWidth }
0 commit comments