sn-input 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -0
- package/ng-package.json +7 -0
- package/package.json +31 -0
- package/src/lib/sn-input.component.spec.ts +23 -0
- package/src/lib/sn-input.component.ts +81 -0
- package/src/lib/sn-input.html +42 -0
- package/src/lib/sn-input.scss +19 -0
- package/src/lib/sn-input.service.spec.ts +16 -0
- package/src/lib/sn-input.service.ts +9 -0
- package/src/public-api.ts +6 -0
- package/tsconfig.lib.json +14 -0
- package/tsconfig.lib.prod.json +10 -0
- package/tsconfig.spec.json +14 -0
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# SnInput
|
|
2
|
+
|
|
3
|
+
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 21.0.0.
|
|
4
|
+
|
|
5
|
+
## Code scaffolding
|
|
6
|
+
|
|
7
|
+
Run `ng generate component component-name --project sn-input` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project sn-input`.
|
|
8
|
+
> Note: Don't forget to add `--project sn-input` or else it will be added to the default project in your `angular.json` file.
|
|
9
|
+
|
|
10
|
+
## Build
|
|
11
|
+
|
|
12
|
+
Run `ng build sn-input` to build the project. The build artifacts will be stored in the `dist/` directory.
|
|
13
|
+
|
|
14
|
+
## Publishing
|
|
15
|
+
|
|
16
|
+
After building your library with `ng build sn-input`, go to the dist folder `cd dist/sn-input` and run `npm publish`.
|
|
17
|
+
|
|
18
|
+
## Running unit tests
|
|
19
|
+
|
|
20
|
+
Run `ng test sn-input` to execute the unit tests via [Karma](https://karma-runner.github.io).
|
|
21
|
+
|
|
22
|
+
## Further help
|
|
23
|
+
|
|
24
|
+
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page.
|
package/ng-package.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sn-input",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Angular input component with validation and styling - SnUI Library",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Swapnil Nakate",
|
|
7
|
+
"email": "nakate.swapnil7@gmail.com",
|
|
8
|
+
"url": "https://swapnilnakate.in"
|
|
9
|
+
},
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"@angular/common": "^21.0.0",
|
|
12
|
+
"@angular/core": "^21.0.0",
|
|
13
|
+
"autoprefixer": "^10.4.19",
|
|
14
|
+
"postcss": "^8.4.40",
|
|
15
|
+
"tailwindcss": "^3.4.7"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"tslib": "^2.3.0"
|
|
19
|
+
},
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/swapnilnakate7/sn-ui/issues",
|
|
23
|
+
"email": "nakate.swapnil7@gmail.com"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"url": "https://github.com/swapnilnakate7/sn-ui",
|
|
27
|
+
"type": "git",
|
|
28
|
+
"directory": "projects/sn-input"
|
|
29
|
+
},
|
|
30
|
+
"license": "MIT"
|
|
31
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import { SnInputComponent } from './sn-input.component';
|
|
4
|
+
|
|
5
|
+
describe('SnInputComponent', () => {
|
|
6
|
+
let component: SnInputComponent;
|
|
7
|
+
let fixture: ComponentFixture<SnInputComponent>;
|
|
8
|
+
|
|
9
|
+
beforeEach(async () => {
|
|
10
|
+
await TestBed.configureTestingModule({
|
|
11
|
+
imports: [SnInputComponent]
|
|
12
|
+
})
|
|
13
|
+
.compileComponents();
|
|
14
|
+
|
|
15
|
+
fixture = TestBed.createComponent(SnInputComponent);
|
|
16
|
+
component = fixture.componentInstance;
|
|
17
|
+
fixture.detectChanges();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should create', () => {
|
|
21
|
+
expect(component).toBeTruthy();
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { booleanAttribute, Component, Input, OnInit } from '@angular/core';
|
|
2
|
+
import { CommonModule, TitleCasePipe } from '@angular/common';
|
|
3
|
+
import { v4 } from 'uuid';
|
|
4
|
+
import { FormControl, ReactiveFormsModule, ValidationErrors, Validators } from '@angular/forms';
|
|
5
|
+
|
|
6
|
+
@Component({
|
|
7
|
+
selector: 'sn-input',
|
|
8
|
+
standalone: true,
|
|
9
|
+
imports: [CommonModule,ReactiveFormsModule],
|
|
10
|
+
templateUrl: 'sn-input.html',
|
|
11
|
+
styleUrl: 'sn-input.scss',
|
|
12
|
+
})
|
|
13
|
+
export class SnInputComponent implements OnInit {
|
|
14
|
+
@Input({ required: true }) type: string = 'text';
|
|
15
|
+
@Input() label?: string;
|
|
16
|
+
@Input() placeholder?: string;
|
|
17
|
+
@Input() pattern?:string;
|
|
18
|
+
@Input({transform:booleanAttribute}) required:boolean=false;
|
|
19
|
+
@Input() infoMessages?:string[]=[];
|
|
20
|
+
@Input() errorMessages?:string[]=[];
|
|
21
|
+
@Input() warningMessages?:string[]=[];
|
|
22
|
+
@Input() successMessages?:string[]=[];
|
|
23
|
+
|
|
24
|
+
_control:FormControl = new FormControl('');
|
|
25
|
+
_info:boolean=true;
|
|
26
|
+
_error:boolean = false;
|
|
27
|
+
_warning:boolean=false;
|
|
28
|
+
_success:boolean=false;
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
_id = v4();
|
|
32
|
+
upperCasePipe: TitleCasePipe = new TitleCasePipe();
|
|
33
|
+
|
|
34
|
+
ngOnInit(): void {
|
|
35
|
+
if(!this.placeholder) this.placeholder = `Enter ${this.upperCasePipe.transform(this.type)}`;
|
|
36
|
+
|
|
37
|
+
if(this.infoMessages?.length){
|
|
38
|
+
this._info = true;
|
|
39
|
+
}
|
|
40
|
+
if(this.warningMessages?.length){
|
|
41
|
+
this._warning = true;
|
|
42
|
+
}
|
|
43
|
+
if(this.errorMessages?.length){
|
|
44
|
+
this._error = true;
|
|
45
|
+
}
|
|
46
|
+
if(this.successMessages?.length){
|
|
47
|
+
this._success = true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if(this.pattern){
|
|
51
|
+
this._control.addValidators(Validators.pattern(this.pattern));
|
|
52
|
+
}
|
|
53
|
+
this._control.addValidators(this.required? Validators.required:[]);
|
|
54
|
+
|
|
55
|
+
}
|
|
56
|
+
getErrorMessage(type:string,error:ValidationErrors):string{
|
|
57
|
+
let message='';
|
|
58
|
+
console.group('getErrorMessage');
|
|
59
|
+
console.log(type);
|
|
60
|
+
console.log(error);
|
|
61
|
+
if(type === 'pattern') message = `Invalid input for Pattern: ${error['pattern'].requiredPattern}`;
|
|
62
|
+
if(type === 'required') message = `Its a required field`;
|
|
63
|
+
|
|
64
|
+
console.groupEnd();
|
|
65
|
+
return message;
|
|
66
|
+
}
|
|
67
|
+
onInputChange():void{
|
|
68
|
+
console.log(this._control.errors)
|
|
69
|
+
this.errorMessages =[];
|
|
70
|
+
if(this._control.errors){
|
|
71
|
+
const errors:ValidationErrors = this._control.errors;
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
for(const errorKey in errors){
|
|
75
|
+
this.errorMessages.push(this.getErrorMessage(errorKey,errors));
|
|
76
|
+
}
|
|
77
|
+
this._error=true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
|
|
2
|
+
<div class="flex flex-col mb-4">
|
|
3
|
+
<label [for]="_id" class="text-gray-500 text-sm mb-1">{{label||placeholder}}</label>
|
|
4
|
+
|
|
5
|
+
<input [id]="_id" [type]="type"
|
|
6
|
+
[placeholder]="placeholder"
|
|
7
|
+
[formControl]="_control"
|
|
8
|
+
(keyup)="onInputChange()"
|
|
9
|
+
|
|
10
|
+
[ngClass]="{'px-4 py-1.5 bg-white rounded-lg shadow-sm focus:ring-blue-300':type!== 'checkbox'}"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
/>
|
|
14
|
+
<div class="flex flex-col mt-2 mb-4 pl-1">
|
|
15
|
+
@if(_info){
|
|
16
|
+
@for(message of infoMessages;track message){
|
|
17
|
+
<span class="text-sm text-blue-500">{{message}}</span>
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@if(_error){
|
|
22
|
+
@for(message of errorMessages;track message){
|
|
23
|
+
<span class="text-sm text-red-500">{{message}}
|
|
24
|
+
</span>
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@if(_warning){
|
|
29
|
+
@for(message of warningMessages;track message){
|
|
30
|
+
|
|
31
|
+
<span class="text-sm text-yellow-500">{{message}}</span>
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@if(_success){
|
|
36
|
+
@for(message of successMessages;track message){
|
|
37
|
+
<span class="text-sm text-green-500">{{message}}
|
|
38
|
+
</span>
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
</div>
|
|
42
|
+
</div>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
@tailwind base;
|
|
2
|
+
@tailwind components;
|
|
3
|
+
@tailwind utilities;
|
|
4
|
+
|
|
5
|
+
input {
|
|
6
|
+
border: 1px solid #ececec;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
input[type="number"] {
|
|
10
|
+
appearance: none;
|
|
11
|
+
|
|
12
|
+
&::-webkit-inner-spin-button {
|
|
13
|
+
appearance: none;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
&::-webkit-outer-spin-button {
|
|
17
|
+
appearance: none;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import { SnInputService } from './sn-input.service';
|
|
4
|
+
|
|
5
|
+
describe('SnInputService', () => {
|
|
6
|
+
let service: SnInputService;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
TestBed.configureTestingModule({});
|
|
10
|
+
service = TestBed.inject(SnInputService);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should be created', () => {
|
|
14
|
+
expect(service).toBeTruthy();
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
|
2
|
+
{
|
|
3
|
+
"extends": "../../tsconfig.json",
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"outDir": "../../out-tsc/lib",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"inlineSources": true,
|
|
9
|
+
"types": []
|
|
10
|
+
},
|
|
11
|
+
"exclude": [
|
|
12
|
+
"**/*.spec.ts"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
|
2
|
+
{
|
|
3
|
+
"extends": "../../tsconfig.json",
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"outDir": "../../out-tsc/spec",
|
|
6
|
+
"types": [
|
|
7
|
+
"jasmine"
|
|
8
|
+
]
|
|
9
|
+
},
|
|
10
|
+
"include": [
|
|
11
|
+
"**/*.spec.ts",
|
|
12
|
+
"**/*.d.ts"
|
|
13
|
+
]
|
|
14
|
+
}
|