-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from material-components/feature/11-curated-po…
…st-title Add searchlist item with preview link
- Loading branch information
Showing
8 changed files
with
418 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
plugin/assets/src/block-editor/components/posts-control/item.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
9 changes: 9 additions & 0 deletions
9
plugin/assets/src/block-editor/components/posts-control/style.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
227 changes: 227 additions & 0 deletions
227
plugin/tests/js/block-editor/components/posts-control/__snapshots__/item.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.