最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Gutenberg block editor - list of form components for custom blocks

programmeradmin1浏览0评论

I'm trying to learn how to create blocks for the block editor. I can't seem to find a list of form elements to add. For example, I need a toggle switch, dropdown, etc.

I found THIS toggle, but I'm not sure how to use it and can't find any examples.

Here is my code, so far I have been able to add PlainText, RichText and MediaUpload. Is there a list of these somewhere? More examples?

const { RichText, MediaUpload, PlainText } = wp.editor;
const { registerBlockType } = wp.blocks;
const { Button } = wpponents;

import './style.scss';
import './editor.scss';

registerBlockType('card-block/main', {
    title: 'Slider',
    icon: 'images-alt2',
    category: 'common',
    attributes: {
        title: {
            source: 'text',
            selector: '.slider__title'
        },
        body: {
            type: 'array',
            source: 'children',
            selector: '.slider__body'
        },
        imageAlt: {
            attribute: 'alt',
            selector: '.slider__image'
        },
        imageUrl: {
            attribute: 'src',
            selector: '.slider__image'
        }
    },

    edit({ attributes, className, setAttributes }) {
        const getImageButton = (openEvent) => {
            if (attributes.imageUrl) {
                return (
                    <img
                        src={attributes.imageUrl}
                        onClick={openEvent}
                        className="image"
                    />
                );
            }
            else {
                return (
                    <div className="button-container">
                        <Button
                            onClick={openEvent}
                            className="button button-large"
                        >
                            Pick an Image
                        </Button>
                    </div>
                );
            }
        };

        return(
            <div className="container">
                <MediaUpload
                    onSelect={media => { setAttributes({ imageAlt: media.alt, imageUrl: media.url }); }}
                    type="image"
                    value={attributes.imageID}
                    render={({ open }) => getImageButton(open)}
                />
                <PlainText
                    onChange={content => setAttributes({ title: content })}
                    value={attributes.title}
                    placeholder="Your card title"
                    className="heading"
                />
                <RichText
                    onChange={content => setAttributes({ body: content })}
                    value={attributes.body}
                    multiline="p"
                    placeholder="Your card text"
                />
            </div>
        );
    },

    save({ attributes }) {
        const cardImage = (src, alt) => {
            if (!src) return null;

            if (alt) {
                return (
                    <img
                        className="slider__image"
                        src={src}
                        alt={alt}
                    />
                );
            }

            return (
                <img
                    className="slider__image"
                    src={src}
                    alt=""
                    aria-hidden="true"
                />
            );
        };

        return (
            <div className="card">
                {cardImage(attributes.imageUrl, attributes.imageAlt)}
                <div className="slider__content">
                    <h3 className="slider__title">{attributes.title}</h3>
                    <div className="slider__body">
                        {attributes.body}
                    </div>
                </div>
            </div>
        );
    }
});

Any help would be greatly appreciated! I am picking up ES6 & JSX as well so this has been a pain!

I'm trying to learn how to create blocks for the block editor. I can't seem to find a list of form elements to add. For example, I need a toggle switch, dropdown, etc.

I found THIS toggle, but I'm not sure how to use it and can't find any examples.

Here is my code, so far I have been able to add PlainText, RichText and MediaUpload. Is there a list of these somewhere? More examples?

const { RichText, MediaUpload, PlainText } = wp.editor;
const { registerBlockType } = wp.blocks;
const { Button } = wpponents;

import './style.scss';
import './editor.scss';

