I have multiple Y.Button instances that are logically associated and need to be managed as a group.
For example, you may need a radio group, so when one button becomes selected, the others are unselected. Or a checkbox group, which allows you to select multiple buttons and at some point you need to obtain the values of all selected buttons.
Note: A push group is really just a checkbox group, but with Y.Button instances whose types are push as opposed to toggle.
addButton- Adds a button to the groupgetButtons- Returns an array of Y.Button instances assigned to the groupgetSelectedButtons- Returns an array of Y.Button instances that are currently selectedgetSelectedValues- Returns an array of the values of select buttons (sugar)
type('checkbox' [default], or 'radio')
selectionChange- Fires after theselectedstate on a grouped button changes.
button-base(requires:base,classnamemanager,node)
// Instantiation
group = new Y.ButtonGroup({type: 'radio'});
buttonA = new Y.Button({srcNode: "#foo"});
buttonB = new Y.Button({srcNode: "#bar"});
// Interaction
group.addButton(ButtonA);
group.addButton(ButtonB);
buttonA.select();
selected = group.getSelectedButtons(); // Returns buttonA
buttonB.select();
selected = group.getSelectedButtons(); // Returns buttonB- Add support for a
buttonsattribute in the Y.ButtonGroup config object to add all buttons on instantiation. Update: see comment #4 - Add support for a
srcNodesselector attribute in the Y.ButtonGroup config object to create Y.Button instances for each node and add to the group. Update: see comment #4 - Add support for a
clickevent (exact name TBD) which fires when any button in the group is clicked. This would be most applicable topushbutton groups.
Actually,
Y.Buttoncreating nodes on the fly was a late addition. It would be preferable for the developer to useY.Node.create()so then they have full control over the button. But feedback was that it seemed logical forY.Buttonto also create buttons, which does make sense, so I added that in. If srcNode is empty at instantiation, it assigns that attritbute asY.Node.create('<button></button>');.Instead of
Y.Buttonsupportingvaluedirectly, it abstracts it out into alabelattribute (which I think is what YUI 2 did). But now that I review the code that renders that attribute, I see an issue:It works fine for
<div>,<a>, etc..., and since<input>only has avalueattribute (noinnerHTML) it will work fine for that as well. But what about a<button>node, which has bothvalueandinnerHTML/text? A singlelabelattribute won't work, so I'll have to fix that.It's not quite as simple as just throwing in a
valueattribute, because what happens here?<button>and<input>both have avalueattribute, but a<div>,<a>, etc... do not. So doesY.Buttonjust ignore thevalueproperty? Does it assign it as a custom attribute? It's probably one of those "You should know better that to do this" things, but you never know, and I haven't had a chance to fully think through it yet.So, I'll work to resolve this issue before 3.5.0pr2.
Good question. Thanks