Skip to content

Instantly share code, notes, and snippets.

@donny-dont
Created September 30, 2012 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donny-dont/3808109 to your computer and use it in GitHub Desktop.
Save donny-dont/3808109 to your computer and use it in GitHub Desktop.
Proposed checked/unchecked for RadioButton, CheckBox
// Copyright (c) 2012, John Evans
// https://github.com/prujohn/Buckshot
// See LICENSE file for Apache 2.0 licensing information.
/**
* A button that only allows a single selection when part of the same group. */
class RadioButton extends Control
{
FrameworkProperty valueProperty, groupNameProperty;
final FrameworkEvent selectionChanged;
final FrameworkEvent checked;
final FrameworkEvent unchecked;
RadioButton()
: selectionChanged = new FrameworkEvent<EventArgs>()
, checked = new FrameworkEvent<EventArgs>()
, unchecked = new FrameworkEvent<EventArgs>()
{
Browser.appendClass(rawElement, "radiobutton");
_initProperties();
_initEvents();
registerEvent('selectionchanged', selectionChanged);
registerEvent('checked', checked);
registerEvent('unchecked', unchecked);
}
RadioButton.register() : super.register(),
selectionChanged = new FrameworkEvent<EventArgs>();
makeMe() => new RadioButton();
void _initProperties(){
valueProperty = new FrameworkProperty(this, "value", (String v){
rawElement.attributes["value"] = v;
});
groupNameProperty = new FrameworkProperty(this, "groupName", (String v){
rawElement.attributes["name"] = v;
}, "default");
}
void _initEvents(){
click + (_, __){
EventArgs args = new EventArgs();
if (isChecked)
checked.invoke(this, args);
else
checked.invoke(this, args);
selectionChanged.invoke(this, args);
};
}
String get value => getValue(valueProperty);
set value(String v) => setValue(valueProperty, v);
String get groupName => getValue(groupNameProperty);
set groupName(String v) => setValue(groupNameProperty, v);
bool get isChecked => rawElement.checked;
void createElement(){
rawElement = new InputElement();
rawElement.attributes["type"] = "radio";
}
void setAsSelected(){
rawElement.attributes["checked"] = "true";
selectionChanged.invoke(this, new EventArgs());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment