Skip to content

Commit c1b1c92

Browse files
author
Hamed
committed
version 1 (100% coverage)
1 parent ca76c73 commit c1b1c92

File tree

9 files changed

+3512
-67
lines changed

9 files changed

+3512
-67
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,8 @@ typings/
6868
out
6969
gen
7070

71-
dist/
71+
dist/
72+
73+
input.js
74+
check.js
75+
radio.js

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Quickly integrate <a href="https://lokesh-coder.github.io/pretty-checkbox/">pret
1212
<img alt="" src="https://img.shields.io/badge/%20pretty--checkbox-3.x-green.svg?style=flat-square&colorA=8033b0&colorB=75b7dd">
1313
</a>
1414
<a href="https://vuejs.org">
15-
<img alt="" src="https://img.shields.io/badge/vue.js-2.x-green.svg?style=flat-square&colorA=35495e&colorB=41b883">
15+
<img alt="" src="https://img.shields.io/badge/vue.js-^2.2.0-green.svg?style=flat-square&colorA=35495e&colorB=41b883">
1616
</a>
1717

1818
<br>
@@ -32,6 +32,13 @@ Quickly integrate <a href="https://lokesh-coder.github.io/pretty-checkbox/">pret
3232

3333
</p>
3434

35+
<div class="highlight highlight-source-shell">
36+
<pre>
37+
<div align="center"><strong>Demo and documentation</strong></div>
38+
<div align="center"><a align="center" href="https://hamed-ehtesham.github.io/pretty-checkbox-vue/">https://hamed-ehtesham.github.io/pretty-checkbox-vue/</a></div>
39+
</pre>
40+
</div>
41+
3542
## Installation
3643