registerBlockType('card-block/main', {
    title: 'Slider',
    icon: 'images-alt2',
    category: 'common',
    attributes: {
        title: {
            source: 'text',
            selector: '.slider__title'
        },
        body: {
            type: 'array',
            source: 'children',
            selector: '.slider__body'
        },
        imageAlt: {
            attribute: 'alt',
            selector: '.slider__image'
        },
        imageUrl: {
            attribute: 'src',
            selector: '.slider__image'
        }
    },

    edit({ attributes, className, setAttributes }) {
        const getImageButton = (openEvent) => {
            if (attributes.imageUrl) {
                return (
                    <img
                        src={attributes.imageUrl}
                        onClick={openEvent}
                        className="image"
                    />
                );
            }
            else {
                return (
                    <div className="button-container">
                        <Button
                            onClick={openEvent}
                            className="button button-large"
                        >
                            Pick an Image
                        </Button>
                    </div>
                );
            }
        };

        return(
            <div className="container">
                <MediaUpload
                    onSelect={media => { setAttributes({ imageAlt: media.alt, imageUrl: media.url }); }}
                    type="image"
                    value={attributes.imageID}
                    render={({ open }) => getImageButton(open)}
                />
                <PlainText
                    onChange={content => setAttributes({ title: content })}
                    value={attributes.title}
                    placeholder="Your card title"
                    className="heading"
                />
                <RichText
                    onChange={content => setAttributes({ body: content })}
                    value={attributes.body}
                    multiline="p"
                    placeholder="Your card text"
                />
            </div>
        );
    },

    save({ attributes }) {
        const cardImage = (src, alt) => {
            if (!src) return null;

            if (alt) {
                return (
                    <img
                        className="slider__image"
                        src={src}
                        alt={alt}
                    />
                );
            }

            return (
                <img
                    className="slider__image"
                    src={src}
                    alt=""
                    aria-hidden="true"
                />
            );
        };

        return (
            <div className="card">
                {cardImage(attributes.imageUrl, attributes.imageAlt)}
                <div className="slider__content">
                    <h3 className="slider__title">{attributes.title}</h3>
                    <div className="slider__body">
                        {attributes.body}
                    </div>
                </div>
            </div>
        );
    }
});

Any help would be greatly appreciated! I am picking up ES6 & JSX as well so this has been a pain!

Share Improve this question edited Mar 7, 2019 at 20:56 Jon asked Jan 29, 2019 at 1:34 JonJon 3275 silver badges17 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

There are two main packages that provide components which can be used inside the blocks API or the plugins API (to create your own blocks or plugins).

The components found in the wpponents package can be used to manipulate data. This data could be the attributes from a block or the data from your custom store or one of the default stores. They can actually be used outside of the block editor.

The components found in the wp.editor package are meant to be used inside the editor to manipulate it. They are used internally by the block editor itself or core blocks and can be used in your own blocks as well. These components make use of the block editor data stores so they have to be used inside it. Currently, the block editor can only be loaded inside the post editor but this is something that will probably change soon.

You can use this Gutenberg handbook for component list - Component List and if you don't want to keep track of what to import from where which component you can use this VSC Extension (I build it) It will import all the component so that you don't have to waste time on importing.

Here's dropdown code -

import { Button, Dropdown } from '@wordpress/components';

const MyDropdown = () => (
    <Dropdown
        className="my-container-class-name"
        contentClassName="my-popover-content-classname"
        position="bottom right"
        renderToggle={ ( { isOpen, onToggle } ) => (
            <Button isPrimary onClick={ onToggle } aria-expanded={ isOpen }>
                Toggle Popover!
            </Button>
        ) }
        renderContent={ () => (
            <div>
                This is the content of the popover.
            </div>
        ) }
    />
);

and ToggleControl code -

import { ToggleControl } from '@wordpress/components';
import { withState } from '@wordpress/compose';

const MyToggleControl = withState( {
    hasFixedBackground: false,
} )( ( { hasFixedBackground, setState } ) => ( 
    <ToggleControl
        label="Fixed Background"
        help={ hasFixedBackground ? 'Has fixed background.' : 'No fixed background.' } 
        checked={ hasFixedBackground }
        onChange={ () => setState( ( state ) => ( { hasFixedBackground: ! state.hasFixedBackground } ) ) }
    />
) );
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>