From 39cd780e1807df71e3078a9b444c6f0e1119b2a3 Mon Sep 17 00:00:00 2001 From: RJ <27843274+rjchow@users.noreply.github.com> Date: Sat, 21 Jan 2023 11:05:45 +0800 Subject: [PATCH] add: slot for tasklist completion component slotfill (#36487) * add: slot for tasklist completion component slotfill * added fillprops and changed to experimental slotfill name --- .../task-list-completion-slot/index.ts | 2 + .../task-list-completion-slot.tsx | 38 ++++++++++ .../task-list-completion-slot/utils.tsx | 69 +++++++++++++++++++ .../client/two-column-tasks/task-list.tsx | 25 ++++++- .../add-tasklist-completion-slotfill | 4 ++ 5 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce-admin/client/two-column-tasks/task-list-completion-slot/index.ts create mode 100644 plugins/woocommerce-admin/client/two-column-tasks/task-list-completion-slot/task-list-completion-slot.tsx create mode 100644 plugins/woocommerce-admin/client/two-column-tasks/task-list-completion-slot/utils.tsx create mode 100644 plugins/woocommerce/changelog/add-tasklist-completion-slotfill diff --git a/plugins/woocommerce-admin/client/two-column-tasks/task-list-completion-slot/index.ts b/plugins/woocommerce-admin/client/two-column-tasks/task-list-completion-slot/index.ts new file mode 100644 index 00000000000..b2bd7c73a97 --- /dev/null +++ b/plugins/woocommerce-admin/client/two-column-tasks/task-list-completion-slot/index.ts @@ -0,0 +1,2 @@ +export * from './task-list-completion-slot'; +export * from './utils'; diff --git a/plugins/woocommerce-admin/client/two-column-tasks/task-list-completion-slot/task-list-completion-slot.tsx b/plugins/woocommerce-admin/client/two-column-tasks/task-list-completion-slot/task-list-completion-slot.tsx new file mode 100644 index 00000000000..d8036058224 --- /dev/null +++ b/plugins/woocommerce-admin/client/two-column-tasks/task-list-completion-slot/task-list-completion-slot.tsx @@ -0,0 +1,38 @@ +/** + * External dependencies + */ +import { useSlot } from '@woocommerce/experimental'; +import classNames from 'classnames'; + +/** + * Internal dependencies + */ +import { + EXPERIMENTAL_WC_TASK_LIST_COMPLETION_SLOT_NAME, + WooTaskListCompletion, + WooTaskListCompletionFillProps, +} from './utils'; + +export const TaskListCompletionSlot = ( { + className, + fillProps, +}: { + className?: string; + fillProps: WooTaskListCompletionFillProps; +} ) => { + const slot = useSlot( EXPERIMENTAL_WC_TASK_LIST_COMPLETION_SLOT_NAME ); + const hasFills = Boolean( slot?.fills?.length ); + if ( ! hasFills ) { + return null; + } + return ( +
+ +
+ ); +}; diff --git a/plugins/woocommerce-admin/client/two-column-tasks/task-list-completion-slot/utils.tsx b/plugins/woocommerce-admin/client/two-column-tasks/task-list-completion-slot/utils.tsx new file mode 100644 index 00000000000..b1e0c76c9c2 --- /dev/null +++ b/plugins/woocommerce-admin/client/two-column-tasks/task-list-completion-slot/utils.tsx @@ -0,0 +1,69 @@ +/** + * External dependencies + */ +import { Slot, Fill } from '@wordpress/components'; + +/** + * Internal dependencies + */ +import { createOrderedChildren, sortFillsByOrder } from '../../utils'; + +type WooTaskListCompletionProps = { + children: React.ReactNode; + order?: number; +}; + +export const EXPERIMENTAL_WC_TASK_LIST_COMPLETION_SLOT_NAME = + 'woocommerce_experimental_task_list_completion'; +/** + * Create a Fill for extensions to add items to the WooCommerce Admin Task List completion component slot. + * + * @slotFill WooTaskListCompletion + * @scope woocommerce-admin + * @example + * const MyTasklistCompletionItem = () => ( + * My Task List completion item + * ); + * + * registerPlugin( 'my-extension', { + * render: MyTasklistCompletionItem, + * scope: 'woocommerce-admin', + * } ); + * @param {Object} param0 + * @param {Array} param0.children - Node children. + * @param {Array} param0.order - Node order. + */ +export const WooTaskListCompletion = ( { + children, + order = 1, +}: WooTaskListCompletionProps ) => { + return ( + + { ( fillProps: Fill.Props ) => { + return createOrderedChildren( children, order, fillProps ); + } } + + ); +}; + +export type WooTaskListCompletionFillProps = { + /** Call this function to hide this Task List completion component completely, without any replacement component */ + hideTasks: () => void; + /** Call this function to show the completed Task List items instead of this TaskList completion component */ + keepTasks: () => void; + /** To show the CES component or not */ + customerEffortScore: boolean; +}; + +WooTaskListCompletion.Slot = ( { + fillProps, +}: { + fillProps: Slot.Props & WooTaskListCompletionFillProps; +} ) => ( + + { sortFillsByOrder } + +); diff --git a/plugins/woocommerce-admin/client/two-column-tasks/task-list.tsx b/plugins/woocommerce-admin/client/two-column-tasks/task-list.tsx index 0745563cac7..32ca6e47bf7 100644 --- a/plugins/woocommerce-admin/client/two-column-tasks/task-list.tsx +++ b/plugins/woocommerce-admin/client/two-column-tasks/task-list.tsx @@ -22,7 +22,7 @@ import { WCDataSelector, } from '@woocommerce/data'; import { recordEvent } from '@woocommerce/tracks'; -import { List } from '@woocommerce/experimental'; +import { List, useSlot } from '@woocommerce/experimental'; import classnames from 'classnames'; /** @@ -36,6 +36,10 @@ import { ProgressHeader } from '~/task-lists/progress-header'; import { TaskListItemTwoColumn } from './task-list-item-two-column'; import { TaskListCompletedHeader } from './completed-header'; import { LayoutContext } from '~/layout'; +import { + TaskListCompletionSlot, + EXPERIMENTAL_WC_TASK_LIST_COMPLETION_SLOT_NAME, +} from './task-list-completion-slot'; export type TaskListProps = TaskListType & { eventName?: string; @@ -99,6 +103,10 @@ export const TaskList: React.FC< TaskListProps > = ( { recordTaskListView(); }, [] ); + const taskListCompletionSlot = useSlot( + EXPERIMENTAL_WC_TASK_LIST_COMPLETION_SLOT_NAME + ); + useEffect( () => { const { task: prevTask } = prevQueryRef.current; const { task } = query; @@ -229,7 +237,22 @@ export const TaskList: React.FC< TaskListProps > = ( { return
; } + const hasTaskListCompletionSlotFills = Boolean( + taskListCompletionSlot?.fills?.length + ); + if ( isComplete && keepCompletedTaskList !== 'yes' ) { + if ( hasTaskListCompletionSlotFills ) { + return ( + + ); + } return ( <> { cesHeader ? ( diff --git a/plugins/woocommerce/changelog/add-tasklist-completion-slotfill b/plugins/woocommerce/changelog/add-tasklist-completion-slotfill new file mode 100644 index 00000000000..5e4a1788aba --- /dev/null +++ b/plugins/woocommerce/changelog/add-tasklist-completion-slotfill @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Added slot for tasklist completion slotfill