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

PlainText v2 #21076

Merged
merged 1 commit into from
Mar 23, 2020
Merged
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
4 changes: 0 additions & 4 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,6 @@ _Returns_

Undocumented declaration.

<a name="Editable" href="#Editable">#</a> **Editable**

Renders an editable text input in which text formatting is not allowed.

<a name="FontSizePicker" href="#FontSizePicker">#</a> **FontSizePicker**

Undocumented declaration.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `Editable`
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if I should move these docs to plain text, even though it's an experimental v2.

# `EditableText`

Renders an editable text input in which text formatting is not allowed.

Expand Down Expand Up @@ -45,9 +45,9 @@ Renders an editable text input in which text formatting is not allowed.

*Optional.* Called when the block can be removed. `forward` is true when the selection is expected to move to the next block, false to the previous block.

## Editable.Content
## EditableText.Content

`Text.Content` should be used in the `save` function of your block to correctly save text content.
`EditableText.Content` should be used in the `save` function of your block to correctly save text content.

## Example

Expand All @@ -65,7 +65,7 @@ wp.blocks.registerBlockType( /* ... */, {
},

edit: function( props ) {
return wp.element.createElement( wp.editor.Editable, {
return wp.element.createElement( wp.editor.EditableText, {
className: props.className,
value: props.attributes.content,
onChange: function( content ) {
Expand All @@ -75,7 +75,7 @@ wp.blocks.registerBlockType( /* ... */, {
},

save: function( props ) {
return wp.element.createElement( wp.editor.Editable.Content, {
return wp.element.createElement( wp.editor.EditableText.Content, {
value: props.attributes.content
} );
}
Expand All @@ -84,7 +84,7 @@ wp.blocks.registerBlockType( /* ... */, {
{% ESNext %}
```js
const { registerBlockType } = wp.blocks;
const { Editable } = wp.editor;
const { EditableText } = wp.editor;

registerBlockType( /* ... */, {
// ...
Expand All @@ -98,7 +98,7 @@ registerBlockType( /* ... */, {

edit( { className, attributes, setAttributes } ) {
return (
<Editable
<EditableText
className={ className }
value={ attributes.content }
onChange={ ( content ) => setAttributes( { content } ) }
Expand All @@ -107,7 +107,7 @@ registerBlockType( /* ... */, {
},

save( { attributes } ) {
return <Editable.Content value={ attributes.content } />;
return <EditableText.Content value={ attributes.content } />;
}
} );
```
Expand Down
22 changes: 22 additions & 0 deletions packages/block-editor/src/components/editable-text/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* WordPress dependencies
*/
import { forwardRef } from '@wordpress/element';

/**
* Internal dependencies
*/
import RichText from '../rich-text';

const EditableText = forwardRef( ( props, ref ) => {
return <RichText ref={ ref } { ...props } __unstableDisableFormats />;
} );

EditableText.Content = ( { value = '', tagName: Tag = 'div', ...props } ) => {
return <Tag { ...props }>{ value }</Tag>;
};

/**
* Renders an editable text input in which text formatting is not allowed.
*/
export default EditableText;
17 changes: 0 additions & 17 deletions packages/block-editor/src/components/editable/index.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export {
RichTextToolbarButton,
__unstableRichTextInputEvent,
} from './rich-text';
export { default as Editable } from './editable';
export { default as ToolSelector } from './tool-selector';
export { default as URLInput } from './url-input';
export { default as URLInputButton } from './url-input/button';
Expand Down
21 changes: 16 additions & 5 deletions packages/block-editor/src/components/plain-text/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
/**
* External dependencies
*/
import TextareaAutosize from 'react-autosize-textarea';
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { forwardRef } from '@wordpress/element';

/**
* External dependencies
* Internal dependencies
*/
import TextareaAutosize from 'react-autosize-textarea';
import classnames from 'classnames';
import EditableText from '../editable-text';

/**
* @see https://github.com/WordPress/gutenberg/blob/master/packages/block-editor/src/components/plain-text/README.md
*/
const PlainText = forwardRef( ( { onChange, className, ...props }, ref ) => {
const PlainText = forwardRef( ( { __experimentalVersion, ...props }, ref ) => {
if ( __experimentalVersion === 2 ) {
return <EditableText ref={ ref } { ...props } />;
}

const { className, onChange, ...remainingProps } = props;

return (
<TextareaAutosize
ref={ ref }
className={ classnames( 'block-editor-plain-text', className ) }
onChange={ ( event ) => onChange( event.target.value ) }
{ ...props }
{ ...remainingProps }
/>
);
} );
Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/site-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
*/
import { useEntityProp } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';
import { Editable } from '@wordpress/block-editor';
import { PlainText } from '@wordpress/block-editor';

export default function SiteTitleEdit() {
const [ title, setTitle ] = useEntityProp( 'root', 'site', 'title' );
return (
<Editable
<PlainText
__experimentalVersion={ 2 }
tagName="h1"
placeholder={ __( 'Site Title' ) }
value={ title }
Expand Down