-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Text Component * Rename * Lint fixes
- Loading branch information
Showing
7 changed files
with
195 additions
and
3 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
114 changes: 114 additions & 0 deletions
114
packages/block-editor/src/components/editable/README.md
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,114 @@ | ||
# `Editable` | ||
|
||
Renders an editable text input in which text formatting is not allowed. | ||
|
||
## Properties | ||
|
||
### `value: String` | ||
|
||
*Required.* String to make editable. | ||
|
||
### `onChange( value: String ): Function` | ||
|
||
*Required.* Called when the value changes. | ||
|
||
### `tagName: String` | ||
|
||
*Default: `div`.* The [tag name](https://www.w3.org/TR/html51/syntax.html#tag-name) of the editable element. Elements that display inline are not supported. | ||
|
||
### `disableLineBreaks: Boolean` | ||
|
||
*Optional.* `Text` won't insert line breaks on `Enter` if set to `true`. | ||
|
||
### `placeholder: String` | ||
|
||
*Optional.* Placeholder text to show when the field is empty, similar to the | ||
[`input` and `textarea` attribute of the same name](https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/HTML5_updates#The_placeholder_attribute). | ||
|
||
### `keepPlaceholderOnFocus: Boolean` | ||
|
||
*Optional.* Show placeholder even when selected/focused, as long as there is no content. | ||
|
||
### `onSplit( value: String ): Function` | ||
|
||
*Optional.* Called when the content can be split, where `value` is a piece of content being split off. Here you should create a new block with that content and return it. Note that you also need to provide `onReplace` in order for this to take any effect. | ||
|
||
### `onReplace( blocks: Array ): Function` | ||
|
||
*Optional.* Called when the `Text` instance can be replaced with the given blocks. | ||
|
||
### `onMerge( forward: Boolean ): Function` | ||
|
||
*Optional.* Called when blocks can be merged. `forward` is true when merging with the next block, false when merging with the previous block. | ||
|
||
### `onRemove( forward: Boolean ): Function` | ||
|
||
*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 | ||
|
||
`Text.Content` should be used in the `save` function of your block to correctly save text content. | ||
|
||
## Example | ||
|
||
{% codetabs %} | ||
{% ES5 %} | ||
```js | ||
wp.blocks.registerBlockType( /* ... */, { | ||
// ... | ||
|
||
attributes: { | ||
content: { | ||
source: 'html', | ||
selector: 'div', | ||
}, | ||
}, | ||
|
||
edit: function( props ) { | ||
return wp.element.createElement( wp.editor.Editable, { | ||
className: props.className, | ||
value: props.attributes.content, | ||
onChange: function( content ) { | ||
props.setAttributes( { content: content } ); | ||
} | ||
} ); | ||
}, | ||
|
||
save: function( props ) { | ||
return wp.element.createElement( wp.editor.Editable.Content, { | ||
value: props.attributes.content | ||
} ); | ||
} | ||
} ); | ||
``` | ||
{% ESNext %} | ||
```js | ||
const { registerBlockType } = wp.blocks; | ||
const { Editable } = wp.editor; | ||
|
||
registerBlockType( /* ... */, { | ||
// ... | ||
|
||
attributes: { | ||
content: { | ||
source: 'html', | ||
selector: '.text', | ||
}, | ||
}, | ||
|
||
edit( { className, attributes, setAttributes } ) { | ||
return ( | ||
<Editable | ||
className={ className } | ||
value={ attributes.content } | ||
onChange={ ( content ) => setAttributes( { content } ) } | ||
/> | ||
); | ||
}, | ||
|
||
save( { attributes } ) { | ||
return <Editable.Content value={ attributes.content } />; | ||
} | ||
} ); | ||
``` | ||
{% end %} |
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,17 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import RichText from '../rich-text'; | ||
|
||
function Editable( props ) { | ||
return <RichText { ...props } __unstableDisableFormats />; | ||
} | ||
|
||
Editable.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 Editable; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEntityProp } from '@wordpress/core-data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { Editable } from '@wordpress/block-editor'; | ||
|
||
export default function SiteTitleEdit() { | ||
const [ title, setTitle ] = useEntityProp( 'root', 'site', 'title' ); | ||
return ( | ||
<Editable | ||
tagName="h1" | ||
placeholder={ __( 'Site Title' ) } | ||
value={ title } | ||
onChange={ setTitle } | ||
disableLineBreaks | ||
/> | ||
); | ||
} |
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