/** * External dependencies */ import clsx from 'clsx'; import type { PropertyValue } from 'csstype'; /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; import { link, linkOff } from '@wordpress/icons'; import { useState } from '@wordpress/element'; import { BaseControl, Button, __experimentalToggleGroupControl as ToggleGroupControl, __experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon, __experimentalHStack as HStack, __experimentalVStack as VStack, __experimentalText as Text, } from '@wordpress/components'; import { useInstanceId } from '@wordpress/compose'; /** * Internal dependencies */ import { BORDER_STYLE_CONTROLS, SIDE_CONTROLS } from '../constants'; import { SideIndicatorControl } from './indicator-control'; const DEFAULT_VALUES = { top: '', right: '', bottom: '', left: '', }; type Props = { label: string; help?: string; onChange: ( event: any ) => void; values: { top?: PropertyValue< string >; right?: PropertyValue< string >; bottom?: PropertyValue< string >; left?: PropertyValue< string >; }; allowSides?: boolean; hasIndicator?: boolean; className?: string; }; type ValuesKey = keyof typeof DEFAULT_VALUES; export default function BorderStyleControl( { label = __( 'Border style', 'flexible-table-block' ), help, onChange, values: valuesProp, allowSides = true, hasIndicator = true, className, }: Props ) { const values = { ...DEFAULT_VALUES, ...valuesProp, }; const instanceId = useInstanceId( BorderStyleControl, 'ftb-border-style-control' ); const headingId = `${ instanceId }-heading`; const isMixed = allowSides && ! ( values.top === values.right && values.top === values.bottom && values.top === values.left ); const [ isLinked, setIsLinked ] = useState< boolean >( true ); const linkedLabel: string = isLinked ? __( 'Unlink sides', 'flexible-table-block' ) : __( 'Link sides', 'flexible-table-block' ); const allInputValue: string | undefined = isMixed ? undefined : values.top || undefined; const toggleLinked = () => setIsLinked( ! isLinked ); const handleOnClickAll = ( value: string | number | undefined ) => { const newValue = value === values.top && value === values.right && value === values.bottom && value === values.left ? undefined : value; onChange( { top: newValue, right: newValue, bottom: newValue, left: newValue, } ); }; const handleOnClick = ( value: string | number | undefined, targetSide: ValuesKey ) => { const newValue = values[ targetSide as ValuesKey ] && value === values[ targetSide as ValuesKey ] ? undefined : value; onChange( { ...values, [ targetSide ]: newValue, } ); }; return ( { isMixed && isLinked ? `${ label } ${ __( '(Mixed)', 'flexible-table-block' ) }` : label } { isLinked ? ( { hasIndicator && } { BORDER_STYLE_CONTROLS.map( ( borderStyle ) => ( ) ) } ) : ( { SIDE_CONTROLS.map( ( item ) => ( { hasIndicator && } handleOnClick( value, item.value as ValuesKey ) } > { BORDER_STYLE_CONTROLS.map( ( borderStyle ) => ( ) ) } ) ) } ) } { allowSides && (