【问题标题】:Gutenberg RichText cursor focus automatically focus outGutenberg RichText 光标焦点自动聚焦
【发布时间】:2020-02-23 12:45:02
【问题描述】:

我正在尝试按功能通过富文本输入文本。但光标会自动移出 RichText。 我已经导入了需要的基本组件。我也通过 onKeyUp 而不是 onChange 尝试了它,但也没有工作。

当我在没有 ArticleContent 功能的情况下尝试时效果很好,但在尝试使用 ArticleContent 功能时效果不佳。

下面是我的代码(它不工作)

registerBlockType('vixmi-support/test', {
    title: __('Test Block'),
    icon : {
        src       : 'media-spreadsheet'
    },
    category   : 'vixmi',
    description: 'Sample desc',
    keywords   : [
        __( 'Single Article' ),
        __( 'Article' ),
    ],

    supports:{
        align : true,
        anchor: true
    },

    // custom attributes
    attributes:{
        title: {
            type    : 'string',
            source  : 'html',
            selector: 'h4',
        },
        articleLayout: {
            type   : 'string',
            default: 'left',
        }
    },


    edit: ( {attributes, setAttributes} ) => {
        const{
            title, content, buttonTitle, buttonLink, linkTarget, articleLayout
        } = attributes;

        function UpdateArticleTitle(newTitle){
            setAttributes( { title:newTitle } )
        }
        function UpdateActionLayout(event){
            setAttributes( { articleLayout:event.target.value } )
        }

        function ArticleContent(props){
            const{
                title
            } = props.attributes;
            return(
                <RichText
                    key         = "editablec"
                    tagName     = "h4"
                    placeholder = "Article title"
                    value       = { title }
                    onChange    = { UpdateArticleTitle } />
            )
        }

        return([
            <div className="sample">
                <ArticleContent attributes={ attributes }/>
            </div>
        ])
    },
    save: ( {attributes} ) => {
        const{
            title
        } = attributes;

        return(
            <div className="sample">
                <h4>{title}</h4>
            </div>
        )
    },

});

【问题讨论】:

    标签: wordpress-gutenberg gutenberg-blocks create-guten-block


    【解决方案1】:

    我完全不知道为什么会发生这种情况,但在我自己努力解决之后,我发现执行{ ArticleContent( { attributes: attributes } ) } 而不是&lt;ArticleContent attributes={ attribbutes }/&gt; 可以解决问题。

    实际的区别在于 RichText 实际上并没有被组件包裹,但是即使在浏览了大约一个小时的 Gutenberg 代码之后,为什么会有任何区别。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      相关资源
      最近更新 更多