• I need to implement the DateTimePicker component (https://github.com/WordPress/gutenberg/tree/master/packages/components/src/date-time) in a custom Gutenberg block and then save the selected date as a string attribute with setAttributes(). (the date should be associated with the block and used in the front-end)

    I don’t understand how use setAttributes() inside withState(), which is used by the DateTimePicker component to store the local states of the component.

    This is an example implementation of DateTimePicker:

    import { DateTimePicker } from '@wordpress/components';
    import { __experimentalGetSettings } from '@wordpress/date';
    import { withState } from '@wordpress/compose';
    
    const MyDateTimePicker = withState( {
    	date: new Date(),
    } )( ( { date, setState } ) => {
    	const settings = __experimentalGetSettings();
    
    	// To know if the current timezone is a 12 hour time with look for an "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 } ) }
    			is12Hour={ is12HourTime }
    		/>
    	);
    } );

    I’ve already asked two similar questions in the past but I’m getting no aswers:

    https://github.com/WordPress/gutenberg/issues/13813

  • The topic ‘Use getAttributes() in withState with DateTimePicker’ is closed to new replies.