-
Notifications
You must be signed in to change notification settings - Fork 851
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
Dropdown/TagBox - Dynamically create new options from user input #5696
Comments
+1 T12664 - Dropdown value as text if value not in list |
+2 #6964 |
+1 T15375 - How to replicate a Select2 using the JQuery version of SurveyJS |
+1 T16951 - User's answer does not exist in option list - dropdown list question Usage Scenario: Dropdown question should display an option even if it is not listed in a |
|
|
|
we can still hope! this is the only reason I'm still using custom widget... thank you guys |
+1 T20547 - Updating params in custom widget |
Hi. Is there any plan to release this feature in the near/medium term? Thank you. |
T21812 - invisible choices/items |
it's a private ticket. Could you state that the feature will be available in the v2? thank you |
Hi @masciugo, |
T22106 - How to allow users to add custom values to the list in tagbox |
Custom Dropdown which creates new items from a user inputThe following demo uses React-Creatable and shows how to implement a custom dropdown question which enables users to add non-existing items: View CodeSandbox. import Creatable, { useCreatable } from "react-select/creatable";
import { Serializer, RendererFactory, ItemValue } from "survey-core";
import { SurveyQuestionDropdown, ReactQuestionFactory } from "survey-react-ui";
import "survey-core/defaultV2.css";
import "survey-creator-core/survey-creator-core.css";
import "./index.css";
class CustomSelect extends SurveyQuestionDropdown {
constructor(props) {
super(props);
}
get options() {
return this.question.visibleChoices.map((c) => {
return { value: c.value, label: c.value };
});
}
get selectedOption() {
return this.options.filter((o) => o.value == this.question.value);
}
onSelectChange = (selectedOption) => {
this.setValueCore(selectedOption.value);
};
onCreateOption = (inputValue) => {
if (!inputValue.trim()) return;
// Check if value already exists
const exists = this.question.choices.some(
(choice) => choice.value === inputValue
);
if (!exists) {
this.question.choices.push(
new ItemValue({ value: inputValue, text: inputValue })
);
}
this.setValueCore(inputValue);
};
renderElement() {
if (this.isDisplayMode) {
return (
<div
id={this.question.inputId}
className={this.question.getControlClass()}
disabled
>
{this.question.displayValue || this.question.placeholder}
</div>
);
}
return (
<Creatable
id={this.question.inputId}
value={this.selectedOption}
onChange={this.onSelectChange}
onCreateOption={this.onCreateOption}
options={this.options}
required={this.question.isRequired}
menuPortalTarget={document.body}
/>
);
}
}
// Register the CustomSelect component as a renderer under a custom name "sv-dropdown-react"
ReactQuestionFactory.Instance.registerQuestion("sv-dropdown-react", (props) => {
return React.createElement(CustomSelect, props);
});
// Register "sv-dropdown-react" as a renderer for questions whose `type` is "dropdown" and `renderAs` property is "dropdown-react"
RendererFactory.Instance.registerRenderer(
"dropdown",
"dropdown-react",
"sv-dropdown-react"
);
// Set "dropdown-react" as a default value for the `renderAs` property of all "dropdown" questions
Serializer.findProperty("dropdown", "renderAs").defaultValue = "dropdown-react"; |
A Custom Tagbox which creates new items from a user inputThe following demo uses React-Creatable and shows how to implement a custom Tagbox question which enables users to add non-existing items: View Demo.
|
@OlgaLarina
|
https://www.figma.com/design/jcuAUsN02n2yJerx39mqf9/Library-Previews?node-id=3014-2573
To discuss:
survey.data
? - No support.Consider dynamic item creation from user input. For example, as it's done for Select2: https://select2.org/tagging.
Source issue: #5689
The text was updated successfully, but these errors were encountered: