-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: (editor-utils) EditorForm component. (#1559)
* Feat: (editor-utils) EditorForm component. * Refactor: Removed unused code in Edit.tsx * Testing: Add unit tests.
- Loading branch information
Showing
7 changed files
with
330 additions
and
14,050 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
10 changes: 8 additions & 2 deletions
10
packages/block-editor-utils/src/components/EditFormFields.tsx
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as React from 'react'; | ||
import { BlockConfiguration, BlockEditProps } from '@wordpress/blocks'; | ||
import { Icon } from '@wordpress/components'; | ||
import { applyFilters } from '@wordpress/hooks'; | ||
import { Control, Field } from '../types/index.js'; | ||
|
||
const styles = { | ||
form: { | ||
padding: '0 10px', | ||
margin: '20px 0', | ||
} as React.CSSProperties, | ||
icon: { | ||
marginRight: '10px', | ||
} as React.CSSProperties, | ||
heading: { | ||
margin: '10px 0', | ||
display: 'flex', | ||
alignItems: 'center', | ||
} as React.CSSProperties, | ||
}; | ||
|
||
interface EditorFormProps<T extends Record<string, any>> { | ||
fields: Field[]; | ||
props: BlockEditProps<T>; | ||
blockJson: BlockConfiguration; | ||
} | ||
|
||
function EditorForm<T extends Record<string, any>>({ | ||
fields, | ||
props, | ||
blockJson, | ||
}: EditorFormProps<T>) { | ||
const loadedControls = applyFilters('faustBlockEditorUtils.controls', {}) as { | ||
[key: string]: Control; | ||
}; | ||
return ( | ||
<div | ||
className="faust-editor-form" | ||
aria-label="Faust block editor form" | ||
style={styles.form}> | ||
<h3 className="faust-editor-form__heading" style={styles.heading}> | ||
<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) { | ||
return null; | ||
} | ||
return ( | ||
<ControlField | ||
config={field} | ||
props={props} | ||
key={`editor-field-${field.name}`} | ||
/> | ||
); | ||
})} | ||
</div> | ||
); | ||
} | ||
|
||
export default EditorForm; |
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
96 changes: 96 additions & 0 deletions
96
packages/block-editor-utils/tests/components/EditorForm.test.tsx
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,96 @@ | ||
import * as React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import EditorForm from '../../src/components/EditorForm.js'; | ||
import { actions, filters, addFilter } from '@wordpress/hooks'; | ||
import { Control, Field } from '../../src/types/index.js'; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
beforeEach(() => { | ||
[actions, filters].forEach((hooks) => { | ||
for (const k in hooks) { | ||
if ('__current' === k) { | ||
continue; | ||
} | ||
|
||
delete hooks[k]; | ||
} | ||
delete hooks.all; | ||
}); | ||
}); | ||
|
||
function filterA(controls: { [key: string]: Control }) { | ||
// eslint-disable-next-line no-param-reassign | ||
controls.color = () => <div>Another Color</div>; | ||
return controls; | ||
} | ||
|
||
const blockProps = { | ||
clientId: '1', | ||
setAttributes: () => null, | ||
context: {}, | ||
attributes: { | ||
message: 'Hello', | ||
}, | ||
isSelected: false, | ||
className: 'SimpleBlock', | ||
}; | ||
|
||
const blockJson = { | ||
title: 'SimpleBlock', | ||
icon: 'star', | ||
category: 'text', | ||
attributes: {}, | ||
}; | ||
|
||
describe('<EditorForm />', () => { | ||
it('renders an empty EditorForm if no fields are provided', () => { | ||
const fields: Field[] = []; | ||
addFilter('faustBlockEditorUtils.controls', 'my_callback', filterA); | ||
render( | ||
<EditorForm fields={fields} props={blockProps} blockJson={blockJson} />, | ||
); | ||
expect(screen.getByLabelText('Faust block editor form')) | ||
.toMatchInlineSnapshot(` | ||
<div | ||
aria-label="Faust block editor form" | ||
class="faust-editor-form" | ||
style="padding: 0px 10px; margin: 20px 0px;" | ||
> | ||
<h3 | ||
class="faust-editor-form__heading" | ||
style="margin: 10px 0px; display: flex; align-items: center;" | ||
> | ||
<span | ||
class="dashicon dashicons dashicons-star" | ||
style="font-size: 24px; width: 24px; height: 24px; margin-right: 10px;" | ||
/> | ||
SimpleBlock | ||
</h3> | ||
</div> | ||
`); | ||
}); | ||
it('renders EditorForm if matching fields are provided', () => { | ||
const fields: Field[] = [ | ||
{ | ||
type: 'string', | ||
control: 'color', | ||
name: 'myColor', | ||
location: 'editor', | ||
}, | ||
{ | ||
type: 'string', | ||
control: 'text', | ||
name: 'myText', | ||
location: 'inspector', | ||
}, | ||
]; | ||
addFilter('faustBlockEditorUtils.controls', 'my_callback', filterA); | ||
render( | ||
<EditorForm fields={fields} props={blockProps} blockJson={blockJson} />, | ||
); | ||
expect(screen.getAllByText('Another Color')).toHaveLength(1); | ||
}); | ||
}); |
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
350dc30
There was a problem hiding this comment.
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:
Learn more about building on Atlas in our documentation.