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

Storybook: Add PlainText Storybook stories #67341

Merged
merged 5 commits into from
Dec 20, 2024
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
40 changes: 40 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,50 @@ Undocumented declaration.

### PlainText

Render an auto-growing textarea allow users to fill any textual content.

_Related_

- <https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/plain-text/README.md>

_Usage_

```jsx
import { registerBlockType } from '@wordpress/blocks';
import { PlainText } from '@wordpress/block-editor';

registerBlockType( 'my-plugin/example-block', {
// ...

attributes: {
content: {
type: 'string',
},
},

edit( { className, attributes, setAttributes } ) {
return (
<PlainText
className={ className }
value={ attributes.content }
onChange={ ( content ) => setAttributes( { content } ) }
/>
);
},
} );
```

_Parameters_

- _props_ `Object`: Component props.
- _props.value_ `string`: String value of the textarea.
- _props.onChange_ `Function`: Function called when the text value changes.
- _props.ref_ `[Object]`: The component forwards the `ref` property to the `TextareaAutosize` component.

_Returns_

- `Element`: Plain text component

### privateApis

Private @wordpress/block-editor APIs.
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/plain-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ Render an auto-growing textarea allow users to fill any textual content.

### `value: string`

_Required._ String value of the textarea
_Required._ String value of the textarea.

### `onChange( value: string ): Function`

_Required._ Called when the value changes.
_Required._ Function called when the text value changes.

You can also pass any extra prop to the textarea rendered by this component.

Expand Down
34 changes: 34 additions & 0 deletions packages/block-editor/src/components/plain-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,41 @@ import { forwardRef } from '@wordpress/element';
import EditableText from '../editable-text';

/**
* Render an auto-growing textarea allow users to fill any textual content.
*
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/plain-text/README.md
*
* @example
* ```jsx
* import { registerBlockType } from '@wordpress/blocks';
* import { PlainText } from '@wordpress/block-editor';
*
* registerBlockType( 'my-plugin/example-block', {
* // ...
*
* attributes: {
* content: {
* type: 'string',
* },
* },
*
* edit( { className, attributes, setAttributes } ) {
* return (
* <PlainText
* className={ className }
* value={ attributes.content }
* onChange={ ( content ) => setAttributes( { content } ) }
* />
* );
* },
* } );
* ````
*
* @param {Object} props Component props.
* @param {string} props.value String value of the textarea.
* @param {Function} props.onChange Function called when the text value changes.
* @param {Object} [props.ref] The component forwards the `ref` property to the `TextareaAutosize` component.
* @return {Element} Plain text component
*/
const PlainText = forwardRef( ( { __experimentalVersion, ...props }, ref ) => {
if ( __experimentalVersion === 2 ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import PlainText from '..';

const meta = {
title: 'BlockEditor/PlainText',
component: PlainText,
parameters: {
docs: {
canvas: { sourceState: 'shown' },
description: {
component:
'PlainText renders an auto-growing textarea that allows users to enter any textual content.',
},
},
},
argTypes: {
value: {
control: {
type: null,
},
table: {
type: {
summary: 'string',
},
},
description: 'String value of the textarea.',
},
onChange: {
action: 'onChange',
control: {
type: null,
},
table: {
type: {
summary: 'function',
},
},
description: 'Function called when the text value changes.',
},
className: {
control: 'text',
table: {
type: {
summary: 'string',
},
},
description: 'Additional class name for the PlainText.',
},
},
};

export default meta;

export const Default = {
render: function Template( { onChange, ...args } ) {
const [ value, setValue ] = useState();
return (
<PlainText
{ ...args }
onChange={ ( ...changeArgs ) => {
onChange( ...changeArgs );
setValue( ...changeArgs );
} }
value={ value }
/>
);
},
};
Loading