Skip to content

Commit fc5118b

Browse files
committed
Finally checkbox logic is completed, Utils: calculateCHMod is working
1 parent 513b8ca commit fc5118b

File tree

4 files changed

+83
-10
lines changed

4 files changed

+83
-10
lines changed

src/components.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export namespace Components {
1414
* The first name
1515
*/
1616
'first': string;
17+
'isExecuteChecked': boolean;
18+
'isReadChecked': boolean;
19+
'isWriteChecked': boolean;
1720
/**
1821
* The last name
1922
*/
@@ -22,6 +25,7 @@ export namespace Components {
2225
* The middle name
2326
*/
2427
'middle': string;
28+
'permissionValue': number;
2529
}
2630
}
2731

@@ -44,6 +48,9 @@ declare namespace LocalJSX {
4448
* The first name
4549
*/
4650
'first'?: string;
51+
'isExecuteChecked'?: boolean;
52+
'isReadChecked'?: boolean;
53+
'isWriteChecked'?: boolean;
4754
/**
4855
* The last name
4956
*/
@@ -52,6 +59,7 @@ declare namespace LocalJSX {
5259
* The middle name
5360
*/
5461
'middle'?: string;
62+
'permissionValue'?: number;
5563
}
5664

5765
interface IntrinsicElements {

src/components/chmod-checkbox/chmod-checkbox.tsx

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, Prop, h } from "@stencil/core";
2+
import { calculatePermission } from "../../utils/utils";
23

34
@Component({
45
tag: "chmod-checkbox",
@@ -9,25 +10,43 @@ export class CHModCheckbox {
910
/**
1011
* The first name
1112
*/
12-
@Prop() first: string = "READ";
13+
@Prop({ mutable: true }) first: string = "READ";
1314

1415
/**
1516
* The middle name
1617
*/
17-
@Prop() middle: string = "WRITE";
18+
@Prop({ mutable: true }) middle: string = "WRITE";
1819

1920
/**
2021
* The last name
2122
*/
22-
@Prop() last: string = "EXECUTE";
23+
@Prop({ mutable: true }) last: string = "EXECUTE";
24+
25+
@Prop() permissionValue: number = 0;
26+
@Prop() isReadChecked: boolean = false;
27+
@Prop() isWriteChecked: boolean = false;
28+
@Prop() isExecuteChecked: boolean = false;
2329

2430
render() {
2531
return (
2632
<div class="wrapper">
2733
<div class="column1 animated-checkbox">
2834
<form>
2935
<div class="flex-center-vertically">
30-
<input name="check" type="checkbox" id="read" />
36+
<input
37+
id="read"
38+
name="check"
39+
type="checkbox"
40+
checked={this.isReadChecked}
41+
onChange={() => {
42+
this.isReadChecked = !this.isReadChecked;
43+
this.permissionValue = calculatePermission(
44+
"read",
45+
this.isReadChecked,
46+
this.permissionValue
47+
);
48+
}}
49+
/>
3150
<label htmlFor="read">
3251
<span></span>
3352
</label>
@@ -38,7 +57,20 @@ export class CHModCheckbox {
3857
<div class="column2 animated-checkbox">
3958
<form>
4059
<div class="flex-center-vertically">
41-
<input name="check" type="checkbox" id="write" />
60+
<input
61+
id="write"
62+
name="check"
63+
type="checkbox"
64+
checked={this.isWriteChecked}
65+
onChange={() => {
66+
this.isWriteChecked = !this.isWriteChecked;
67+
this.permissionValue = calculatePermission(
68+
"write",
69+
this.isWriteChecked,
70+
this.permissionValue
71+
);
72+
}}
73+
/>
4274
<label htmlFor="write">
4375
<span></span>
4476
</label>
@@ -49,7 +81,20 @@ export class CHModCheckbox {
4981
<div class="column3 animated-checkbox">
5082
<form>
5183
<div class="flex-center-vertically">
52-
<input name="check" type="checkbox" id="execute" />
84+
<input
85+
id="execute"
86+
name="check"
87+
type="checkbox"
88+
checked={this.isExecuteChecked}
89+
onChange={() => {
90+
this.isExecuteChecked = !this.isExecuteChecked;
91+
this.permissionValue = calculatePermission(
92+
"execute",
93+
this.isExecuteChecked,
94+
this.permissionValue
95+
);
96+
}}
97+
/>
5398
<label htmlFor="execute">
5499
<span></span>
55100
</label>

src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
<script nomodule src="/build/chmod-checkbox.js"></script>
1212
</head>
1313
<body>
14-
<chmod-checkbox />
14+
<chmod-checkbox />
1515
</body>
1616
</html>

src/utils/utils.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1+
export function calculatePermission(
2+
type: string,
3+
logic: boolean,
4+
permission: number
5+
) {
6+
switch (type) {
7+
case "read":
8+
permission = logic === true ? permission + 1 : permission - 1;
9+
break;
10+
case "write":
11+
permission = logic === true ? permission + 2 : permission - 2;
12+
break;
13+
case "execute":
14+
permission = logic === true ? permission + 4 : permission - 4;
15+
break;
16+
default:
17+
return permission;
18+
}
19+
permission < 0 ? (permission = 0) : permission;
20+
console.log("Permission: ", permission);
21+
return permission;
22+
}
123

224
export function format(first: string, middle: string, last: string): string {
325
return (
4-
(first || '') +
5-
(middle ? ` ${middle}` : '') +
6-
(last ? ` ${last}` : '')
26+
(first || "") + (middle ? ` ${middle}` : "") + (last ? ` ${last}` : "")
727
);
828
}

0 commit comments

Comments
 (0)