| 1 |
/** |
| 2 |
* Inspector panel for add-on field controls from `window.wppbFb.extraControls`. |
| 3 |
* Renders only descriptors whose attribute is on this block's schema. |
| 4 |
* `hideInsideRepeater` suppresses controls that classic strips from sub-fields. |
| 5 |
*/ |
| 6 |
import { PanelBody, TextControl, ToggleControl } from '@wordpress/components'; |
| 7 |
import { __ } from '@wordpress/i18n'; |
| 8 |
|
| 9 |
export default function ExtraFieldPropertiesPanel( { attributes, setAttributes, insideRepeater = false } ) { |
| 10 |
const fb = ( typeof window !== 'undefined' && window.wppbFb ) || {}; |
| 11 |
const controls = Array.isArray( fb.extraControls ) ? fb.extraControls : []; |
| 12 |
|
| 13 |
// De-dupe by attribute (last wins); skip attrs not on this block. |
| 14 |
const seen = new Set(); |
| 15 |
const applicable = []; |
| 16 |
for ( let i = controls.length - 1; i >= 0; i-- ) { |
| 17 |
const c = controls[ i ]; |
| 18 |
if ( ! c || ! c.attribute || seen.has( c.attribute ) ) continue; |
| 19 |
if ( ! Object.prototype.hasOwnProperty.call( attributes, c.attribute ) ) continue; |
| 20 |
if ( insideRepeater && c.hideInsideRepeater ) continue; |
| 21 |
seen.add( c.attribute ); |
| 22 |
applicable.unshift( c ); |
| 23 |
} |
| 24 |
if ( applicable.length === 0 ) return null; |
| 25 |
|
| 26 |
return ( |
| 27 |
<PanelBody title={ __( 'Additional Settings', 'profile-builder' ) } initialOpen={ false }> |
| 28 |
{ applicable.map( ( c ) => { |
| 29 |
if ( c.control === 'toggle' ) { |
| 30 |
const onValue = c.onValue != null ? c.onValue : 'Yes'; |
| 31 |
const offValue = c.offValue != null ? c.offValue : 'No'; |
| 32 |
return ( |
| 33 |
<ToggleControl |
| 34 |
key={ c.attribute } |
| 35 |
label={ c.label || c.attribute } |
| 36 |
help={ c.help || undefined } |
| 37 |
checked={ attributes[ c.attribute ] === onValue } |
| 38 |
onChange={ ( on ) => setAttributes( { [ c.attribute ]: on ? onValue : offValue } ) } |
| 39 |
/> |
| 40 |
); |
| 41 |
} |
| 42 |
return ( |
| 43 |
<TextControl |
| 44 |
key={ c.attribute } |
| 45 |
label={ c.label || c.attribute } |
| 46 |
help={ c.help || undefined } |
| 47 |
type={ c.control === 'number' ? 'number' : 'text' } |
| 48 |
value={ attributes[ c.attribute ] != null ? attributes[ c.attribute ] : '' } |
| 49 |
onChange={ ( val ) => setAttributes( { [ c.attribute ]: val } ) } |
| 50 |
/> |
| 51 |
); |
| 52 |
} ) } |
| 53 |
</PanelBody> |
| 54 |
); |
| 55 |
} |
| 56 |
|