Skip to content
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

Y.Button can render as non-submit #968

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 4 additions & 1 deletion src/button/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ Button Change History
@VERSION@
------

* No changes.
* Added type ATTR to ButtonCore to enable Button nodes to be rendered with "type" attribute
The default `type` for `<button>`s `submit`, which is not always desired, especially for ToggleButton.
* `Button` now supports `submit` (backwards-compatible default), `button` and `reset`
* `ToggleButton` is always rendered with `type="button"`

3.10.3
------
Expand Down
35 changes: 28 additions & 7 deletions src/button/js/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Y.extend(Button, Y.Widget, {
}
},

renderUI: function () {
this.getNode().set('type', this.get('type'));
},

/**
* bindUI implementation
*
Expand Down Expand Up @@ -136,6 +140,17 @@ Y.extend(Button, Y.Widget, {
*/
label: {
value: Y.ButtonCore.ATTRS.label.value
},

/**
* Type of button: 'submit', 'button' or 'reset'
*/
type: {
value: 'submit',
validator: function (val) {
return val === 'submit' || val === 'button' || val === 'reset';
},
getter: null // don't get type from node
}
},

Expand All @@ -153,6 +168,10 @@ Y.extend(Button, Y.Widget, {

disabled: function(node) {
return node.getDOMNode().disabled;
},

type: function (node) {
return node.get('type');
}
},

Expand Down Expand Up @@ -208,8 +227,7 @@ Y.extend(ToggleButton, Button, {
*/
initializer: function (config) {
var button = this,
type = button.get('type'),
selectedAttrName = (type === "checkbox" ? 'checked' : 'pressed'),
selectedAttrName = (config.type === "checkbox" ? 'checked' : 'pressed'),
selectedState = config[selectedAttrName] || false;

// Create the checked/pressed attribute
Expand All @@ -228,6 +246,10 @@ Y.extend(ToggleButton, Button, {
delete this.selectedAttrName;
},

renderUI: function () {
this.getNode().set('type', 'button'); // toggles are buttons
},

/**
* @method bindUI
* @description Hooks up events for the widget
Expand Down Expand Up @@ -319,14 +341,13 @@ Y.extend(ToggleButton, Button, {
ATTRS: {

/**
*
*
* @attribute type
* @type String
* 'toggle' (default) or 'checkbox'
*/
type: {
value: 'toggle',
writeOnce: 'initOnly'
validator: function (val) {
return val === 'toggle' || val === 'checkbox';
}
}
},

Expand Down
30 changes: 26 additions & 4 deletions src/button/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ButtonCore.prototype = {
* @param config {Object} Config object.
* @private
*/
_renderUI: function() {
_renderUI: function(config) {
var node = this.getNode(),
tagName = node.get('tagName').toLowerCase();

Expand All @@ -92,6 +92,8 @@ ButtonCore.prototype = {

if (tagName !== 'button' && tagName !== 'input') {
node.set('role', 'button');
} else if (config.type) {
node.set('type', config.type);
}
},

Expand Down Expand Up @@ -142,6 +144,10 @@ ButtonCore.prototype = {
return label;
},

_getType: function () {
return this.getNode().get('type');
},

/**
* @method _uiSetLabel
* @description Setter for a button's 'label' ATTR
Expand Down Expand Up @@ -212,6 +218,22 @@ ButtonCore.ATTRS = {
value: false,
setter: '_uiSetDisabled',
lazyAdd: false
},

/**
* The host node's type
*
* If instantiated without a host, defaults 'submit', but can be set to 'button' or 'reset'
* Else, type is the host's type
*
* @attribute
* @type String
*/
type: {
value: 'submit',
getter: '_getType',
writeOnce: 'initOnly',
lazyAdd: false
}
};

Expand Down Expand Up @@ -242,7 +264,7 @@ ButtonCore.CLASS_NAMES = {
/**
* Array of static constants used to for applying ARIA states
*
* @property CLASS_NAMES
* @property ARIA_STATES
* @type {Object}
* @private
* @static
Expand All @@ -255,7 +277,7 @@ ButtonCore.ARIA_STATES = {
/**
* Array of static constants used to for applying ARIA roles
*
* @property CLASS_NAMES
* @property ARIA_ROLES
* @type {Object}
* @private
* @static
Expand All @@ -267,4 +289,4 @@ ButtonCore.ARIA_ROLES = {
};

// Export Button
Y.ButtonCore = ButtonCore;
Y.ButtonCore = ButtonCore;
35 changes: 35 additions & 0 deletions src/button/tests/unit/assets/button-core-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,41 @@ YUI.add('button-core-test', function (Y) {
Assert.areEqual(button.get('label'), 'foo');
Assert.isInstanceOf(Y.ButtonCore, button);
},

'New ButtonCores nodes should get proper type attribute': function () {
Assert.areEqual('submit', new Y.ButtonCore({}).get('type'));
Assert.areEqual('submit', new Y.ButtonCore({type: 'submit'}).get('type'));
Assert.areEqual('submit', new Y.ButtonCore({type: 'submit'}).get('type'));

Assert.areEqual('button', new Y.ButtonCore({type: 'button'}).get('type')); // sensible
Assert.areEqual('reset', new Y.ButtonCore({type: 'reset'}).get('type')); // sensible

Assert.areEqual('submit', new Y.ButtonCore({type: 'foo'}).get('type')); // ridiculous value, fall back to default
},

'ButtonCore should respect host node\'s type': function () {
function getButtonType () {
return new Y.ButtonCore({
host: Y.one("#testButton")
}).get('type');
}

Y.one("#container").setContent('<button id="testButton">Hoi</button>');
Assert.areEqual('submit', getButtonType());
Y.one("#container").setContent('<button id="testButton" type="submit">Hoi</button>');
Assert.areEqual('submit', getButtonType());

Y.one("#container").setContent('<button id="testButton" type="button">Hoi</button>');
Assert.areEqual('button', getButtonType());

Y.one("#container").setContent('<button id="testButton" type="reset">Hoi</button>');
Assert.areEqual('reset', getButtonType());

Y.one("#container").setContent('<input id="testButton" type="submit">Hoi</button>');
Assert.areEqual('submit', getButtonType());
Y.one("#container").setContent('<input id="testButton" type="button">Hoi</button>');
Assert.areEqual('button', getButtonType());
},

'Modifying the label of a nested button structure should not modify the non-label elements': function () {
Y.one("#container").setContent('<button id="testButton">**<span class="yui3-button-label">Hello</span>**</button>');
Expand Down
48 changes: 48 additions & 0 deletions src/button/tests/unit/assets/button-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ suite.add(new Y.Test.Case({
Assert.areEqual('toggle', role);
},

'ToggleButton node should always be type="button"': function () {
Assert.areEqual('button', this.toggleButton.getNode().get('type'));
Assert.areEqual('button', this.checkButton.getNode().get('type'));
},

'Selecting a toggleButton should add class `yui3-button-selected`': function () {
var button = this.toggleButton,
cb = button.get('contentBox');
Expand Down Expand Up @@ -194,6 +199,22 @@ suite.add(new Y.Test.Case({
Y.one("#container").empty(true);
},

'Passing a type should change the node\'s type': function () {
function getNewButtonType (type) {
return new Y.Button({
type: type,
render: '#container'
}).getNode().get('type');
}

Assert.areEqual('submit', getNewButtonType());
Assert.areEqual('submit', getNewButtonType('submit'));
Assert.areEqual('button', getNewButtonType('button'));
Assert.areEqual('reset', getNewButtonType('reset'));

Assert.areEqual('submit', getNewButtonType('feisty'));
},

'Passing `pressed=true` in with the config will default the button to a `pressed` state': function() {
var button;

Expand Down Expand Up @@ -293,6 +314,33 @@ suite.add(new Y.Test.Case({
Y.one("#container").empty(true);
},

'The parser should read the button type': function () {
function getButtonType () {
var b;
b = new Y.Button({
srcNode: Y.one("#container button, #container input")
});
b.render();
return b.get('type');
}

Y.one("#container").setContent('<button>Hoi</button>');
Assert.areEqual('submit', getButtonType());
Y.one("#container").setContent('<button type="submit">Hoi</button>');
Assert.areEqual('submit', getButtonType());

Y.one("#container").setContent('<button type="button">Hoi</button>');
Assert.areEqual('button', getButtonType());

Y.one("#container").setContent('<button type="reset">Hoi</button>');
Assert.areEqual('reset', getButtonType());

Y.one("#container").setContent('<input type="submit">Hoi</button>');
Assert.areEqual('submit', getButtonType());
Y.one("#container").setContent('<input type="button">Hoi</button>');
Assert.areEqual('button', getButtonType());
},

'The HTML parser for the `label` attribute should reference the button text': function() {

var Test = this,
Expand Down