Skip to content

Commit b19bf85

Browse files
committed
Adding search functionality
1 parent 65923fd commit b19bf85

File tree

11 files changed

+98
-11
lines changed

11 files changed

+98
-11
lines changed

color-palette/build/color-palette-frontend.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

color-palette/build/color-palette.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

color-palette/build/styles-editor.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

color-palette/build/styles-frontend.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

color-palette/inc/setup.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,18 @@ public function enqueue_editor_assets(): void {
5656
* @return void
5757
*/
5858
public function enqueue_frontend_assets(): void {
59+
$js = '../build/color-palette-frontend.min.js';
5960
$css= '../build/styles-frontend.min.css';
6061

6162
$handle = 'color-palette-frontend';
6263

64+
wp_enqueue_script(
65+
$handle,
66+
plugins_url( $js, __FILE__ ),
67+
[ 'jquery' ],
68+
filemtime( plugin_dir_path( __FILE__ ) . $js )
69+
);
70+
6371
wp_enqueue_style(
6472
$handle,
6573
plugin_dir_url( __FILE__ ) . $css,

color-palette/source/blocks/colors/_frontend.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ $lg: 1200px;
7070
.cp-palette-search {
7171
margin-bottom: 1rem;
7272

73+
fieldset {
74+
border: 0;
75+
}
76+
7377
&-label {
7478
display: block;
7579
margin-bottom: 0.25rem;

color-palette/source/blocks/colors/save.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ const ColorsSave = ( props ) => {
1919
if ( search ) {
2020
maybeSearch = (
2121
<form className='cp-palette-search'>
22-
<label for='cp-palette-search-input' className='cp-palette-search-label'>Search for a color</label>
23-
<input type='text' id='cp-palette-search-input' className='cp-palette-search-input' />
22+
<fieldset>
23+
<label className='cp-palette-search-label'>Search for a color</label>
24+
<input type='text' className='cp-palette-search-input' />
25+
</fieldset>
2426
<input type='submit' className='cp-palette-search-submit' value='Search' />
2527
</form>
2628
);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Master JS file for this plugin.
2+
* Master JS file for the editor scripts for this plugin.
33
*/
44

55
import Color from './blocks/color/index.js';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Master JS file for the front-end scripts for this plugin.
3+
*/
4+
5+
import ColorSearch from './scripts/search/index.js';
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Search functionality for the color palette block.
3+
*
4+
* This keyword search form will search by color name within a given
5+
* color palette block. It will display only those colors that match the
6+
* keyword and hide the rest from view.
7+
*/
8+
9+
const ColorSearch = ( ($) => {
10+
11+
$( document ).ready( () => {
12+
13+
$( '.cp-palette-search-submit' ).click( (e) => { search(e); } );
14+
15+
16+
const search = ( e ) => {
17+
e.preventDefault();
18+
19+
// Get the parent block, so that we know which color palette we are
20+
// working with (in case there are multiple on the page).
21+
const parent = $( e.target ).closest( '.cp-palette' );
22+
23+
// Get the search keyword from the form.
24+
const keyword = $( '.cp-palette-search-input', parent ).val();
25+
26+
// Get all of the child color blocks in this parent.
27+
const children = $( '.cp-color', parent );
28+
29+
// If there is no keyword, show everything!
30+
if ( !keyword ) {
31+
$( children ).show();
32+
return false;
33+
}
34+
35+
// If we are still there, there are child blocks to filter! Go through
36+
// each child block. If the keyword exists in the color name, show that
37+
// color; otherwise hide it.
38+
for ( let i=0; i<children.length; i++ ) {
39+
const colorName = $( '.cp-color-name', children[i] ).text();
40+
41+
// Check for the keyword, but convert everything to lower case to make
42+
// the search non-case-sensitive.
43+
if ( colorName.toLowerCase().indexOf( keyword.toLowerCase() ) >= 0 ) {
44+
$( children[i] ).show();
45+
} else {
46+
$( children[i] ).hide();
47+
}
48+
}
49+
}
50+
51+
} );
52+
53+
} )( jQuery );
54+
55+
export default ColorSearch;

0 commit comments

Comments
 (0)