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

QuickEdit: Add slug field control #65196

Merged
merged 21 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,12 @@
"markdown_source": "../packages/eslint-plugin/README.md",
"parent": "packages"
},
{
"title": "@wordpress/fields",
"slug": "packages-fields",
"markdown_source": "../packages/fields/README.md",
"parent": "packages"
},
{
"title": "@wordpress/format-library",
"slug": "packages-format-library",
Expand Down
48 changes: 48 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@wordpress/editor": "file:packages/editor",
"@wordpress/element": "file:packages/element",
"@wordpress/escape-html": "file:packages/escape-html",
"@wordpress/fields": "file:packages/fields",
"@wordpress/format-library": "file:packages/format-library",
"@wordpress/hooks": "file:packages/hooks",
"@wordpress/html-entities": "file:packages/html-entities",
Expand Down
1 change: 1 addition & 0 deletions packages/dependency-extraction-webpack-plugin/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const BUNDLED_PACKAGES = [
'@wordpress/interface',
'@wordpress/sync',
'@wordpress/undo-manager',
'@wordpress/fields',
];

/**
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@wordpress/editor": "file:../editor",
"@wordpress/element": "file:../element",
"@wordpress/escape-html": "file:../escape-html",
"@wordpress/fields": "file:../fields",
"@wordpress/hooks": "file:../hooks",
"@wordpress/html-entities": "file:../html-entities",
"@wordpress/i18n": "file:../i18n",
Expand Down
9 changes: 8 additions & 1 deletion packages/edit-site/src/components/post-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ function PostEditForm( { postType, postId } ) {
);
const form = {
type: 'panel',
fields: [ 'title', 'status', 'date', 'author', 'comment_status' ],
fields: [
'title',
'status',
'date',
'author',
'slug',
'comment_status',
],
};
const onChange = ( edits ) => {
for ( const id of ids ) {
Expand Down
8 changes: 8 additions & 0 deletions packages/edit-site/src/components/post-fields/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { __experimentalHStack as HStack, Icon } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEntityRecords, store as coreStore } from '@wordpress/core-data';
import { SlugView, SlugEdit } from '@wordpress/fields';

/**
* Internal dependencies
Expand Down Expand Up @@ -369,6 +370,13 @@ function usePostFields( viewType ) {
return <time>{ getFormattedDate( item.date ) }</time>;
},
},
{
Copy link
Member

Choose a reason for hiding this comment

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

This works because our APIs are already extensible. However, there's an alternative that @joshuatf mentioned somewhere. Instead of doing:

{
  id: 'slug',
  Edit: /* custom slug Edit */,
  render: /* custom slug render */,
  isValid: /* custom slug isValid */,
  // etc.
}

we can do something along these lines:

registerFieldType( 'core/slug', FieldSlug );

const fields = [
  {
    id: 'slug',
    type: 'core/slug',
  }
];

This has a few advantages:

  • It gets us a bit closer to making the type field required. We're not there there yet because there are a couple of missing types we need (image, for example), but we should aim to do it.

  • We offer higher semantics and gets us a bit closer to having no-code JSON-oriented fields. This may sound a bit abstract, but imagine the following scenario: in the future, there's a mechanism for users to create screens based on declaring which fields they want from a dataset. All via UI. In that scenario, that UI needs to be no-code, and users shouldn't have to provide a "custom render", "custom edit", "custom validation", etc. if they just want a field to be a "slug". Instead, they should work with types: the basic ones provided by DataViews (string, integer, date, etc.) but also any other one that is domain-specific (slug, cart-status, etc.).

Another aspect of this PR is what should be the API offered by the wordpress/field package?

This suggest it should export individual field properties (render, edit, isValid, etc.). There are a few. What if it exported the whole field instead? An advantage of exporting the whole Field is that the wordpress/field package can implement new APIs as they come without having to modify the consumers:

// The consumer doesn't have to modify any code when
// a new Field property is introduced, renamed it, etc.
// It's the FieldSlug responsibility to implement it.

const fields = [
  {
    id: 'slug',
    type: 'core/slug'
  }
];

label: __( 'Link' ),
id: 'slug',
getValue: ( { item } ) => item.slug,
render: SlugView,
Edit: SlugEdit,
},
{
id: 'comment_status',
label: __( 'Discussion' ),
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/style.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import "../../dataviews/src/style.scss";
@import "../../fields/src/styles.scss";

@import "./components/add-new-template/style.scss";
@import "./components/block-editor/style.scss";
Expand Down
1 change: 1 addition & 0 deletions packages/fields/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 5 additions & 0 deletions packages/fields/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->

## Unreleased

Initial release.
33 changes: 33 additions & 0 deletions packages/fields/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Fields

This package provides core elements for the DataView library, designed to simplify the creation and management of data display elements in WordPress.

## Installation

Install the module

```bash
npm install @wordpress/fields --save
```

## Usage

<!-- START TOKEN(Autogenerated API docs) -->

### SlugEdit

Undocumented declaration.

### SlugView

Undocumented declaration.

<!-- END TOKEN(Autogenerated API docs) -->

## Contributing to this package

This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to [npm](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) as well as other software projects.

To find out more about contributing to this package or Gutenberg as a whole, please read the project's main [contributor guide](https://github.com/WordPress/gutenberg/tree/HEAD/CONTRIBUTING.md).

<br /><br /><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
52 changes: 52 additions & 0 deletions packages/fields/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@wordpress/fields",
"version": "0.0.1",
"description": "DataViews is a component that provides an API to render datasets using different types of layouts (table, grid, list, etc.).",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"keywords": [
"wordpress",
"gutenberg",
"dataviews"
],
"homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/packages/fields/README.md",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/gutenberg.git",
"directory": "packages/fields"
},
"bugs": {
"url": "https://github.com/WordPress/gutenberg/issues"
},
"engines": {
"node": ">=18.12.0",
"npm": ">=8.19.2"
},
"main": "build/index.js",
"module": "build-module/index.js",
"types": "build-types",
"sideEffects": [
"build-style/**",
"src/**/*.scss"
],
"dependencies": {
"@babel/runtime": "^7.16.0",
"@wordpress/components": "file:../components",
"@wordpress/compose": "file:../compose",
"@wordpress/data": "file:../data",
"@wordpress/element": "file:../element",
"@wordpress/i18n": "file:../i18n",
"@wordpress/icons": "file:../icons",
"@wordpress/notices": "file:../notices",
"@wordpress/primitives": "file:../primitives",
"@wordpress/private-apis": "file:../private-apis",
"@wordpress/url": "file:../url",
"@wordpress/warning": "file:../warning"
},
"peerDependencies": {
"react": "^18.0.0"
},
"publishConfig": {
"access": "public"
}
}
2 changes: 2 additions & 0 deletions packages/fields/src/fields/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as SlugEdit } from './slug/slug.edit';
export { default as SlugView } from './slug/slug.view';
Loading
Loading