Skip to content

Commit 9f16b72

Browse files
committed
init
0 parents  commit 9f16b72

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+657
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.js
2+
*.js.map
3+
*.log
4+
demo/app/*.js
5+
demo/*.d.ts
6+
demo/platforms
7+
demo/node_modules
8+
node_modules

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
demo/
2+
screens/
3+
*.png
4+
*.log
5+
*.ts
6+
!index.d.ts
7+
!checkbox.d.ts

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
nativescript-checkbox
4+
Copyright (c) 2016, Brad Martin
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# NativeScript-CheckBox
2+
3+
[![npm](https://img.shields.io/npm/v/nativescript-checkbox.svg)](https://www.npmjs.com/package/nativescript-checkbox)
4+
[![npm](https://img.shields.io/npm/dt/nativescript-checkbox.svg?label=npm%20downloads)](https://www.npmjs.com/package/nativescript-checkbox)
5+
6+
# NativeScript CheckBox :white_check_mark:
7+
A NativeScript plugin for the native checkbox widget.
8+
** Android only - there is no concept of a checkbox on iOS. I'm looking to find a cocoapod to bring this plugin to both platforms. **
9+
10+
#### Platform controls used:
11+
Android |
12+
---------- |
13+
[Android CheckBox](https://developer.android.com/reference/android/widget/CheckBox.html) |
14+
15+
16+
## Sample Usage
17+
18+
Sample |
19+
-------------------------------------|
20+
![Sample Usage](./screens/checkbox.gif) |
21+
22+
23+
## Installation
24+
From your command prompt/terminal go to your app's root folder and execute:
25+
26+
`tns plugin add nativescript-checkbox`
27+
28+
## Usage
29+
30+
###
31+
```XML
32+
<Page
33+
xmlns="http://schemas.nativescript.org/tns.xsd"
34+
xmlns:CheckBox="nativescript-checkbox" loaded="pageLoaded">
35+
<ActionBar title="Native Checkbox" />
36+
<StackLayout>
37+
<CheckBox:CheckBox checked="{{ checkProp }}" text="{{ myCheckText }}" color="{{ myCheckColor }}" id="myCheckbox" />
38+
<CheckBox:CheckBox text="CheckBox Label" checked="false" />
39+
</StackLayout>
40+
</Page>
41+
```
42+
43+
###
44+
```TS
45+
import { CheckBox } from 'nativescript-checkbox';
46+
import { topmost } from 'ui/frame';
47+
48+
public toggleCheck() {
49+
let checkBox = topmost().getViewById('yourCheckBoxId');
50+
checkBox.toggle();
51+
}
52+
53+
public getCheckProp() {
54+
let checkBox = topmost().getViewById('yourCheckBoxId');
55+
console.log('checked prop value = ' + checkBox.checked);
56+
}
57+
58+
```
59+
60+
## Properties
61+
62+
- **checked** - boolean
63+
- **text** - text to use with the checkbox
64+
65+
## API
66+
67+
- **toggle()** - Change the checked state of the view to the inverse of its current state.
68+
69+
## Styling
70+
71+
- **color** - set the checkbox color tint - Android 21+ only.

checkbox.android.ts

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import { View } from "ui/core/view";
2+
import { Color } from "color";
3+
import { isAndroid, device } from "platform";
4+
import { Property, PropertyChangeData } from "ui/core/dependency-observable";
5+
import { PropertyMetadata } from "ui/core/proxy";
6+
import style = require("ui/styling/style");
7+
8+
declare var android: any;
9+
10+
export class CheckBox extends View {
11+
private _android: any; /// android.widget.CheckBox
12+
13+
public static checkedProperty = new Property(
14+
"checked",
15+
"CheckBox",
16+
new PropertyMetadata(false)
17+
);
18+
19+
public static textProperty = new Property(
20+
"text",
21+
"CheckBox",
22+
new PropertyMetadata(false)
23+
);
24+
25+
26+
get android() {
27+
return this._android;
28+
}
29+
30+
get _nativeView() {
31+
return this._android;
32+
}
33+
34+
get checked(): boolean {
35+
return this._getValue(CheckBox.checkedProperty);
36+
}
37+
set checked(value: boolean) {
38+
this._setValue(CheckBox.checkedProperty, value);
39+
}
40+
41+
get text(): boolean {
42+
return this._getValue(CheckBox.textProperty);
43+
}
44+
set text(value: boolean) {
45+
this._setValue(CheckBox.textProperty, value);
46+
}
47+
48+
49+
public _createUI() {
50+
51+
this._android = new android.widget.CheckBox(this._context, null);
52+
53+
if (this.text) {
54+
this._android.setText(this.text);
55+
}
56+
57+
var that = new WeakRef(this);
58+
59+
this._android.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener({
60+
get owner() {
61+
return that.get();
62+
},
63+
64+
onCheckedChanged: function (sender, isChecked) {
65+
if (this.owner) {
66+
this.owner._onPropertyChangedFromNative(CheckBox.checkedProperty, isChecked);
67+
}
68+
}
69+
}));
70+
71+
}
72+
73+
public toggle(): void {
74+
this._android.toggle();
75+
}
76+
77+
78+
79+
}
80+
81+
82+
function onCheckedPropertyChanged(data: PropertyChangeData) {
83+
var cBox = <CheckBox>data.object;
84+
if (!cBox.android) {
85+
return;
86+
}
87+
88+
cBox.android.setChecked(data.newValue);
89+
}
90+
91+
// register the setNativeValue callbacks
92+
(<PropertyMetadata>CheckBox.checkedProperty.metadata).onSetNativeValue = onCheckedPropertyChanged;
93+
94+
95+
function onTextPropertyChanged(data: PropertyChangeData) {
96+
var cBox = <CheckBox>data.object;
97+
if (!cBox.android) {
98+
return;
99+
}
100+
101+
cBox.android.setText(data.newValue);
102+
}
103+
104+
// register the setNativeValue callbacks
105+
(<PropertyMetadata>CheckBox.textProperty.metadata).onSetNativeValue = onTextPropertyChanged;
106+
107+
108+
109+
export class CheckBoxStyler implements style.Styler {
110+
private static setColorProperty(view: View, newValue: any) {
111+
var cb = <android.widget.CheckBox>view._nativeView;
112+
if (cb) {
113+
cb.setButtonTintList(android.content.res.ColorStateList.valueOf(new Color(newValue).android));
114+
}
115+
}
116+
117+
private static resetColorProperty(view: View, nativeValue: number) {
118+
// Do nothing.
119+
}
120+
121+
public static registerHandlers() {
122+
style.registerHandler(style.colorProperty, new style.StylePropertyChangedHandler(
123+
CheckBoxStyler.setColorProperty,
124+
CheckBoxStyler.resetColorProperty), "CheckBox");
125+
126+
style.registerHandler(style.borderWidthProperty, style.ignorePropertyHandler, "CheckBox");
127+
style.registerHandler(style.borderColorProperty, style.ignorePropertyHandler, "CheckBox");
128+
style.registerHandler(style.borderRadiusProperty, style.ignorePropertyHandler, "CheckBox");
129+
}
130+
}
131+
132+
CheckBoxStyler.registerHandlers();

checkbox.d.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Contains the CheckBox class, which represents a checkbox component.
3+
*/
4+
declare module "nativescript-checkbox" {
5+
import { View } from "ui/core/view";
6+
import { Property } from "ui/core/dependency-observable";
7+
8+
/**
9+
* Represents a CheckBox component.
10+
*/
11+
export class CheckBox extends View {
12+
13+
/**
14+
* Represents the observable property backing the checked property of each Switch instance.
15+
*/
16+
public static checkedProperty: Property;
17+
18+
/**
19+
* Gets the native [android widget](https://developer.android.com/reference/android/widget/CheckBox.html) that represents the user interface for this component. Valid only when running on Android OS.
20+
*/
21+
android: any /* android.widget.CheckBox */;
22+
23+
/**
24+
* Gets or sets if a switch is checked or not.
25+
*/
26+
checked: boolean;
27+
28+
/**
29+
* Change the checked state of the view to the inverse of its current state.
30+
*/
31+
toggle(): void;
32+
33+
}
34+
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="__PACKAGE__"
4+
android:versionCode="1"
5+
android:versionName="1.0">
6+
7+
<supports-screens
8+
android:smallScreens="true"
9+
android:normalScreens="true"
10+
android:largeScreens="true"
11+
android:xlargeScreens="true"/>
12+
13+
<uses-sdk
14+
android:minSdkVersion="17"
15+
android:targetSdkVersion="__APILEVEL__"/>
16+
17+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
18+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
19+
<uses-permission android:name="android.permission.INTERNET"/>
20+
21+
<application
22+
android:name="com.tns.NativeScriptApplication"
23+
android:allowBackup="true"
24+
android:icon="@drawable/icon"
25+
android:label="@string/app_name"
26+
android:theme="@style/AppTheme" >
27+
<activity
28+
android:name="com.tns.NativeScriptActivity"
29+
android:label="@string/title_activity_kimera"
30+
android:configChanges="keyboardHidden|orientation|screenSize">
31+
32+
<intent-filter>
33+
<action android:name="android.intent.action.MAIN" />
34+
35+
<category android:name="android.intent.category.LAUNCHER" />
36+
</intent-filter>
37+
</activity>
38+
<activity android:name="com.tns.ErrorReportActivity"/>
39+
</application>
40+
</manifest>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Add your native dependencies here:
2+
3+
// Uncomment to add recyclerview-v7 dependency
4+
//dependencies {
5+
// compile 'com.android.support:recyclerview-v7:+'
6+
//}
10.7 KB
Loading
6.03 KB
Loading

0 commit comments

Comments
 (0)