Dynamic options for Select Field #564
Replies: 7 comments 9 replies
-
Hello @webcredo!
Currently, no. The setup for graphql and database schema, and validation are all strict around the predefined values.
As a future enhancement I would like to see the Your best bet today is to use a You can copy much of what you need for it to work from the Payload Relationships field since it uses react-select and calls an api to get options. If you get stuck we can help you more on this and if you make something worth sharing I'd love to see you share it here for others to use. |
Beta Was this translation helpful? Give feedback.
-
import React, { useCallback, useEffect, useState } from "react";
import {
useField,
SelectInput,
} from "payload/components/forms";
const Select = (props) => {
const { path, name, label, options } = props;
const [opts, setOpts] = useState([]);
const { showError, value, setValue } = useField({
path,
});
useEffect(() => {
setOpts(options);
// Mimic network request...
setTimeout(() => {
setOpts([
{
label: "Some Label",
value: "__value__",
},
]);
console.log("Set Options Called!");
}, 3000);
}, []);
const onChange = useCallback(
(incomingOption) => {
const { value: incomingValue } = incomingOption;
setValue(incomingValue);
},
[setValue]
);
return (
<SelectInput
name={name}
label={label}
options={opts}
value={value}
onChange={onChange}
showError={showError}
/>
);
};
export default Select; @DanRibbens |
Beta Was this translation helpful? Give feedback.
-
Is this something you would want help implementing?
It doesn't look like the creatable react component accept additional props of interest, except for perhaps |
Beta Was this translation helpful? Give feedback.
-
Today, I took a copy of the existing Select field and modified the The limitation that I've now hit is that passing custom properties into this custom field doesn't seem to be possible because of restrictions on types. When I specify my collection, I've set my field up as I've worked around this by wrapping my
Then I execute the loader function inside the This is the best way I've found to minimise boilerplate code when needing to create a variety of |
Beta Was this translation helpful? Give feedback.
-
Sorry for late reply as I was busy with my day job. Yes, I figured out the way at that time to make select field dynamic. I created a custom component for that, I am pasting the code below, but I don't know if it is completely correct to use as I last worked on that project 10 months ago. It worked for me at that time. import React, { useCallback, useEffect, useState } from "react";
import { useField, SelectInput } from "payload/components/forms";
const Select = (props) => {
const { path, name, label, options, required, access } = props;
const [opts, setOpts] = useState([]);
const { showError, value, setValue } = useField({
path,
});
useEffect(() => {
setOpts(options);
fetch(`https://your_api`)
.then((response) => response.json())
.then((data) => {
let mOptions = [];
data.forEach((option) => {
mOptions.push({
label: option,
value: option,
});
});
setOpts(mOptions);
});
}, []);
const onChange = useCallback(
(incomingOption) => {
const { value: incomingValue } = incomingOption;
console.log("Incoming Value:", incomingValue);
setValue(incomingValue);
},
[setValue]
);
return (
<SelectInput
name={name}
label={label}
options={opts}
value={value}
access={access}
required={required}
onChange={onChange}
showError={showError}
/>
);
};
export default Select; |
Beta Was this translation helpful? Give feedback.
-
I think it should not "only" be about dynamic loading the options, but using AsyncSelect from react-select for typeahead functionality. Something we two callable's can be given, one to resolve the option based on value (initial load) and one for the concrete typeahead resolving related options. The problem with this idea is thats its incompatible with for example the whole custom react select code within payload. |
Beta Was this translation helpful? Give feedback.
-
@JessChowdhury created a video on this exact topic, check it out here: https://www.youtube.com/watch?v=Efn9OxSjA6Y |
Beta Was this translation helpful? Give feedback.
-
Wondering if is possible to make options for select field to be dynamic and not only predefined?
For example I want show options for this fields from another source by API request.
Beta Was this translation helpful? Give feedback.
All reactions