Skip to content

Commit 4551dd0

Browse files
committed
docs: add docs use docgeni
1 parent 67da6e1 commit 4551dd0

27 files changed

+2303
-124
lines changed

.docgeni/public/styles.scss

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
@import '~bootstrap/scss/bootstrap.scss';
2+
@import '@docgeni/template/styles/index.css';
3+
4+
@mixin ngx-form-validation-state($state, $color) {
5+
.ngx-custom-select {
6+
.was-validated &:#{$state},
7+
&.is-#{$state} {
8+
// border-color: $color;
9+
border: 1px solid $color;
10+
11+
@if $enable-validation-icons {
12+
padding-right: $input-height-inner;
13+
background-repeat: no-repeat;
14+
background-position: center right calc(#{$input-height-inner} / 4);
15+
background-size: calc(#{$input-height-inner} / 2) calc(#{$input-height-inner} / 2);
16+
17+
@if $state == 'valid' {
18+
background-image: $form-feedback-icon-valid;
19+
} @else {
20+
background-image: $form-feedback-icon-invalid;
21+
}
22+
}
23+
24+
&:focus {
25+
border-color: $color;
26+
box-shadow: 0 0 0 $input-focus-width rgba($color, 0.25);
27+
}
28+
29+
~ .#{$state}-feedback,
30+
~ .#{$state}-tooltip {
31+
display: block;
32+
}
33+
}
34+
}
35+
}
36+
37+
@include ngx-form-validation-state('valid', $form-feedback-valid-color);
38+
@include ngx-form-validation-state('invalid', $form-feedback-invalid-color);
39+
40+
a:hover {
41+
text-decoration: none;
42+
}

.docgeni/public/tsconfig.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": "./",
4+
"outDir": "./dist/out-tsc/site",
5+
"sourceMap": true,
6+
"declaration": false,
7+
"downlevelIteration": true,
8+
"experimentalDecorators": true,
9+
"module": "ESNext",
10+
"moduleResolution": "node",
11+
"esModuleInterop": false,
12+
"importHelpers": true,
13+
"target": "es2015",
14+
"lib": [
15+
"es2018",
16+
"dom"
17+
],
18+
"types": [
19+
"node"
20+
],
21+
"paths":{
22+
"@why520crazy/ngx-validator/*":["../../packages/core/src/*"],
23+
"@why520crazy/ngx-validator":["../../packages/core/src/index.ts","../../packages/core/src/public-api.ts"],
24+
}
25+
},
26+
"files": [
27+
"src/main.ts",
28+
"src/polyfills.ts"
29+
],
30+
"include": [
31+
"src/**/*.d.ts"
32+
],
33+
"exclude": [
34+
"src/assets",
35+
"test.ts"
36+
]
37+
}

.docgenirc.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @type {import('@docgeni/core').DocgeniConfig}
3+
*/
4+
module.exports = {
5+
mode: 'lite',
6+
title: 'ngx-validator',
7+
logoUrl: 'https://cdn.pingcode.com/open-sources/docgeni/logo.png',
8+
description: '',
9+
docsDir: 'docs',
10+
navs: [
11+
null,
12+
{
13+
title: 'Directives',
14+
path: 'components',
15+
lib: 'core',
16+
hide: true,
17+
hidden: true,
18+
locales: {}
19+
}
20+
],
21+
libs: [
22+
{
23+
name: 'core',
24+
rootDir: 'packages/core',
25+
abbrName: 'ngx',
26+
include: ['src', 'examples'],
27+
categories: []
28+
}
29+
]
30+
};

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ testem.log
3838
# System Files
3939
.DS_Store
4040
Thumbs.db
41+
.docgeni/site

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.md
2+
*.json

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ yarn add @why520crazy/ngx-validator
3434

3535
#### Loading the module in the app module
3636

37-
```
37+
```ts
3838
import { NgxValidatorModule, ValidationFeedbackStrategyBuilder } from '@why520crazy/ngx-validator';
3939

