Skip to content

Commit

Permalink
[Merl 1149] Add TextControl field (#1560)
Browse files Browse the repository at this point in the history
* Feat: (editor-utils) EditorForm component.

* Refactor: Removed unused code in Edit.tsx

* Testing: Add unit tests.

* Feat:(editor-utils) Text Control.

* Chore: Changeset

* Style: Border set in EditorForm only.

* Tests:(block-editor-utils) Update snapshots.
  • Loading branch information
theodesp authored Sep 7, 2023
1 parent b2ad517 commit db848c4
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-nails-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/block-editor-utils': patch
---

Feat: Handle TextControl fields in block-editor-utils.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
root: true,
env: {
browser: true,
es6: true,
Expand Down
6 changes: 6 additions & 0 deletions packages/block-editor-utils/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/restrict-template-expressions": "off"
}
};
2 changes: 1 addition & 1 deletion packages/block-editor-utils/src/components/EditorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const styles = {
form: {
padding: '0 10px',
margin: '20px 0',
border: '1px solid black',
} as React.CSSProperties,
icon: {
marginRight: '10px',
Expand Down Expand Up @@ -42,7 +43,6 @@ function EditorForm<T extends Record<string, any>>({
<Icon size={24} icon={blockJson.icon} style={styles.icon} />
{blockJson.title}
</h3>
{/* eslint-disable-next-line @typescript-eslint/no-unsafe-call */}
{fields.map((field: Field) => {
const ControlField = loadedControls[field.control];
if (!ControlField) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function InspectorFields<T extends Record<string, any>>({
return (
<InspectorControls key="FaustBlockInspectorControls">
<>
{/* eslint-disable-next-line @typescript-eslint/no-unsafe-call */}
{fields.map((field: Field) => {
const ControlField = loadedControls[field.control];
if (!ControlField) {
Expand All @@ -29,7 +28,6 @@ function InspectorFields<T extends Record<string, any>>({
return (
<PanelBody
className="faust-inspector-form-field"
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
key={`inspector-controls-panel-${field.name}`}>
<ControlField config={field} props={props} />
</PanelBody>
Expand Down
21 changes: 21 additions & 0 deletions packages/block-editor-utils/src/controls/Text.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as React from 'react';
import { TextControl } from '@wordpress/components';
import { ControlProps } from '../types/index.js';

function Text<T extends Record<string, any>>({
config,
props,
}: ControlProps<T>) {
const onChange = (newContent: string) => {
props.setAttributes({ [config.name]: newContent });
};
return (
<TextControl
label={config.label}
value={props.attributes[config.name]}
onChange={onChange}
/>
);
}

export default Text;
4 changes: 4 additions & 0 deletions packages/block-editor-utils/src/controls/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import registerControl from '../helpers/registerControl.js';
import Text from './Text.js';

registerControl('text', Text);
16 changes: 16 additions & 0 deletions packages/block-editor-utils/src/helpers/registerControl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { addFilter } from '@wordpress/hooks';
import { Control, FieldControl } from '../types/index.js';

function registerControl(name: FieldControl, control: Control) {
addFilter(
'faustBlockEditorUtils.controls',
'faust-block-editor-utils',
(controls: { [key: string]: Control }) => {
// eslint-disable-next-line no-param-reassign
controls[name] = control;
return controls;
},
);
}

export default registerControl;
1 change: 1 addition & 0 deletions packages/block-editor-utils/src/registerFaustBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import DefaultSaveFn from './components/Save.js';
import DefaultEditFn from './components/Edit.js';
import { BlockFC, ConfigType } from './types/index.js';
import './controls/index.js';

export interface RegisterFaustBlockMetadata<T extends Record<string, any>> {
// The block.json metadata object
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor-utils/tests/components/Edit.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('<Edit />', () => {
<div
aria-label="Faust block editor form"
class="faust-editor-form"
style="padding: 0px 10px; margin: 20px 0px;"
style="padding: 0px 10px; margin: 20px 0px; border: 1px solid black;"
>
<h3
class="faust-editor-form__heading"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('<EditorForm />', () => {
<div
aria-label="Faust block editor form"
class="faust-editor-form"
style="padding: 0px 10px; margin: 20px 0px;"
style="padding: 0px 10px; margin: 20px 0px; border: 1px solid black;"
>
<h3
class="faust-editor-form__heading"
Expand Down

1 comment on commit db848c4

@headless-platform-by-wp-engine

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out the recent updates to your Atlas environment:

App Environment URL Build
faustjs canary https://hg…wered.com ✅ (logs)

Learn more about building on Atlas in our documentation.

Please sign in to comment.