Skip to content

Commit 80bee7a

Browse files
authored
Merge pull request #10 from erimicel/add-more-examples-to-demo
Add disabled option example and style update
2 parents 5b91a4e + fa1bab8 commit 80bee7a

7 files changed

+108
-5
lines changed

dist/select2-tailwindcss-theme-plain.css

+17
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,23 @@
168168
color: rgb(255 255 255 / var(--tw-text-opacity, 1))
169169
}
170170

171+
/* Disabled option items */
172+
173+
.select2-container--tailwindcss-3 .select2-results__option--disabled{
174+
cursor: not-allowed;
175+
--tw-border-opacity: 1;
176+
border-color: rgb(229 231 235 / var(--tw-border-opacity, 1));
177+
--tw-bg-opacity: 1;
178+
background-color: rgb(249 250 251 / var(--tw-bg-opacity, 1));
179+
--tw-text-opacity: 1;
180+
color: rgb(107 114 128 / var(--tw-text-opacity, 1))
181+
}
182+
183+
.select2-container--tailwindcss-3 .select2-results__option--disabled:where(.dark, .dark *){
184+
border-color: rgb(255 255 255 / 0.05);
185+
background-color: rgb(255 255 255 / 0.1)
186+
}
187+
171188
/* Hover state */
172189

173190
.select2-container--tailwindcss-3 .select2-results__option--highlighted{

dist/select2-tailwindcss-theme-plain.min.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/select2-tailwindcss-theme.css

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
@apply relative py-2 pl-3 text-gray-900 transition-colors duration-150 ease-in-out cursor-default select-none pr-9 dark:text-white;
3434
}
3535

36+
/* Disabled option items */
37+
.select2-container--tailwindcss-3 .select2-results__option--disabled {
38+
@apply text-gray-500 cursor-not-allowed bg-gray-50 border-gray-200 dark:bg-white/10 dark:border-white/5;
39+
}
40+
3641
/* Hover state */
3742
.select2-container--tailwindcss-3 .select2-results__option--highlighted {
3843
@apply text-white bg-indigo-600;

dist/select2-tailwindcss-theme.min.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/css/select2-tailwindcss-theme-plain.min.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

+78-2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ <h2 class="mb-6 text-2xl font-semibold text-gray-800 dark:text-white">
7474
</select>
7575
</div>
7676

77+
<!-- Single Ajax Select -->
78+
<div>
79+
<label class="block mb-2 text-sm font-medium text-gray-700 dark:text-white">Example of single ajax select</label>
80+
<select data-placeholder="Search for github repos" data-allow-clear="1" data-fetch="https://api.github.com/search/repositories">
81+
<option></option>
82+
</select>
83+
</div>
84+
7785
<!-- Tags Select -->
7886
<div>
7987
<label class="block mb-2 text-sm font-medium text-gray-700 dark:text-white">Example of dynamic option creation</label>
@@ -100,6 +108,17 @@ <h2 class="mb-6 text-2xl font-semibold text-gray-800 dark:text-white">
100108
<select disabled data-placeholder="Cannot choose" data-allow-clear="1"></select>
101109
</div>
102110

111+
<!-- Disabled option -->
112+
<div>
113+
<label class="block mb-2 text-sm font-medium text-gray-700 dark:text-white">Example of disabled option</label>
114+
<select data-placeholder="Cannot anything">
115+
<option></option>
116+
<option value="one">First</option>
117+
<option value="two" disabled="disabled">Second (disabled)</option>
118+
<option value="three">Third</option>
119+
</select>
120+
</div>
121+
103122
<!-- Multiple Disabled Select -->
104123
<div>
105124
<label class="block mb-2 text-sm font-medium text-gray-700 dark:text-white">Example of disabled multiple select with selected items</label>
@@ -194,15 +213,72 @@ <h2 class="mb-6 text-2xl font-semibold text-gray-800 dark:text-white">
194213

195214
// Select2 init
196215
$('select').each(function () {
197-
$(this).select2({
216+
let options = {
198217
theme: 'tailwindcss-3',
199218
width: $(this).data('width') ? $(this).data('width') : $(this).hasClass('w-100') ? '100%' : 'style',
200219
placeholder: $(this).data('placeholder') || 'Select an option',
201220
allowClear: Boolean($(this).data('allow-clear')),
202221
closeOnSelect: !$(this).attr('multiple'),
203222
tags: Boolean($(this).data('tags')),
204-
});
223+
};
224+
225+
if ($(this).data('fetch')) {
226+
options = {
227+
...options,
228+
ajax: {
229+
url: $(this).data('fetch'),
230+
dataType: 'json',
231+
delay: 250,
232+
data: function (params) {
233+
return {
234+
q: params.term, // search term
235+
page: params.page
236+
};
237+
},
238+
processResults: function (data, params) {
239+
// parse the results into the format expected by Select2
240+
// since we are using custom formatting functions we do not need to
241+
// alter the remote JSON data, except to indicate that infinite
242+
// scrolling can be used
243+
params.page = params.page || 1;
244+
245+
return {
246+
results: data.items,
247+
pagination: {
248+
more: (params.page * 30) < data.total_count
249+
}
250+
};
251+
},
252+
cache: true
253+
},
254+
minimumInputLength: 1,
255+
templateResult: formatRepo,
256+
templateSelection: formatRepoSelection
257+
};
258+
}
259+
260+
$(this).select2(options);
205261
});
262+
263+
function formatRepo(repo) {
264+
if (repo.loading) {
265+
return repo.text;
266+
}
267+
268+
var $container = $(
269+
"<div class='flex'>" +
270+
"<div class='text-base sm:text-sm/6 dark:text-white select2-result-repository__title'></div>" +
271+
"</div>"
272+
);
273+
274+
$container.find(".select2-result-repository__title").text(repo.full_name);
275+
276+
return $container;
277+
}
278+
279+
function formatRepoSelection(repo) {
280+
return repo.full_name || repo.text;
281+
}
206282
});
207283
</script>
208284

0 commit comments

Comments
 (0)