Skip to content

Commit

Permalink
Merge pull request #31 from material-components/feature/11-curated-po…
Browse files Browse the repository at this point in the history
…st-title

Add searchlist item with preview link
  • Loading branch information
ravichdev committed Apr 8, 2021
2 parents 3168e21 + fe92a90 commit 8ed570e
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { __, _n, sprintf } from '@wordpress/i18n';
*/
import withSearchedPosts from '../../hocs/with-searched-posts';
import ErrorMessage from '../error-message';
import SearchListItem from './item';

/**
* The posts control exposes a custom selector for searching and selecting posts.
Expand Down Expand Up @@ -80,6 +81,7 @@ const PostsControl = ( {
onSearch={ onSearch }
onChange={ onChange }
messages={ messages }
renderItem={ args => <SearchListItem { ...args } /> }
/>
);
};
Expand Down
112 changes: 112 additions & 0 deletions plugin/assets/src/block-editor/components/posts-control/item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { escapeRegExp, first, last, isNil } from 'lodash';

/**
* Internal dependencies
*/
import './style.css';

function getHighlightedName( name, search ) {
if ( ! search ) {
return name;
}
const re = new RegExp( escapeRegExp( search ), 'ig' );
return name.replace( re, '<strong>$&</strong>' );
}

function getBreadcrumbsForDisplay( breadcrumbs ) {
if ( breadcrumbs.length === 1 ) {
return first( breadcrumbs );
}
if ( breadcrumbs.length === 2 ) {
return first( breadcrumbs ) + ' › ' + last( breadcrumbs );
}

return first( breadcrumbs ) + ' … ' + last( breadcrumbs );
}

const SearchListItem = ( {
countLabel,
className,
depth = 0,
item,
isSelected,
isSingle,
onSelect,
search = '',
...props
} ) => {
const showCount = ! isNil( countLabel ) || ! isNil( item.count );
const classes = [ className, 'woocommerce-search-list__item' ];
classes.push( `depth-${ depth }` );
if ( isSingle ) {
classes.push( 'is-radio-button' );
}
if ( showCount ) {
classes.push( 'has-count' );
}
const hasBreadcrumbs = item.breadcrumbs && item.breadcrumbs.length;

return (
<label htmlFor={ item.id } className={ classes.join( ' ' ) }>
{ isSingle ? (
<input
type="radio"
id={ item.id }
name={ item.name }
value={ item.value }
onChange={ onSelect( item ) }
checked={ isSelected }
className="woocommerce-search-list__item-input"
{ ...props }
></input>
) : (
<input
type="checkbox"
id={ item.id }
name={ item.name }
value={ item.value }
onChange={ onSelect( item ) }
checked={ isSelected }
className="woocommerce-search-list__item-input"
{ ...props }
></input>
) }

<span className="woocommerce-search-list__item-label">
{ hasBreadcrumbs ? (
<span className="woocommerce-search-list__item-prefix">
{ getBreadcrumbsForDisplay( item.breadcrumbs ) }
</span>
) : null }
<span
className="woocommerce-search-list__item-name"
dangerouslySetInnerHTML={ {
__html: getHighlightedName( item.name, search ),
} }
/>
{ item.link ? (
<a
className="woocommerce-search-list__item-preview"
href={ item.link }
target="_blank"
rel="noopener noreferrer"
>
{ __( 'Preview', 'material-design' ) }
</a>
) : null }
</span>

{ !! showCount && (
<span className="woocommerce-search-list__item-count">
{ countLabel || item.count }
</span>
) }
</label>
);
};

