Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BBMSK- 25] Field configurator refactor #23

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 54 additions & 133 deletions src/admin/components/FieldConfigurator.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import {
CheckboxControl,
TextControl,
ToggleControl,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useEffect } from '@wordpress/element';
import classNames from 'classnames';

import OptionConfig from './OptionConfig';
import { getAttributes } from '../fields';
import { getAttributes, getConfig } from '../fields';

const FieldConfigurator = ( { field, setting, setNewSetting } ) => {
/**
* Set the new setting with default attributes when the field changes and on first mount
* Set the new setting with default attributes when the
* field changes and on first mount
*/
useEffect( () => {
setNewSetting( {
Expand All @@ -32,141 +29,65 @@ const FieldConfigurator = ( { field, setting, setNewSetting } ) => {
} );
};

const renderFieldConfig = () => {
const fieldConfig = getConfig( field );

if ( ! fieldConfig ) {
return null;
}

if ( fieldConfig.options ) {
return (
<OptionConfig
optionsKey={ fieldConfig.optionsKey }
setting={ setting }
handleAttributeChange={ handleAttributeChange }
optionsHeader={ fieldConfig.optionsHeader }
newOption={ fieldConfig.newOption }
controls={ fieldConfig.controls }
/>
);
}

return fieldConfig.controls.map( ( control, index ) => {
const Control = control.component;
/**
* Dynamically set the prop based on the control componentProp,
* getting the correct value from the setting object.
* E.g checked, value etc
*/
const dynamicProp = {
[ control.componentProp ]:
setting?.[ control.settingKey ] || '',
};

return (
<Control
key={ index }
className={ classNames( {
'form-field--required': control.required,
} ) }
label={ control.label }
required={ control.required }
{ ...dynamicProp }
onChange={ ( value ) =>
handleAttributeChange( control.settingKey, value )
}
/>
);
} );
};

return (
<div className="field-config">
<h3>{ __( 'Field Configuration', 'bb_site_settings' ) }</h3>

<p>
{ __(
'Configure the field for the setting',
'bb_site_settings'
) }
</p>
<div className="field-config__field">
{ field === 'text' && (
<div className="field-config__options">
<TextControl
className="form-field--required"
required
label={ __(
'Label for field',
'bb_site_settings'
) }
value={ setting?.label || '' }
onChange={ ( value ) =>
handleAttributeChange( 'label', value )
}
/>
<TextControl
label={ __(
'Value for field',
'bb_site_settings'
) }
value={ setting?.value || '' }
onChange={ ( value ) => {
handleAttributeChange( 'value', value );
} }
/>
</div>
) }

{ field === 'toggle' && (
<div className="field-config__options">
<TextControl
className="form-field--required"
required
label={ __(
'Label for field',
'bb_site_settings'
) }
value={ setting?.label || '' }
onChange={ ( value ) =>
handleAttributeChange( 'label', value )
}
/>
<ToggleControl
label={ __(
'Value for field',
'bb_site_settings'
) }
checked={ setting?.checked || false }
onChange={ ( value ) => {
handleAttributeChange( 'checked', value );
} }
/>
</div>
) }

{ field === 'radio' && (
<OptionConfig
setting={ setting }
handleAttributeChange={ handleAttributeChange }
optionsHeader={ __(
'Radio options',
'bb_site_settings'
) }
config={ {
newOption: { label: '', value: '' },
controls: [
{
type: TextControl,
required: true,
label: __(
'Option Label',
'bb_site_settings'
),
updateField: 'label',
valueProp: 'value',
},
{
type: TextControl,
label: __(
'Option Value',
'bb_site_settings'
),
updateField: 'value',
valueProp: 'value',
},
],
} }
/>
) }

{ field === 'checkbox-group' && (
<OptionConfig
setting={ setting }
handleAttributeChange={ handleAttributeChange }
optionsHeader={ __(
'Checkbox options',
'bb_site_settings'
) }
config={ {
newOption: { label: '', checked: false },
controls: [
{
type: TextControl,
required: true,
label: __(
'Option Label',
'bb_site_settings'
),
updateField: 'label',
valueProp: 'value',
},
{
type: CheckboxControl,
label: __(
'Option Value',
'bb_site_settings'
),
updateField: 'checked',
valueProp: 'checked',
},
],
} }
/>
) }
</div>
<div className="field-config__field">{ renderFieldConfig() }</div>
</div>
);
};
Expand Down
Loading