Skip to content

Commit

Permalink
Storybook: Add PlainText Storybook stories (#67341)
Browse files Browse the repository at this point in the history
* Storybook: Add PlainText Storybook stories

- Add Default, LongText, and WithClassName stories
- Configure meta for PlainText component

* Refactor: `PlainText` stories and removed useEffect

* Refactor: Remove comment from story

* Storybook: improve `PlainText` JSDoc and storybook descriptions

- Enhance prop descriptions
- Fix import order in storybook.

* Storybook: Unify value and onChange prop descriptions

Co-authored-by: SainathPoojary <[email protected]>
Co-authored-by: t-hamano <[email protected]>
  • Loading branch information
3 people authored Dec 20, 2024
1 parent dea955c commit ad073b0
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 2 deletions.
40 changes: 40 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -713,10 +713,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 }
/>
);
},
};

1 comment on commit ad073b0

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in ad073b0.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12424678327
📝 Reported issues:

Please sign in to comment.