-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fields package: Add Storybook examples #71864
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
Merged
andrewserong
merged 2 commits into
trunk
from
try/add-storybook-examples-to-fields-package
Sep 25, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,187 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useState } from '@wordpress/element'; | ||
| import { DataForm, DataViews, type Form } from '@wordpress/dataviews'; | ||
| import type { Field, View } from '@wordpress/dataviews'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
|
|
||
| import { | ||
| authorField, | ||
| commentStatusField, | ||
| dateField, | ||
| orderField, | ||
| passwordField, | ||
| slugField, | ||
| statusField, | ||
| titleField, | ||
| } from '../fields'; | ||
|
|
||
| // Fields not yet covered: | ||
| // featuredImageField, | ||
| // pageTitleField, | ||
| // parentField, | ||
| // patternTitleField, | ||
| // templateField, | ||
| // templateTitleField, | ||
|
|
||
| import type { BasePost, BasePostWithEmbeddedAuthor } from '../types'; | ||
|
|
||
| export default { | ||
| title: 'Fields/Base Fields', | ||
| component: DataForm, | ||
| }; | ||
|
|
||
| // Sample data for different field types | ||
| const sampleBasePost: BasePost = { | ||
| id: 1, | ||
| title: { rendered: 'Sample Post Title', raw: 'Sample Post Title' }, | ||
| content: { | ||
| rendered: '<p>This is sample content.</p>', | ||
| raw: 'This is sample content.', | ||
| }, | ||
| type: 'post', | ||
| slug: 'sample-post-title', | ||
| permalink_template: 'http://localhost:8888/%postname%/', | ||
| date: '2024-01-15T10:30:00', | ||
| modified: '2024-01-20T14:45:00', | ||
| status: 'publish', | ||
| comment_status: 'open', | ||
| password: '', | ||
| parent: 0, | ||
| menu_order: 0, | ||
| author: 1, | ||
| featured_media: 123, | ||
| template: 'single', | ||
| }; | ||
|
|
||
| const samplePostWithAuthor: BasePostWithEmbeddedAuthor = { | ||
| ...sampleBasePost, | ||
| _embedded: { | ||
| author: [ | ||
| { | ||
| name: 'John Doe', | ||
| avatar_urls: { | ||
| '24': 'https://gravatar.com/avatar?d=retro&s=24', | ||
| '48': 'https://gravatar.com/avatar?d=retro&s=48', | ||
| '96': 'https://gravatar.com/avatar?d=retro&s=96', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| // Create a showcase of all base fields. | ||
| // This does not include fields that require more complex setups, | ||
| // however this could be extended in the future, by setting up the data | ||
| // stores to contain entities like pages, users, media, etc. | ||
| const showcaseFields: Field< any >[] = [ | ||
| titleField, | ||
| slugField, | ||
| statusField, | ||
| dateField, | ||
| authorField, | ||
| commentStatusField, | ||
| passwordField, | ||
| orderField, | ||
| ]; | ||
|
|
||
| const DataFormsComponent = ( { type }: { type: 'regular' | 'panel' } ) => { | ||
| const [ data, setData ] = useState( samplePostWithAuthor ); | ||
|
|
||
| const handleChange = ( updates: Partial< BasePostWithEmbeddedAuthor > ) => { | ||
| setData( ( prev ) => ( { ...prev, ...updates } ) ); | ||
| }; | ||
|
|
||
| // Form configuration for the showcase. | ||
| const showcaseForm: Form = { | ||
| layout: { | ||
| type, | ||
| }, | ||
| fields: [ | ||
| 'title', | ||
| 'slug', | ||
| 'status', | ||
| 'date', | ||
| 'author', | ||
| 'comment_status', | ||
| 'password', | ||
| 'menu_order', | ||
| ], | ||
| }; | ||
|
|
||
| return ( | ||
| <div style={ { padding: '20px' } }> | ||
| <h2>Base Fields</h2> | ||
| <p> | ||
| This story demonstrates all the base fields from the | ||
| @wordpress/fields package within a DataForm. | ||
| </p> | ||
|
|
||
| <DataForm | ||
| data={ data } | ||
| fields={ showcaseFields } | ||
| form={ showcaseForm } | ||
| onChange={ handleChange } | ||
| /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const DataFormsPreview = { | ||
| render: DataFormsComponent, | ||
| argTypes: { | ||
| type: { | ||
| control: { type: 'select' }, | ||
| description: 'Choose the layout type.', | ||
| options: [ 'regular', 'panel' ], | ||
| }, | ||
| }, | ||
| args: { | ||
| type: 'regular', | ||
| }, | ||
| }; | ||
|
|
||
| export const DataViewsPreview = () => { | ||
| const [ view, setView ] = useState< View >( { | ||
| type: 'table', | ||
| fields: showcaseFields.map( ( f ) => f.id ), | ||
| titleField: 'title', | ||
| descriptionField: undefined, | ||
| mediaField: undefined, | ||
| } ); | ||
| const [ data ] = useState( [ samplePostWithAuthor ] ); | ||
|
|
||
| const paginationInfo = { | ||
| totalItems: 1, | ||
| totalPages: 1, | ||
| }; | ||
|
|
||
| const defaultLayouts = { | ||
| table: {}, | ||
| list: {}, | ||
| grid: {}, | ||
| }; | ||
|
|
||
| return ( | ||
| <div style={ { padding: '20px' } }> | ||
| <h2>Fields Package DataViews Preview</h2> | ||
| <p> | ||
| This story demonstrates all the base fields from the | ||
| @wordpress/fields package, rendered in a DataViews component, | ||
| allowing preview of view state and layout switching. | ||
| </p> | ||
| <DataViews | ||
| data={ data } | ||
| fields={ showcaseFields } | ||
| view={ view } | ||
| onChangeView={ ( nextView: View ) => setView( nextView ) } | ||
| paginationInfo={ paginationInfo } | ||
| defaultLayouts={ defaultLayouts } | ||
| /> | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
| @import "../../packages/fields/build-style/style.css"; |
This file contains hidden or 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 @@ | ||
| @import "../../packages/fields/build-style/style-rtl.css"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just curious where is this used. I can't see John in the fields! 😄
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.
I know, how disappointing! I wasn't quite sure how far we want to go with setting up the testing environment for the author within the Storybook example, so this is a remnant of my explorations there. For this one, it won't show us the avatar unless we inject some stuff into the state, as it turns out it's using the
avatar_urlsfrom a call togetEntityRecord.To satisfy the type, though, I believe we need to have
avatar_urlsbe set, but it could always be set to an empty object intead.