Skip to content

Commit

Permalink
Merge branch 'trunk' into add/make-sure-focused-elements-are-visible
Browse files Browse the repository at this point in the history
  • Loading branch information
Imran92 committed Apr 2, 2024
2 parents 1687172 + b59c56b commit ad1632c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
42 changes: 31 additions & 11 deletions assets/home/tasks-section/task-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import {
Icon,
external as externalIcon,
info as infoIcon,
} from '@wordpress/icons';
import { Tooltip } from '@wordpress/components';

/**
* Internal dependencies
*/
Expand All @@ -13,18 +23,20 @@ import { isUrlExternal } from '../utils';
/**
* WordPress dependencies
*/
import { Icon, external } from '@wordpress/icons';

/**
* Tasks item component.
*
* @param {Object} props Component props.
* @param {string} props.title Item title.
* @param {string} props.url Item URL.
* @param {boolean} props.done Whether item is completed.
* @param {Object} props Component props.
* @param {string} props.title Item title.
* @param {string} props.url Item URL.
* @param {boolean} props.done Whether item is completed.
* @param {boolean} props.disabled Whether item is disabled.
* @param {boolean} props.info Info text.
*/
const TaskItem = ( { title, url, done } ) => {
const Tag = done ? 'span' : 'a';
const TaskItem = ( { title, url, done, info, disabled } ) => {
const isActive = ! done && ! disabled;
const Tag = isActive ? 'a' : 'span';
const isExternal =
isUrlExternal( url ) || url?.indexOf( 'external=true' ) >= 0; // If the URL contains 'external=true', we show the external icon,
// It's helpful when it's an internal URL (maybe for tracking purpose) but redirecting to an external one.
Expand All @@ -38,7 +50,7 @@ const TaskItem = ( { title, url, done } ) => {
return (
<li
className={ classnames( 'sensei-home-tasks__item', {
'sensei-home-tasks__item--done': done,
'sensei-home-tasks__item--disabled': ! isActive,
} ) }
>
<Tag className="sensei-home-tasks__link" { ...linkProps }>
Expand All @@ -47,14 +59,22 @@ const TaskItem = ( { title, url, done } ) => {
) }
<span className="sensei-home-tasks__item-title">
{ title }
{ isExternal && (
{ isExternal && isActive && (
<Icon
icon={ external }
icon={ externalIcon }
className="sensei-home-tasks__external-icon sensei-home-tasks__icon-with-current-color"
/>
) }
</span>
{ ! done && (
{ info && (
<Tooltip text={ info }>
<Icon
className="sensei-home-tasks__icon-with-current-color"
icon={ infoIcon }
/>
</Tooltip>
) }
{ isActive && (
<ChevronRightIcon className="sensei-home-tasks__link-chevron" />
) }
</Tag>
Expand Down
41 changes: 41 additions & 0 deletions assets/home/tasks-section/task-item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
*/
import { render } from '@testing-library/react';

/**
* WordPress dependencies
*/
import { Tooltip } from '@wordpress/components';

/**
* Internal dependencies
*/
import TaskItem from './task-item';

jest.mock( '@wordpress/components' );

describe( '<TaskItem />', () => {
it( 'Should render an anchor when item is not completed', () => {
const { container } = render( <TaskItem url="#" /> );
Expand Down Expand Up @@ -55,4 +62,38 @@ describe( '<TaskItem />', () => {

expect( externalIcon ).toBeNull();
} );

it( 'Should not render external icon when task is not active', () => {
const { container } = render(
<TaskItem
url="www.example.com/something?this=false&external=true"
showExternalIcon
disabled
/>
);

const externalIcon = container.querySelector(
'.sensei-home-tasks__external-icon'
);

expect( externalIcon ).toBeNull();
} );

it( 'Should render a span when item is disabled', () => {
const { container } = render( <TaskItem url="#" disabled /> );

const renderedTag = container.querySelector(
'.sensei-home-tasks__link'
).tagName;

expect( renderedTag ).toEqual( 'SPAN' );
} );

it( 'Should render a task with the info', () => {
Tooltip.mockImplementation( ( { text } ) => text );

const { queryByText } = render( <TaskItem url="#" info="Info text" /> );

expect( queryByText( 'Info text' ) ).toBeTruthy();
} );
} );
2 changes: 2 additions & 0 deletions assets/home/tasks-section/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const Tasks = ( { items } ) => (
title={ task.title }
url={ task.url }
done={ task.done }
disabled={ task.disabled }
info={ task.info }
/>
) ) }
</ul>
Expand Down
2 changes: 1 addition & 1 deletion assets/home/tasks-section/tasks.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
border-top: none;
}

&--done {
&--disabled {
color: $gray-50;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ private function get_tasks(): array {
* @property {string} `$tasks[]['url']` Optional. Destination URL for users when clicking on the task.
* @property {string} `$tasks[]['image']` Optional. Source url or path for the featured image when this task is the first pending one.
* @property {bool} `$tasks[]['done']` Whether the task is considered done or not.
* @property {bool} `$tasks[]['disabled']` Whether the task is considered disabled or not.
* @property {bool} `$tasks[]['info']` A text to describe something about the task in an `info` icon.
* @return {array} Filtered tasks.
*/
return apply_filters( 'sensei_home_tasks', $tasks );
Expand Down

0 comments on commit ad1632c

Please sign in to comment.