jenkins-bot has submitted this change and it was merged.
Change subject: CapsuleMultiselectWidget: Allow ignoring user input for
'allowArbitrary' widgets
......................................................................
CapsuleMultiselectWidget: Allow ignoring user input for 'allowArbitrary' widgets
Previously, subclasses had to implement #createItemWidget that would
always return a CapsuleItemWidget instance; they could not "reject"
user input. However in some cases there is just no sane way to produce
one (for a simple example, you only want to allow numbers, and the
user types in 'potato').
After this change, #createItemWidget can return `null`, which will
result in no item being added to the widget and the inputted text
remaining in the text field.
To make some use of this, items with empty labels are now disallowed
using the new system, rather than a special case check.
Bug: T136238
Change-Id: Ifb016e1d2c73ae701cbce4505583e48769fd4ed6
---
M src/widgets/CapsuleMultiselectWidget.js
1 file changed, 18 insertions(+), 7 deletions(-)
Approvals:
Jforrester: Looks good to me, approved
jenkins-bot: Verified
diff --git a/src/widgets/CapsuleMultiselectWidget.js
b/src/widgets/CapsuleMultiselectWidget.js
index ccd1d6f..833969f 100644
--- a/src/widgets/CapsuleMultiselectWidget.js
+++ b/src/widgets/CapsuleMultiselectWidget.js
@@ -204,13 +204,17 @@
/**
* Construct a OO.ui.CapsuleItemWidget (or a subclass thereof) from given
label and data.
+ * May return `null` if the given label and data are not valid.
*
* @protected
* @param {Mixed} data Custom data of any type.
* @param {string} label The label text.
- * @return {OO.ui.CapsuleItemWidget}
+ * @return {OO.ui.CapsuleItemWidget|null}
*/
OO.ui.CapsuleMultiselectWidget.prototype.createItemWidget = function ( data,
label ) {
+ if ( label === '' ) {
+ return null;
+ }
return new OO.ui.CapsuleItemWidget( { data: data, label: label } );
};
@@ -260,7 +264,9 @@
if ( !item ) {
item = widget.createItemWidget( data, label );
}
- widget.addItems( [ item ], i );
+ if ( item ) {
+ widget.addItems( [ item ], i );
+ }
} );
if ( items.length ) {
@@ -288,9 +294,12 @@
if ( !widget.getItemFromData( data ) ) {
item = menu.getItemFromData( data );
if ( item ) {
- items.push( widget.createItemWidget( data,
item.label ) );
+ item = widget.createItemWidget( data,
item.label );
} else if ( widget.allowArbitrary ) {
- items.push( widget.createItemWidget( data,
String( data ) ) );
+ item = widget.createItemWidget( data, String(
data ) );
+ }
+ if ( item ) {
+ items.push( item );
}
}
} );
@@ -309,13 +318,15 @@
* @return {boolean} Whether the item was added or not
*/
OO.ui.CapsuleMultiselectWidget.prototype.addItemFromLabel = function ( label )
{
- var item = this.menu.getItemFromLabel( label, true );
+ var item, items;
+ item = this.menu.getItemFromLabel( label, true );
if ( item ) {
this.addItemsFromData( [ item.data ] );
return true;
- } else if ( this.allowArbitrary && this.$input.val().trim() !== '' ) {
+ } else if ( this.allowArbitrary ) {
+ items = this.getItems();
this.addItemsFromData( [ label ] );
- return true;
+ return !OO.compare( this.getItems(), items );
}
return false;
};
--
To view, visit https://gerrit.wikimedia.org/r/297287
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ifb016e1d2c73ae701cbce4505583e48769fd4ed6
Gerrit-PatchSet: 1
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Bartosz DziewoĆski <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: Prtksxna <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits