kirki icon indicating copy to clipboard operation
kirki copied to clipboard

Select Control doesn't work with ZERO values

Open Oxibug opened this issue 2 years ago • 1 comments

Issue description:

All Select controls outside repeaters which use the Searchable [Select] doesn't work when arrays has ['0', 0] value.

But it works properly with Select controls inside repeaters

For example:

    function ddl_categories() {

        $categories = get_categories( array('orderby' => 'id') );

        $cats = array(
            '0' => __('Recent Posts', 'DOMAIN') 
        );    
        foreach( $categories as $category ) {   
            $cats[ $category->term_id ] = $category->name;
        }
        return $cats;
    }

This will generate the following error in Console window react-dom.min.js?ver=17.0.1:9 Uncaught TypeError: Cannot use 'in' operator to search for 'options' in Recent Posts

Version used:

4.0.21

Using theme_mods or options?

theme_mods

JS error messages that might be related

react-dom.min.js?ver=17.0.1:9 Uncaught TypeError: Cannot use 'in' operator to search for 'options' in MY_DROPDOWNLIST_TEXT
    at Select-126cf1dd.esm.js:1033:9
    at Array.map (<anonymous>)
    at En (Select-126cf1dd.esm.js:1032:24)
    at t.buildCategorizedOptions (Select-126cf1dd.esm.js:1371:14)
    at t.buildFocusableOptions (Select-126cf1dd.esm.js:1379:64)
    at o.value (Select-126cf1dd.esm.js:1918:35)
    at t.onControlMouseDown (Select-126cf1dd.esm.js:1420:17)
    at Object.ha (react-dom.min.js?ver=17.0.1:9:95935)
    at B (react-dom.min.js?ver=17.0.1:9:9748)
    at react-dom.min.js?ver=17.0.1:9:20547

Oxibug avatar Jun 12 '22 15:06 Oxibug

I'm fairly new to Kirki myself so maybe I'm wrong, but this doesn't seem like a Kirki issue. It seems like a WP core issue with react-dom.min.js

You could submit a ticket to WP Core tracker requesting a check for empty array values being passed. If/when a patch is made the issue will go away. In the meantime, your best bet is to revise your own code to only append values to the array if there's actually a value to append:

 function ddl_categories() {

        $cats = array(
            '0' => __('Recent Posts', 'DOMAIN') 
        );
  
        $categories = get_categories( array('orderby' => 'id') );
        if ($categories && !is_wp_error($categories)) {
                 foreach( $categories as $category ) {   
                         $cats[ strval($category->term_id) ] = $category->name;
                 }
        }

       
        return $cats;
    }

NOTE: the addition of strval() for the array index. I suspect part of your issue is mixing the string value '0' with the integer values for each $category->term_id. Pick one value type (either strings, or integers) as your array index, but don't mix both together. Integer values makes more sense to me for array indexes, but your example code shows you using a string as the index for "Recent Posts" so that's what I went with.

From what I can tell, your issue has nothing to do with Kirki.

codejp3 avatar Oct 03 '22 05:10 codejp3