export default SearchListItem;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.woocommerce-search-list__item-preview {
border: 1px solid #e2e4e7;
border-radius: 16px;
color: #555d66;
font-size: 0.8em;
margin-left: auto;
padding: 2px 12px;
text-decoration: none;
}
4 changes: 2 additions & 2 deletions plugin/assets/src/block-editor/hocs/with-searched-posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ const withSearchedPosts = createHigherOrderComponent( OriginalComponent => {

const debouncedOnSearch = debounce( onSearch, 400 );

const transformedList = list.map( ( { id, title } ) => {
return { id, name: title.rendered };
const transformedList = list.map( ( { id, title, link } ) => {
return { id, name: title.rendered, link };
} );

useEffect(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SearchListControl matches snapshot 1`] = `
Object {
"asFragment": [Function],
"baseElement": <body>
<div>
<label
class=" woocommerce-search-list__item depth-0"
for="2"
>
<input
class="woocommerce-search-list__item-input"
id="2"
name="Example Post"
type="checkbox"
value=""
/>
<span
class="woocommerce-search-list__item-label"
>
<span
class="woocommerce-search-list__item-name"
>
Example Post
</span>
<a
class="woocommerce-search-list__item-preview"
href="http://example.com"
rel="noopener noreferrer"
target="_blank"
>
Preview
</a>
</span>
</label>
</div>
</body>,
"container": <div>
<label
class=" woocommerce-search-list__item depth-0"
for="2"
>
<input
class="woocommerce-search-list__item-input"
id="2"
name="Example Post"
type="checkbox"
value=""
/>
<span
class="woocommerce-search-list__item-label"
>
<span
class="woocommerce-search-list__item-name"
>
Example Post
</span>
<a
class="woocommerce-search-list__item-preview"
href="http://example.com"
rel="noopener noreferrer"
target="_blank"
>
Preview
</a>
</span>
</label>
</div>,
"debug": [Function],
"findAllByAltText": [Function],
"findAllByDisplayValue": [Function],
"findAllByLabelText": [Function],
"findAllByPlaceholderText": [Function],
"findAllByRole": [Function],
"findAllByTestId": [Function],
"findAllByText": [Function],
"findAllByTitle": [Function],
"findByAltText": [Function],
"findByDisplayValue": [Function],
"findByLabelText": [Function],
"findByPlaceholderText": [Function],
"findByRole": [Function],
"findByTestId": [Function],
"findByText": [Function],
"findByTitle": [Function],
"getAllByAltText": [Function],
"getAllByDisplayValue": [Function],
"getAllByLabelText": [Function],
"getAllByPlaceholderText": [Function],
"getAllByRole": [Function],
"getAllByTestId": [Function],
"getAllByText": [Function],
"getAllByTitle": [Function],
"getByAltText": [Function],
"getByDisplayValue": [Function],
"getByLabelText": [Function],
"getByPlaceholderText": [Function],
"getByRole": [Function],
"getByTestId": [Function],
"getByText": [Function],
"getByTitle": [Function],
"queryAllByAltText": [Function],
"queryAllByDisplayValue": [Function],
"queryAllByLabelText": [Function],
"queryAllByPlaceholderText": [Function],
"queryAllByRole": [Function],
"queryAllByTestId": [Function],
"queryAllByText": [Function],
"queryAllByTitle": [Function],
"queryByAltText": [Function],
"queryByDisplayValue": [Function],
"queryByLabelText": [Function],
"queryByPlaceholderText": [Function],
"queryByRole": [Function],
"queryByTestId": [Function],
"queryByText": [Function],
"queryByTitle": [Function],
"rerender": [Function],
"unmount": [Function],
}
`;

exports[`SearchListControl matches snapshot when no link exists 1`] = `
Object {
"asFragment": [Function],
"baseElement": <body>
<div>
<label
class=" woocommerce-search-list__item depth-0"
for="2"
>
<input
class="woocommerce-search-list__item-input"
id="2"
name="Example Post"
type="checkbox"
value=""
/>
<span
class="woocommerce-search-list__item-label"
>
<span
class="woocommerce-search-list__item-name"
>
Example Post
</span>
</span>
</label>
</div>
</body>,
"container": <div>
<label
class=" woocommerce-search-list__item depth-0"
for="2"
>
<input
class="woocommerce-search-list__item-input"
id="2"
name="Example Post"
type="checkbox"
value=""
/>
<span
class="woocommerce-search-list__item-label"
>
<span
class="woocommerce-search-list__item-name"
>
Example Post
</span>
</span>
</label>
</div>,
"debug": [Function],
"findAllByAltText": [Function],
"findAllByDisplayValue": [Function],
"findAllByLabelText": [Function],
"findAllByPlaceholderText": [Function],
"findAllByRole": [Function],
"findAllByTestId": [Function],
"findAllByText": [Function],
"findAllByTitle": [Function],
"findByAltText": [Function],
"findByDisplayValue": [Function],
"findByLabelText": [Function],
"findByPlaceholderText": [Function],
"findByRole": [Function],
"findByTestId": [Function],
"findByText": [Function],
"findByTitle": [Function],
"getAllByAltText": [Function],
"getAllByDisplayValue": [Function],
"getAllByLabelText": [Function],
"getAllByPlaceholderText": [Function],
"getAllByRole": [Function],
"getAllByTestId": [Function],
"getAllByText": [Function],
"getAllByTitle": [Function],
"getByAltText": [Function],
"getByDisplayValue": [Function],
"getByLabelText": [Function],
"getByPlaceholderText": [Function],
"getByRole": [Function],
"getByTestId": [Function],
"getByText": [Function],
"getByTitle": [Function],
"queryAllByAltText": [Function],
"queryAllByDisplayValue": [Function],
"queryAllByLabelText": [Function],
"queryAllByPlaceholderText": [Function],
"queryAllByRole": [Function],
"queryAllByTestId": [Function],
"queryAllByText": [Function],
"queryAllByTitle": [Function],
"queryByAltText": [Function],
"queryByDisplayValue": [Function],
"queryByLabelText": [Function],
"queryByPlaceholderText": [Function],
"queryByRole": [Function],
"queryByTestId": [Function],
"queryByText": [Function],
"queryByTitle": [Function],
"rerender": [Function],
"unmount": [Function],
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { render } from '@testing-library/react';
/**
* Internal dependencies
*/
import PostsControl from '../../../../assets/src/block-editor/components/posts-control';
import PostsControl from '../../../../../assets/src/block-editor/components/posts-control';

jest.mock( '@woocommerce/components', () => {
return {
Expand All @@ -34,7 +34,7 @@ jest.mock( '@woocommerce/components', () => {
} );

jest.mock(
'../../../../assets/src/block-editor/hocs/with-searched-posts.js',
'../../../../../assets/src/block-editor/hocs/with-searched-posts.js',
() => {
return {
__esModule: true,
Expand Down
Loading

0 comments on commit 8ed570e

Please sign in to comment.