首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Gutenberg编辑器中使用@wordpress/date块?

如何在Gutenberg编辑器中使用@wordpress/date块?
EN

WordPress Development用户
提问于 2018-09-25 22:19:31
回答 1查看 2.3K关注 0票数 1

似乎有一个开箱即用的日期块由Wordpress:https://wordpress.org/gutenberg/handbook/packages/packages-date/提供

我们如何在Gutenberg编辑器中启用/使用这个块或另一个datepicker块?

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2018-09-30 07:10:26

默认情况下,WordPress不提供日期选择器块,但是在核心中有日期组件。您需要调用相关组件才能让日期选择器工作。下面是来自核心日期组件的代码

代码语言:javascript
复制
import { DateTimePicker } from '@wordpress/components';
import { getSettings } from '@wordpress/date';
import { withState } from '@wordpress/compose';


const MyDateTimePicker = withState( {
    date: new Date(),
} )( ( { date, setState } ) => {
    const settings = getSettings();

    // To know if the current timezone is a 12 hour time with look for "a" in the time format.
    // We also make sure this a is not escaped by a "/".
    const is12HourTime = /a(?!\\)/i.test(
        settings.formats.time
            .toLowerCase() // Test only the lower case a
            .replace( /\\\\/g, '' ) // Replace "//" with empty strings
            .split( '' ).reverse().join( '' ) // Reverse the string and test for "a" not followed by a slash
    );

    return (
        <DateTimePicker
            currentDate={ date }
            onChange={ ( date ) => setState( { date } ) }
            locale={ settings.l10n.locale }
            is12Hour={ is12HourTime }
            />
    );

} );
票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/315144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档