Skip to content

Commit

Permalink
Add support for allowCustom in dropdown fields
Browse files Browse the repository at this point in the history
Replaces #151. Credit to @RomainMazB
  • Loading branch information
LukeTowers committed Feb 6, 2024
1 parent 9a86cfa commit bb79834
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion modules/backend/widgets/form/partials/_field_dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
$fieldOptions = $field->options();
$useSearch = $field->getConfig('showSearch', true);
$emptyOption = $field->getConfig('emptyOption', $field->placeholder);
$allowCustom = $field->getConfig('allowCustom', false);
?>

<!-- Dropdown -->
Expand All @@ -14,7 +15,7 @@
<select
id="<?= $field->getId() ?>"
name="<?= $field->getName() ?>"
class="form-control custom-select <?= $useSearch ? '' : 'select-no-search' ?>"
class="form-control custom-select <?= $useSearch ? '' : 'select-no-search' ?> <?= $allowCustom ? 'select-modifiable' : '' ?>"
<?= $field->getAttributes() ?>
<?= $field->placeholder ? 'data-placeholder="'.e(trans($field->placeholder)).'"' : '' ?>
>
Expand Down
28 changes: 28 additions & 0 deletions modules/system/assets/ui/js/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@
if ($element.hasClass('select-no-search')) {
extraOptions.minimumResultsForSearch = Infinity
}
if ($element.hasClass('select-modifiable')) {
extraOptions.tags = true;

extraOptions.createTag = function (params) {
var term = $.trim(params.term);

if (term === '') {
return null;
}

return {
id: term,
text: term,
newTag: true
};
}

extraOptions.templateResult = function (state) {
if (!state.id) {
return state.text;
}
var icon = state.newTag ? '<i class="icon-plus"></i> ' : '';

return $(
'<span>' + icon + state.text + '</span>'
);
}
}
if ($element.hasClass('select-no-dropdown')) {
extraOptions.dropdownCssClass += ' select-no-dropdown'
extraOptions.containerCssClass += ' select-no-dropdown'
Expand Down
3 changes: 2 additions & 1 deletion modules/system/assets/ui/storm-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bb79834

Please sign in to comment.