-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dynamic select control... #34
Comments
@SatishKumarPrajapati you mean adding dynamic items to select control ? |
@ajitbohra yes i want to add dynamic items to select control... |
You can add dynamically generated array of objects containing label, value property to select control. <SelectControl
multiple
label={ __( 'Select some users:' ) }
value={ this.state.users } // e.g: value = [ 'a', 'c' ]
onChange={ ( users ) => { this.setState( { users } ) } }
options={ [
{ value: 'a', label: 'User A' },
{ value: 'b', label: 'User B' },
{ value: 'c', label: 'User c' },
] }
/> For more info |
I want to get data from PHP function.... and I tried to that but not able to get succeed... |
@ajitbohra is there any example for dynamic select control ? |
check |
I'm not able to create a dynamic option also, any help on this, I tired to add the options as an array but no luck . |
I came up with a workaround: https://gist.github.com/rogerlos/7502541070942e16a1188dd0bb9ac2b9 |
I've been struggling with this for a long time as well. My use case is to mimic the various
The only problem with it (for my immediate need) is that:
Any help in solving that problem would be greatly appreciated! |
here's how I'm using it in my block:
|
(Doesn't it seem kind of nuts these oft-used core mimicking functions aren't part of Gutenberg? It does to me.) I ended up cheating. I have a regular old JS file, which doesn't use JSX or react, which is a dependency of my block. Within it I stashed a function which is the REST caller, and what amounts to a "data store" for the returned post list, since my page typically has tens of blocks which use the exact same REST posts list in the SelectControl and it seems stupid to repeatedly ask for the same thing, but I'm not hip on the latest techniques, so... function cheat() {
this.cheat = this;
this.types = [];
this.get_posts = function () {
return new Promise(
function ( resolve ) {
wp.apiFetch( { path : '/wp/v2/types' } ).then(
function ( types ) {
this.types = types;
}
wp.apiFetch( { path : '/wp/v1/custom/getposts' } ).then(
function ( res ) {
resolve( res )
}
)
)
}
)
};
this.post_selector_opts = function () {
let cheat = this.cheat;
return new Promise(
function ( resolve ) {
cheat.get_posts()
.then(
function ( posts ) {
let opts = [{ key: 'none', label: 'Select Post', value: 0 }];
for ( const P of posts ) {
opts.push( { key: P.slug, label: P.title, value: P.id } );
}
resolve( opts );
}
)
}
)
};
this.init = function () {
this.post_selector_opts().then( opts => {
cheater.posts = opts;
} )
}
}
const cheater = { posts: [] };
let shared = new cheat();
shared.init(); and then within my block: el(
SelectControl,
{
options: cheater.posts,
}
) Kind of sleazy, sure, and definitely would make the facebook folks cry. Because the REST call is made when the editor loads, the select control always has the content available. This makes sense to me; if you know you're going to be making the exact same call potentially dozens of times, why would you not stash the results somewhere? (FWIW, I'm going to ditch the select control since it's way too basic; without the ability to use headers within it to organize the list (as allowed by the HTML spec) it's incomprehensible to use for a post selector on a site with more than a dozen or so posts.) |
I completely agree that GB should include controls that mimic all of the |
From what I can tell (by watching REST API hits against the server) with the way I wrote |
I suspect I can solve that problem by using a Promise, but since I don't really understand what a Promise is I'm not sure :-( @rogerlos, I'm gonna look at how you are using Promises and see if I can crib something from that. Thanx. |
I clearly don't totally grok promises, either, as anyone who is an expert at them probably would not be thrilled with my code. My custom API endpoint returns posts by types, and I omitted some code above where I make sure that types I don't want are omitted from the selector, hence the layered REST calls. And you are undoubtedly right about the cached call, but it's never super-clear to me exactly what process is caching what, so I tend to be a bit cautious. I do a lot of dev on a non-WP project where the only cache is what you make yourself, and that bleeds into this stuff. |
One thing which you might investigate: (I think that) edit: function ( props ) {
const getopts = () => {
return // code to get results;
}
let myopts = typeof( myopts ) === 'undefined' ? getopts() : myopts; // or whatever
return(
// your code, using myopts as options for SelectControl
)
} |
Finally, I think what you really want to do upon the initial return from REST of your options list is to call the same "onChange" function that is called when someone makes a selection, to trigger all of the react stuff which happens onChange: edit: function( props ) {
const termChange = val => {
props.setAttributes( { term: val } )
}
const getOpts = () => {
let termList = []; // your code instead of []
if ( typeof( props.attributes.term ) === 'undefined' )
termChange( yourDefaultValue );
return termList;
}
let myopts = typeof( myopts ) === 'undefined' ? getOpts() : myopts;
return (
// 'onChange' for term selector is termChange, 'options' is myopts
)
} |
Very good man, well done. You nailed this with traditional Javascript and without the JSX. For anyone wondering how to use SelectControl, it is a component. So you can use wp.components directly or define the component object before you use it at the top of the file:
|
====> Update to comment The only problem I am having is that onChange, the entire SelectControl disappears from the panel. Very strange. I can't seem to get past this road block. Any advice is appreciated. I think it has something to do with the options being set from this function. Here I've pasted the code which is all from within my edit function const journeyOptions = []; function onChangeJourney( newJourney ) { <SelectControl |
Yes, I want to load data via the WordPress REST API (using |
Dropping this here for those who need to solve this incorrectly in a hurry: (Note, this is the wrong way of doing this as it's piggybacking off of the stylesheet of your block in a nefarious way, the above answer from @rogerlos is more legit, or this looks more like the 'proper' way of doing this, but sometimes needs must. Goes away and adds an issue to circle back around and make more solid once there's better docs.)
Don't be like me 😄 |
is there any method to create dynamic select control ?
The text was updated successfully, but these errors were encountered: