Skip to content

Commit

Permalink
Substitute NumberControl by DataForm config
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Jul 24, 2024
1 parent 368d0ac commit 5dd8e8e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
34 changes: 33 additions & 1 deletion packages/dataviews/src/components/dataform/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import type { Dispatch, SetStateAction } from 'react';
/**
* WordPress dependencies
*/
import { TextControl } from '@wordpress/components';
import {
TextControl,
__experimentalNumberControl as NumberControl,
} from '@wordpress/components';
import { useCallback, useMemo } from '@wordpress/element';

/**
Expand Down Expand Up @@ -56,12 +59,41 @@ function DataFormTextControl< Item >( {
);
}

function DataFormNumberControl< Item >( {
data,
field,
onChange,
}: DataFormControlProps< Item > ) {
const { id, label, description } = field;
const value = field.getValue( { item: data } );

const onChangeControl = useCallback(
( newValue: string | undefined ) =>
onChange( ( prevItem: Item ) => ( {
...prevItem,
[ id ]: newValue,
} ) ),
[ id, onChange ]
);

return (
<NumberControl
label={ label }
help={ description }
value={ value }
onChange={ onChangeControl }
__next40pxDefaultSize
/>
);
}

const controls: {
[ key: string ]: < Item >(
props: DataFormControlProps< Item >
) => JSX.Element;
} = {
text: DataFormTextControl,
number: DataFormNumberControl,
};

function getControlForField< Item >( field: NormalizedField< Item > ) {
Expand Down
5 changes: 5 additions & 0 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export type Field< Item > = {
*/
label?: string;

/**
* A description of the field.
*/
description?: string;

/**
* Placeholder for the field.
*/
Expand Down
30 changes: 19 additions & 11 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
__experimentalText as Text,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
__experimentalNumberControl as NumberControl,
} from '@wordpress/components';

/**
Expand All @@ -39,21 +38,31 @@ import { getItemTitle } from '../../dataviews/actions/utils';
const { PATTERN_TYPES, CreatePatternModalContents, useDuplicatePatternProps } =
unlock( patternsPrivateApis );

// TODO: this should be shared with other components (page-pages).
// TODO: this should be shared with other components (see post-fields in edit-site).
const fields = [
{
type: 'text',
header: __( 'Title' ),
id: 'title',
label: __( 'Title' ),
placeholder: __( 'No title' ),
getValue: ( { item } ) => item.title,
},
{
type: 'number',
id: 'menu_order',
label: __( 'Order' ),
description: __( 'Determines the order of pages.' ),
},
];

const form = {
visibleFields: [ 'title' ],
};

const formOrderAction = {
visibleFields: [ 'menu_order' ],
};

/**
* Check if a template is removable.
*
Expand Down Expand Up @@ -635,12 +644,12 @@ function useRenamePostAction( postType ) {
}

function ReorderModal( { items, closeModal, onActionPerformed } ) {
const [ item ] = items;
const [ item, setItem ] = useState( items[ 0 ] );
const orderInput = item.menu_order;
const { editEntityRecord, saveEditedEntityRecord } =
useDispatch( coreStore );
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
const [ orderInput, setOrderInput ] = useState( item.menu_order );

async function onOrder( event ) {
event.preventDefault();
Expand Down Expand Up @@ -684,12 +693,11 @@ function ReorderModal( { items, closeModal, onActionPerformed } ) {
'Determines the order of pages. Pages with the same order value are sorted alphabetically. Negative order values are supported.'
) }
</div>
<NumberControl
__next40pxDefaultSize
label={ __( 'Order' ) }
help={ __( 'Set the page order.' ) }
value={ orderInput }
onChange={ setOrderInput }
<DataForm
data={ item }
fields={ fields }
form={ formOrderAction }
onChange={ setItem }
/>
<HStack justify="right">
<Button
Expand Down

0 comments on commit 5dd8e8e

Please sign in to comment.