forked from impress-org/givewp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add auto-generated, unique field names (impress-org#65)
* wip * wip * wip * wip * refactor: Scope field name to custom field * wip: Add help text to field name control * feature: Validate field name as required, slug * refactor: Use field name in fields API * refactor: move settings into seperate folder and destrcut props inline the function * fix: missing useSelect deps * refactor: move slugify into common folder * refactor: destruct FieldSettings props inline and rename useFieldNames to useFieldNameValidator Co-authored-by: Jon Waldstein <jonwaldstein@jons-air.mynetworksettings.com>
- Loading branch information
Showing
15 changed files
with
299 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
packages/form-builder/src/blocks/fields/settings/DefaultFieldSettings.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {PanelBody, PanelRow, TextControl, ToggleControl} from '@wordpress/components'; | ||
import {__} from '@wordpress/i18n'; | ||
import {InspectorControls} from '@wordpress/block-editor'; | ||
import {noop} from 'lodash'; | ||
|
||
export default function DefaultFieldSettings({attributes, setAttributes, onLabelTextControlBlur = noop}) { | ||
const {label, placeholder, isRequired, options} = attributes; | ||
const requiredClass = isRequired ? 'give-is-required' : ''; | ||
|
||
return ( | ||
<> | ||
<div> | ||
{'undefined' === typeof options ? ( | ||
<TextControl | ||
label={label} | ||
placeholder={placeholder} | ||
required={isRequired} | ||
className={requiredClass} | ||
/> | ||
) : ( | ||
<select> | ||
{options.map((option) => ( | ||
<option key={option.value} value={option.value}> | ||
{option.label} | ||
</option> | ||
))} | ||
</select> | ||
)} | ||
</div> | ||
|
||
<InspectorControls> | ||
<PanelBody title={__('Field Settings', 'give')} initialOpen={true}> | ||
<PanelRow> | ||
<TextControl | ||
label={__('Label', 'give')} | ||
value={label} | ||
onChange={(val) => setAttributes({label: val})} | ||
onBlur={onLabelTextControlBlur} | ||
/> | ||
</PanelRow> | ||
<PanelRow> | ||
<TextControl | ||
label={__('Placeholder', 'give')} | ||
value={placeholder} | ||
onChange={(val) => setAttributes({placeholder: val})} | ||
/> | ||
</PanelRow> | ||
<PanelRow> | ||
<ToggleControl | ||
label={__('Required', 'give')} | ||
checked={isRequired} | ||
onChange={() => setAttributes({isRequired: !isRequired})} | ||
/> | ||
</PanelRow> | ||
</PanelBody> | ||
</InspectorControls> | ||
</> | ||
); | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/form-builder/src/blocks/fields/settings/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {__} from '@wordpress/i18n'; | ||
import DefaultFieldSettings from './DefaultFieldSettings'; | ||
|
||
const defaultSettings = { | ||
title: __('Field', 'custom-block-editor'), | ||
|
||
supports: { | ||
html: false, // Removes support for an HTML mode. | ||
multiple: true, | ||
}, | ||
|
||
attributes: { | ||
fieldName: { | ||
type: 'string', | ||
source: 'attribute', | ||
}, | ||
label: { | ||
type: 'string', | ||
source: 'attribute', | ||
default: __('Text Field', 'give'), | ||
}, | ||
placeholder: { | ||
type: 'string', | ||
source: 'attribute', | ||
default: '', | ||
}, | ||
isRequired: { | ||
type: 'boolean', | ||
source: 'attribute', | ||
default: false, | ||
}, | ||
options: { | ||
type: 'array', | ||
}, | ||
}, | ||
|
||
edit: DefaultFieldSettings, | ||
|
||
save: function () { | ||
return null; // Save as attributes - not rendered HTML. | ||
}, | ||
}; | ||
|
||
export default defaultSettings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export {default as Storage} from './storage'; | ||
export {default as slugify} from './slugify'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function slugify(value){ | ||
return value | ||
.toLowerCase() | ||
.replace(/\s|_/g, '-') // Replace spaces and underscores with dashes | ||
.replace(/[^a-zA-Z\d\s-]/g, '') // Replace non-alphanumeric characters (other than dashes) | ||
.replace(/-$/g, ''); // Remove trailing dash | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import useToggleState from "./useToggleState"; | ||
import useFieldNameValidator from './useFieldNameValidator'; | ||
import useToggleState from './useToggleState'; | ||
|
||
export { | ||
useToggleState, | ||
}; | ||
export {useFieldNameValidator, useToggleState}; |
Oops, something went wrong.