/** * WordPress dependencies */ import { BaseControl, TextareaControl, SelectControl, } from '@safe-wordpress/components'; import { useInstanceId } from '@safe-wordpress/compose'; import { useState } from '@safe-wordpress/element'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import clsx from 'clsx'; import { toPairs, trim, uniq } from 'lodash'; import { ErrorText } from '@nab/components'; import type { SREditProps } from '@nab/types'; /** * Internal dependencies */ import './edit.scss'; import type { Attributes } from './types'; export const Edit = ( { attributes: { condition, values }, errors, setAttributes, }: SREditProps< Attributes > ): JSX.Element => { const instanceId = useInstanceId( Edit ); const [ currentValue, setCurrentValue ] = useState( values.join( '\n' ) ); return ( <> setAttributes( { condition: newCondition } ) } />
{ setCurrentValue( newValue ); setAttributes( { values: uniq( newValue .split( '\n' ) .map( trim ) .filter( ( x ) => !! x.length ) ), } ); } } />
{ !! errors.values && (

) } ); }; // ======= // HELPERS // ======= const CONDITION_LABEL_RECORD: Record< Attributes[ 'condition' ], string > = { 'is-equal-to': _x( 'Is equal to', 'text', 'nelio-ab-testing' ), 'is-not-equal-to': _x( 'Is not equal to', 'text', 'nelio-ab-testing' ), }; const CONDITIONS = toPairs( CONDITION_LABEL_RECORD ).map( ( [ value, label ] ) => ( { label, value, } ) );