4040
@NgModule({
@@ -59,7 +59,7 @@ class AppModule {}
5959

6060
add `ngxFormValidator` directive to form element and add `ngxFormSubmit` directive to submit button.
6161

62-
```
62+
```html
6363
<form name="exampleForm" novalidate [ngxFormValidator]="validatorConfig">
6464
<div class="form-group">
6565
<label for="email1">Email address</label>
@@ -143,7 +143,7 @@ get `formValidator` by `<form #formValidator="ngxFormValidator">`
143143

144144
#### Custom Feedback Strategy
145145

146-
```
146+
```ts
147147
const CUSTOM_INVALID_CLASS = 'custom-invalid';
148148
const CUSTOM_INVALID_FEEDBACK_CLASS = 'custom-invalid-feedback';
149149

docs/index.md

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: Intro
3+
order: 10
4+
---
5+
6+
An Angular 7+ form validator library, may be the best angular validator library in the world.
7+
8+
> handle validation messages easy and automatic, don't need to manually write error tips templates, just configure validation rules, and support extensive custom feedback strategy.
9+
10+
## Installation
11+
12+
```
13+
npm install @why520crazy/ngx-validator --save
14+
# or
15+
yarn add @why520crazy/ngx-validator
16+
```
17+
18+
## Getting Started
19+
20+
21+
### import NgxValidatorModule
22+
Loading the module in the any module (AppModule or Feature module).
23+
24+
```ts
25+
import { NgxValidatorModule, ValidationFeedbackStrategyBuilder } from '@why520crazy/ngx-validator';
26+
27+
@NgModule({
28+
imports: [
29+
CommonModule,
30+
NgxValidatorModule.forRoot({
31+
validationFeedbackStrategy: ValidationFeedbackStrategyBuilder.bootstrap(), // default is bootstrap 4 style
32+
validationMessages: {
33+
username: {
34+
required: 'Username is required.',
35+
pattern: 'Incorrect username format.'
36+
}
37+
},
38+
validateOn: 'submit' | 'blur' // default is submit
39+
})
40+
]
41+
})
42+
class AppModule {}
43+
```
44+
45+
### Add directives to form elements
46+
47+
add `ngxFormValidator` directive to form element and add `ngxFormSubmit` directive handle submit event.
48+
49+
```html
50+
<form name="exampleForm" novalidate [ngxFormValidator]="validatorConfig">
51+
<div class="form-group">
52+
<label for="email1">Email address</label>
53+
<input type="email" email class="form-control" name="email" id="email1"
54+
[(ngModel)]="model.email" required placeholder="Enter email" />
55+
</div>
56+
<button type="button" (ngxFormSubmit)="submit()" class="btn btn-primary">Submit</button>
57+
<form>
58+
```
59+
60+
```ts
61+
// .ts
62+
validatorConfig: NgxValidatorConfig = {
63+
validationMessages: {
64+
username: {
65+
required: '用户名不能为空',
66+
pattern: '用户名格式不正确,以字母,数字,下划线组成,首字母不能为数字,必须是2-20个字符',
67+
ngxUniqueCheck: '输入的用户名已经存在,请重新输入'
68+
}
69+
},
70+
validateOn: 'blur' | 'submit'
71+
};
72+
73+
submit() {
74+
// handle submit event
75+
}
76+
```
77+
78+
## Global configuration
79+
80+
Global configuration can be set by `NgxValidatorModule.forRoot(config)`, or by injecting `NgxValidatorLoader` service at runtime.
81+
82+
| Name | Type | Description |
83+
| ------------ | ------------------- | --------------- |
84+
| validationMessages | {[controlName: string]: {[validatorErrorKey: string]: string}} | validation Rules |
85+
| validationFeedbackStrategy | ValidationFeedbackStrategy | validation feedback strategy which contains error show and hide |
86+
| globalValidationMessages | {[validatorErrorKey: string]: string} | validator default validation rules |
87+
| validateOn | 'submit' \| 'blur' | validate trigger |
88+
89+
<br >
90+
91+
Default `globalValidationMessages` rules as below:
92+
93+
```
94+
{
95+
required: '该选项不能为空',
96+
maxlength: '该选项输入值长度不能大于{requiredLength}',
97+
minlength: '该选项输入值长度不能小于{requiredLength}',
98+
ngxUniqueCheck: '输入值已经存在,请重新输入',
99+
email: '输入邮件的格式不正确',
100+
repeat: '两次输入不一致',
101+
pattern: '该选项输入格式不正确',
102+
number: '必须输入数字',
103+
url: '输入URL格式不正确',
104+
max: '该选项输入值不能大于{max}',
105+
min: '该选项输入值不能小于{min}'
106+
};
107+
```
108+
109+
The priority of ngx-form's `validationMessages` config is greater than `validationMessages`,
110+
it will use `globalValidationMessages` when an element doesn't match form config `validationMessages` or global config validationMessages
111+
112+
## Extensions
113+
114+
Get `formValidator` directive by `<form #formValidator="ngxFormValidator">` or `ViewChild`
115+
116+
1. `formValidator.validator.validateControl(name: string)` validate an control individually
117+
2. `formValidator.validator.markControlAsError(name: string, errorMessage: string)` show error by server's error code for an control
118+
119+
120+
## Custom Feedback Strategy
121+
122+
```ts
123+
const CUSTOM_INVALID_CLASS = 'custom-invalid';
124+
const CUSTOM_INVALID_FEEDBACK_CLASS = 'custom-invalid-feedback';
125+
126+
export class CustomValidationFeedbackStrategy implements ValidationFeedbackStrategy {
127+
showError(element: HTMLElement, errorMessages: string[]): void {
128+
element.classList.add(CUSTOM_INVALID_CLASS);
129+
// add element show error message
130+
}
131+
132+
removeError(element: HTMLElement): void {
133+
element.classList.remove(CUSTOM_INVALID_CLASS);
134+
// remove element error message
135+
}
136+
}
137+
138+
NgxValidatorModule.forRoot({
139+
...
140+
validationFeedbackStrategy: new CustomValidationFeedbackStrategy(),
141+
...
142+
})
143+
```

0 commit comments

Comments
 (0)