3744
```js
@@ -55,15 +62,33 @@ Include the script file, then install the component with `Vue.use(PrettyCheckbox
5562
```js
5663
import PrettyCheckbox from 'pretty-checkbox-vue';
5764

58-
Vue.component('p-checkbox', PrettyCheckbox);
65+
Vue.use(PrettyCheckbox);
66+
```
67+
68+
Or
69+
70+
```js
71+
import PrettyInput from 'pretty-checkbox-vue/input';
72+
import PrettyCheck from 'pretty-checkbox-vue/check';
73+
import PrettyRadio from 'pretty-checkbox-vue/radio';
74+
75+
Vue.component('p-input', PrettyInput);
76+
Vue.component('p-check', PrettyCheck);
77+
Vue.component('p-radio', PrettyRadio);
5978
```
6079

6180
## Usage
6281

6382
Once installed, it can be used in a template as simply as:
6483

6584
```html
66-
<p-checkbox name="remember" class="p-default" button-variant="success" v-model="remember">remember</p-checkbox>
85+
<p-check name="check" color="success" v-model="check">check</p-check>
86+
<p-radio name="radio" color="info" v-model="radio">radio</p-radio>
87+
88+
<!-- Or it can be used just like input -->
89+
90+
<p-input type="checkbox" name="check" color="success" v-model="check">check</p-input>
91+
<p-input type="radio" name="radio" color="info" v-model="radio">radio</p-input>
6792
```
6893

6994
If you have discovered a 🐜 or have a feature suggestion, feel free to create an [issue](https://github.com/hamed-ehtesham/pretty-checkbox-vue/issues) on Github.

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"Checkbox",
1717
"Radio"
1818
],
19-
"main": "src/PrettyCheckbox.vue",
19+
"main": "dist/pretty-checkbox-vue.js",
2020
"scripts": {
2121
"build": "rimraf ./dist && webpack --config ./webpack.config.js"
2222
},
@@ -26,7 +26,10 @@
2626
},
2727
"files": [
2828
"src",
29-
"dist"
29+
"dist",
30+
"input.js",
31+
"check.js",
32+
"radio.js"
3033
],
3134
"author": "Hamed Ehtesham",
3235
"license": "MIT",
@@ -35,7 +38,7 @@
3538
},
3639
"homepage": "https://hamed-ehtesham.github.io/pretty-checkbox-vue/",
3740
"peerDependencies": {
38-
"vue": "^2.4.2"
41+
"vue": "^2.2.0"
3942
},
4043
"dependencies": {
4144
"pretty-checkbox": "^3.0.3"
@@ -46,7 +49,6 @@
4649
"babel-plugin-transform-object-rest-spread": "^6.26.0",
4750
"babel-plugin-transform-runtime": "^6.9.0",
4851
"babel-preset-env": "^1.6.1",
49-
"babel-preset-es2015": "^6.9.0",
5052
"babel-preset-stage-2": "^6.11.0",
5153
"babel-runtime": "^6.9.2",
5254
"css-loader": "^0.28.7",

src/PrettyCheckbox.vue

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,21 @@
1-
<template>
2-
<div class="pretty">
3-
<input type="checkbox" :name="name" :checked="checked" :disabled="disabled" :required="required" v-model="val"/>
4-
<div :class="classes">
5-
<label>
6-
<slot></slot>
7-
</label>
8-
</div>
9-
</div>
10-
</template>
11-
121
<script>
13-
export default {
14-
name: "pretty-checkbox",
15-
16-
props: {
17-
value: {},
18-
checked: Boolean,
19-
buttonVariant: String,
20-
name: String,
21-
disabled: Boolean,
22-
required: {
23-
type: Boolean,
24-
default: false,
25-
},
26-
state: {},
27-
plain: {
28-
type: Boolean,
29-
default: false,
30-
},
31-
indeterminate: Boolean,
32-
},
2+
let prettyInput = require('./PrettyInput.vue');
333
34-
data() {
35-
return {
36-
val: null,
37-
}
38-
},
39-
40-
computed: {
41-
classes() {
42-
let classes = {state: true,};
43-
classes[`p-${this.buttonVariant}`] = true;
44-
45-
return classes;
46-
}
47-
},
4+
let prettyCheckbox = {
5+
name: "pretty-checkbox",
486
49-
watch: {
50-
val(v) {
51-
this.$emit('input', v);
52-
},
7+
input_type: 'checkbox',
538
54-
value(v) {
55-
this.val = v;
56-
}
57-
},
58-
}
59-
</script>
9+
model: prettyInput.model,
10+
props: prettyInput.props,
11+
data: prettyInput.data,
12+
computed: prettyInput.computed,
13+
watch: prettyInput.watch,
14+
mounted: prettyInput.mounted,
15+
methods: prettyInput.methods,
6016
61-
<style scoped>
17+
render: prettyInput.render,
18+
};
6219
63-
</style>
20+
export default prettyCheckbox;
21+
</script>

src/PrettyInput.vue

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<template>
2+
<div :class="classes">
3+
<input ref="input" :type="_type" :name="name"
4+
:checked="shouldBeChecked" :value="value" @change="updateInput"
5+
:disabled="_disabled" :required="_required"/>
6+
<div :class="onClasses">
7+
<slot name="extra"></slot>
8+
<label>
9+
<slot></slot>
10+
</label>
11+
</div>
12+
<div v-if="_toggle" :class="offClasses">
13+
<slot name="off-extra"></slot>
14+
<slot name="off-label"></slot>
15+
</div>
16+
<div v-if="_hover" :class="hoverClasses">
17+
<slot name="hover-extra"></slot>
18+
<slot name="hover-label"></slot>
19+
</div>
20+
<div v-if="indeterminate" :class="indeterminateClasses">
21+
<slot name="indeterminate-extra"></slot>
22+
<slot name="indeterminate-label"></slot>
23+
</div>
24+
</div>
25+
</template>
26+
27+
<script>
28+
export default {
29+
name: "pretty-input",
30+
31+
model: {
32+
prop: 'modelValue',
33+
event: 'change'
34+
},
35+
36+
props: {
37+
type: String,
38+
name: String,
39+
value: String,
40+
modelValue: {},
41+
trueValue: {},
42+
falseValue: {},
43+
checked: {},
44+
disabled: {},
45+
required: {},
46+
indeterminate: {},
47+
color: String,
48+
offColor: String,
49+
hoverColor: String,
50+
indeterminateColor: String,
51+
toggle: {},
52+
hover: {},
53+
focus: {},
54+
},
55+
56+
data() {
57+
return {
58+
default_mode: false,
59+
}
60+
},
61+
62+
computed: {
63+
_type() {
64+
if (this.$options.input_type)
65+
return this.$options.input_type;
66+
if (this.type)
67+
return this.type;
68+
69+
return 'checkbox';
70+
},
71+
shouldBeChecked() {
72+
if (this.modelValue !== undefined) {
73+
// radio
74+
if (this._type === 'radio') {
75+
return this.modelValue === this.value;
76+
}
77+
78+
// checkbox
79+
if (this.modelValue instanceof Array) {
80+
return this.modelValue.includes(this.value);
81+
}
82+
if (this._trueValue) {
83+
return this.modelValue === this.trueValue;
84+
}
85+
return typeof this.modelValue === 'string' ? true : !!this.modelValue;
86+
}
87+
88+
return typeof this.checked === 'string' ? true : !!this.checked;
89+
},
90+
_disabled() {
91+
return typeof this.disabled === 'string' ? true : !!this.disabled;
92+
},
93+
_required() {
94+
return typeof this.required === 'string' ? true : !!this.required;
95+
},
96+
_trueValue() {
97+
return typeof this.trueValue === 'string' ? this.trueValue : !!this.trueValue;
98+
},
99+
_falseValue() {
100+
return typeof this.falseValue === 'string' ? this.falseValue : !!this.falseValue;
101+
},
102+
_toggle() {
103+
return typeof this.toggle === 'string' ? true : !!this.toggle;
104+
},
105+
_hover() {
106+
return typeof this.hover === 'string' ? true : !!this.hover;
107+
},
108+
_focus() {
109+
return typeof this.focus === 'string' ? true : !!this.focus;
110+
},
111+
classes() {
112+
return {
113+
pretty: true,
114+
'p-default': this.default_mode,
115+
'p-toggle': this._toggle,
116+
'p-has-hover': this._hover,
117+
'p-has-focus': this._focus,
118+
'p-has-indeterminate': this.indeterminate,
119+
};
120+
},
121+
onClasses() {
122+
let classes = {
123+
state: true,
124+
'p-on': this._toggle,
125+
};
126+
if (this.color)
127+
classes[`p-${this.color}`] = true;
128+
129+
return classes;
130+
},
131+
offClasses() {
132+
let classes = {
133+
state: true,
134+
'p-off': true,
135+
};
136+
if (this.offColor)
137+
classes[`p-${this.offColor}`] = true;
138+
139+
return classes;
140+
},
141+
hoverClasses() {
142+
let classes = {
143+
state: true,
144+
'p-is-hover': true,
145+
};
146+
if (this.hoverColor)
147+
classes[`p-${this.hoverColor}`] = true;
148+
149+
return classes;
150+
},
151+
indeterminateClasses() {
152+
let classes = {
153+
state: true,
154+
'p-is-indeterminate': true,
155+
};
156+
if (this.indeterminateColor)
157+
classes[`p-${this.indeterminateColor}`] = true;
158+
159+
return classes;
160+
}
161+
},
162+
163+
watch: {
164+
indeterminate(v) {
165+
this.$refs.input.indeterminate = v;
166+
},
167+
},
168+
169+
mounted() {
170+
if (this.$vnode.data && !this.$vnode.data.staticClass)
171+
this.default_mode = true;
172+
},
173+
174+
methods: {
175+
updateInput(event) {
176+
if (this._type === 'radio') {
177+
this.$emit('change', this.value);
178+
return;
179+
}
180+
181+
let isChecked = event.target.checked;
182+
183+
if (this.modelValue instanceof Array) {
184+
let newValue = [...this.modelValue];
185+
186+
if (isChecked) {
187+
newValue.push(this.value);
188+
} else {
189+
newValue.splice(newValue.indexOf(this.value), 1);
190+
}
191+
192+
this.$emit('change', newValue);
193+
} else {
194+
this.$emit('change', isChecked ? (this._trueValue ? this.trueValue : true) : (this._falseValue ? this.falseValue : false));
195+
}
196+
}
197+
}
198+
};
199+
</script>

0 commit comments

Comments
 (0)