Skip to content

Commit

Permalink
Render date as button that opens form
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Aug 5, 2024
1 parent bf65a6f commit e40a0ea
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 21 deletions.
74 changes: 68 additions & 6 deletions packages/dataviews/src/field-types/datetime.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
/**
* External dependencies
*/
import type { ComponentType } from 'react';

/**
* WordPress dependencies
*/
import { DateTimePicker } from '@wordpress/components';
import {
Button,
DateTimePicker,
Dropdown,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { useCallback } from '@wordpress/element';
import { closeSmall } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
Expand All @@ -28,12 +41,44 @@ function isValid( value: any, context?: ValidationContext ) {
return true;
}

interface DateTimePickerControlProps {
title: string;
value: string;
onChange: ( newValue: string | null ) => void;
onClose: () => void;
}

const DateTimePickerForm: ComponentType< DateTimePickerControlProps > = ( {
title,
value,
onChange,
onClose,
} ) => {
return (
<VStack>
<HStack>
<span>{ title }</span>
<Button
label={ __( 'Close' ) }
icon={ closeSmall }
onClick={ onClose }
/>
</HStack>
<DateTimePicker
currentDate={ value }
onChange={ onChange }
is12Hour
/>
</VStack>
);
};

function Edit< Item >( {
data,
field,
onChange,
}: DataFormControlProps< Item > ) {
const { id } = field;
const { id, label, description } = field;
const value = field.getValue( { item: data } ) ?? '';
const onChangeControl = useCallback(
( newValue: string | null ) =>
Expand All @@ -45,10 +90,27 @@ function Edit< Item >( {
);

return (
<DateTimePicker
currentDate={ value }
onChange={ onChangeControl }
is12Hour
<Dropdown
renderToggle={ ( { onToggle, isOpen } ) => (
<Button
size="compact"
variant="tertiary"
onClick={ onToggle }
aria-expanded={ isOpen }
aria-label={ label }
label={ description }
>
{ value }
</Button>
) }
renderContent={ ( { onClose } ) => (
<DateTimePickerForm
onClose={ onClose }
title={ label }
value={ value }
onChange={ onChangeControl }
/>
) }
/>
);
}
Expand Down
38 changes: 23 additions & 15 deletions packages/edit-site/src/components/post-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { __ } from '@wordpress/i18n';
import { DataForm, isItemValid } from '@wordpress/dataviews';
import { useDispatch, useSelect, useRegistry } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';
import { Button } from '@wordpress/components';
import {
Button,
__experimentalVStack as VStack,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { useState, useMemo } from '@wordpress/element';

/**
Expand Down Expand Up @@ -72,20 +76,24 @@ function PostEditForm( { postType, postId } ) {
const isUpdateDisabled = ! isItemValid( itemWithEdits, fields, form );
return (
<form onSubmit={ onSubmit }>
<DataForm
data={ itemWithEdits }
fields={ fields }
form={ form }
onChange={ setEdits }
/>
<Button
variant="primary"
type="submit"
accessibleWhenDisabled
disabled={ isUpdateDisabled }
>
{ __( 'Update' ) }
</Button>
<VStack>
<DataForm
data={ itemWithEdits }
fields={ fields }
form={ form }
onChange={ setEdits }
/>
<HStack>
<Button
variant="primary"
type="submit"
accessibleWhenDisabled
disabled={ isUpdateDisabled }
>
{ __( 'Update' ) }
</Button>
</HStack>
</VStack>
</form>
);
}
Expand Down

0 comments on commit e40a0ea

Please sign in to comment.