Skip to content

Commit 2718ac6

Browse files
authored
Accurately sync values ​​of UnitControl and ToggleGroupControl (#240)
1 parent f2f4f40 commit 2718ac6

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

src/settings/table-cell-settings.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
__experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon,
2323
__experimentalUnitControl as UnitControl,
2424
__experimentalUseCustomUnits as useCustomUnits,
25+
__experimentalParseQuantityAndUnitFromRawValue as parseQuantityAndUnitFromRawValue,
2526
} from '@wordpress/components';
2627

2728
/**
@@ -67,6 +68,8 @@ import type {
6768
BlockAttributes,
6869
} from '../BlockAttributes';
6970

71+
const PERCENTAGE_WIDTHS = [ 25, 50, 75, 100 ];
72+
7073
type Props = {
7174
setAttributes: ( attrs: Partial< BlockAttributes > ) => void;
7275
vTable: VTable;
@@ -99,6 +102,9 @@ export default function TableCellSettings( { setAttributes, vTable, selectedCell
99102
}, [] );
100103

101104
const cellStylesObj = convertToObject( targetCell.styles );
105+
const [ parsedWidthQuantity, parsedWidthUnit ] = parseQuantityAndUnitFromRawValue(
106+
cellStylesObj?.width
107+
);
102108

103109
const updateCellsState = ( state: {
104110
styles?: any;
@@ -269,10 +275,16 @@ export default function TableCellSettings( { setAttributes, vTable, selectedCell
269275
hideLabelFromVision
270276
label={ __( 'Cell percentage width', 'flexible-table-block' ) }
271277
isBlock
272-
value={ cellStylesObj?.width }
278+
value={
279+
parsedWidthQuantity &&
280+
PERCENTAGE_WIDTHS.includes( parsedWidthQuantity ) &&
281+
parsedWidthUnit === '%'
282+
? cellStylesObj?.width
283+
: undefined
284+
}
273285
onChange={ ( value ) => onChangeWidth( value as Property.Width ) }
274286
>
275-
{ [ 25, 50, 75, 100 ].map( ( perWidth ) => {
287+
{ PERCENTAGE_WIDTHS.map( ( perWidth ) => {
276288
return (
277289
<ToggleGroupControlOption
278290
key={ perWidth }

src/settings/table-settings.tsx

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
7071
import type { StickyValue, BlockAttributes } from '../BlockAttributes';
7172
import type { StoreOptions } from '../store';
7273

74+
const PERCENTAGE_WIDTHS = [ 25, 50, 75, 100 ];
75+
7376
type 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

Comments
 (0)