-
Notifications
You must be signed in to change notification settings - Fork 28
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same for this file ^ |
||
this.$selection.attr('aria-required', 'true') | ||
} | ||
|
||
this.$selection.on('focus', function (evt) { | ||
self.trigger('focus', evt); | ||
}); | ||
|
@@ -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') | ||
|
@@ -4405,7 +4415,6 @@ S2.define('select2/dropdown/attachBody',[ | |
|
||
var parentOffset = $offsetParent.offset(); | ||
|
||
css.top -= parentOffset.top; | ||
css.left -= parentOffset.left; | ||
|
||
if (!isCurrentlyAbove && !isCurrentlyBelow) { | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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(); | ||
|
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… (optional)</option><option value="AX" >Å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… (required)</option><option value="AX" >Å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… (required)</option><option value="AX" >Å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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably we also should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every other HTML file in the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> |
There was a problem hiding this comment.
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.