Skip to content

feat: add support for required attribute #53

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

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions dist/js/select2.full.js
Original file line number Diff line number Diff line change
Expand Up @@ -1386,9 +1386,14 @@ S2.define('select2/selection/base',[
var id = container.id + '-container';
var resultsId = container.id + '-results';
var searchHidden = this.options.get('minimumResultsForSearch') === Infinity;
var isRequired = this.options.get('required') === true;

this.container = container;

if (isRequired) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a PR under review that drops select2

woocommerce/woocommerce#48731

So these changes might not be required.

this.$selection.attr('aria-required', 'true')
}

this.$selection.on('focus', function (evt) {
self.trigger('focus', evt);
});
Expand Down Expand Up @@ -1545,6 +1550,11 @@ S2.define('select2/selection/single',[

SingleSelection.__super__.bind.apply(this, arguments);

var isRequired = this.options.get('required') === true;
if (isRequired) {
this.$selection.find('.select2-selection__rendered').attr('aria-required', 'true')
}

var id = container.id + '-container';

this.$selection.find('.select2-selection__rendered')
Expand Down Expand Up @@ -4405,7 +4415,6 @@ S2.define('select2/dropdown/attachBody',[

var parentOffset = $offsetParent.offset();

css.top -= parentOffset.top;
css.left -= parentOffset.left;

if (!isCurrentlyAbove && !isCurrentlyBelow) {
Expand All @@ -4420,7 +4429,7 @@ S2.define('select2/dropdown/attachBody',[

if (newDirection == 'above' ||
(isCurrentlyAbove && newDirection !== 'below')) {
css.top = container.top - parentOffset.top - dropdown.height;
css.top = container.top - dropdown.height;
}

if (newDirection != null) {
Expand Down Expand Up @@ -5064,6 +5073,10 @@ S2.define('select2/options',[
this.options.disabled = $e.prop('disabled');
}

if (!this.options.required) {
this.options.required = $e.prop('required');
}

if (this.options.language == null) {
if ($e.prop('lang')) {
this.options.language = $e.prop('lang').toLowerCase();
Expand Down
17 changes: 15 additions & 2 deletions dist/js/select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -1386,9 +1386,14 @@ S2.define('select2/selection/base',[
var id = container.id + '-container';
var resultsId = container.id + '-results';
var searchHidden = this.options.get('minimumResultsForSearch') === Infinity;
var isRequired = this.options.get('required') === true;

this.container = container;

if (isRequired) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for this file ^

woocommerce/woocommerce#48731

this.$selection.attr('aria-required', 'true')
}

this.$selection.on('focus', function (evt) {
self.trigger('focus', evt);
});
Expand Down Expand Up @@ -1545,6 +1550,11 @@ S2.define('select2/selection/single',[

SingleSelection.__super__.bind.apply(this, arguments);

var isRequired = this.options.get('required') === true;
if (isRequired) {
this.$selection.find('.select2-selection__rendered').attr('aria-required', 'true')
}

var id = container.id + '-container';

this.$selection.find('.select2-selection__rendered')
Expand Down Expand Up @@ -4405,7 +4415,6 @@ S2.define('select2/dropdown/attachBody',[

var parentOffset = $offsetParent.offset();

css.top -= parentOffset.top;
css.left -= parentOffset.left;

if (!isCurrentlyAbove && !isCurrentlyBelow) {
Expand All @@ -4420,7 +4429,7 @@ S2.define('select2/dropdown/attachBody',[

if (newDirection == 'above' ||
(isCurrentlyAbove && newDirection !== 'below')) {
css.top = container.top - parentOffset.top - dropdown.height;
css.top = container.top - dropdown.height;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, why we had to make this change? and that too only for Select2 lib and not SelectWoo.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been auto-compiled and bundled within here because of changes from a previous PR that didn't get compiled: https://github.com/woocommerce/selectWoo/pull/49/files

}

if (newDirection != null) {
Expand Down Expand Up @@ -5064,6 +5073,10 @@ S2.define('select2/options',[
this.options.disabled = $e.prop('disabled');
}

if (!this.options.required) {
this.options.required = $e.prop('required');
}

if (this.options.language == null) {
if ($e.prop('lang')) {
this.options.language = $e.prop('lang').toLowerCase();
Expand Down
16 changes: 15 additions & 1 deletion dist/js/selectWoo.full.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* SelectWoo 1.0.10
* SelectWoo 1.0.11
* https://github.com/woocommerce/selectWoo
*
* Released under the MIT license
Expand Down Expand Up @@ -1386,9 +1386,14 @@ S2.define('select2/selection/base',[
var id = container.id + '-container';
var resultsId = container.id + '-results';
var searchHidden = this.options.get('minimumResultsForSearch') === Infinity;
var isRequired = this.options.get('required') === true;

this.container = container;

if (isRequired) {
this.$selection.attr('aria-required', 'true')
}

this.$selection.on('focus', function (evt) {
self.trigger('focus', evt);
});
Expand Down Expand Up @@ -1545,6 +1550,11 @@ S2.define('select2/selection/single',[

SingleSelection.__super__.bind.apply(this, arguments);

var isRequired = this.options.get('required') === true;
if (isRequired) {
this.$selection.find('.select2-selection__rendered').attr('aria-required', 'true')
}

var id = container.id + '-container';

this.$selection.find('.select2-selection__rendered')
Expand Down Expand Up @@ -5063,6 +5073,10 @@ S2.define('select2/options',[
this.options.disabled = $e.prop('disabled');
}

if (!this.options.required) {
this.options.required = $e.prop('required');
}

if (this.options.language == null) {
if ($e.prop('lang')) {
this.options.language = $e.prop('lang').toLowerCase();
Expand Down
16 changes: 15 additions & 1 deletion dist/js/selectWoo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* SelectWoo 1.0.10
* SelectWoo 1.0.11
* https://github.com/woocommerce/selectWoo
*
* Released under the MIT license
Expand Down Expand Up @@ -1386,9 +1386,14 @@ S2.define('select2/selection/base',[
var id = container.id + '-container';
var resultsId = container.id + '-results';
var searchHidden = this.options.get('minimumResultsForSearch') === Infinity;
var isRequired = this.options.get('required') === true;

this.container = container;

if (isRequired) {
this.$selection.attr('aria-required', 'true')
}

this.$selection.on('focus', function (evt) {
self.trigger('focus', evt);
});
Expand Down Expand Up @@ -1545,6 +1550,11 @@ S2.define('select2/selection/single',[

SingleSelection.__super__.bind.apply(this, arguments);

var isRequired = this.options.get('required') === true;
if (isRequired) {
this.$selection.find('.select2-selection__rendered').attr('aria-required', 'true')
}

var id = container.id + '-container';

this.$selection.find('.select2-selection__rendered')
Expand Down Expand Up @@ -5063,6 +5073,10 @@ S2.define('select2/options',[
this.options.disabled = $e.prop('disabled');
}

if (!this.options.required) {
this.options.required = $e.prop('required');
}

if (this.options.language == null) {
if ($e.prop('lang')) {
this.options.language = $e.prop('lang').toLowerCase();
Expand Down
1 change: 1 addition & 0 deletions docs/accessibility-testing/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ <h2>SelectWoo Accessibility Testing Samples</h2>
<li><a href="samples/ajax.html">Single select with ajax</a></li>
<li><a href="samples/multi.html">Multiselect</a></li>
<li><a href="samples/multi-ajax.html">Multiselect with ajax</a></li>
<li><a href="samples/required.html">Single select with required option</a></li>
</ul>
</body>
24 changes: 24 additions & 0 deletions docs/accessibility-testing/samples/required.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<head>
<title>Single select with required option</title>

<link href="../select2.min.css" type="text/css" rel="stylesheet" />

<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="../selectWoo.full.js"></script>
</head>

<body>
<h2>Required attribute</h2>

<select name="not_required" id="not_required" class="country_to_state country_select "><option value="">Select a country&hellip; (optional)</option><option value="AX" >&#197;land Islands</option><option value="AF" >Afghanistan</option><option value="AL" >Albania</option><option value="DZ" >Algeria</option><option value="AS" >American Samoa</option><option value="AD" >Andorra</option><option value="AO" >Angola</option><option value="AI" >Anguilla</option><option value="AQ" >Antarctica</option><option value="EH" >Western Sahara</option><option value="YE" >Yemen</option><option value="ZM" >Zambia</option><option value="ZW" >Zimbabwe</option></select>
<select name="required_attribute" id="required_attribute" class="country_to_state country_select " required><option value="">Select a country&hellip; (required)</option><option value="AX" >&#197;land Islands</option><option value="AF" >Afghanistan</option><option value="AL" >Albania</option><option value="DZ" >Algeria</option><option value="AS" >American Samoa</option><option value="AD" >Andorra</option><option value="AO" >Angola</option><option value="AI" >Anguilla</option><option value="AQ" >Antarctica</option><option value="EH" >Western Sahara</option><option value="YE" >Yemen</option><option value="ZM" >Zambia</option><option value="ZW" >Zimbabwe</option></select>
<select name="required_option" id="required_option" class="country_to_state country_select "><option value="">Select a country&hellip; (required)</option><option value="AX" >&#197;land Islands</option><option value="AF" >Afghanistan</option><option value="AL" >Albania</option><option value="DZ" >Algeria</option><option value="AS" >American Samoa</option><option value="AD" >Andorra</option><option value="AO" >Angola</option><option value="AI" >Anguilla</option><option value="AQ" >Antarctica</option><option value="EH" >Western Sahara</option><option value="YE" >Yemen</option><option value="ZM" >Zambia</option><option value="ZW" >Zimbabwe</option></select>

<script>
$( '#not_required' ).select2();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we also should use .selectWoo signature which is consistent with what we are using in the core.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every other HTML file in the sample directory uses select2, so I thought I'd be consistent here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though we still support select2's signature, we have officially removed it from our repo, woocommerce/woocommerce#48731. Maybe as part of another Issue, we should keep the samples consistent.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that sounds fine!

$( '#required_attribute' ).select2();
$( '#required_option' ).select2({
required: true,
});
</script>
</body>
Loading