|
1 |
| -# AngularDynamicForms |
| 1 | + |
2 | 2 |
|
3 |
| -This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 13.3.2. |
| 3 | +# Angular Dynamic Forms |
4 | 4 |
|
5 |
| -## Development server |
| 5 | +Angular component that allows the creation of dynamic forms. You can use this component in situations where you get the configuration for the form from an external API or if you just hate HTML 😀 |
6 | 6 |
|
7 |
| -Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The application will automatically reload if you change any of the source files. |
| 7 | +## List of features |
8 | 8 |
|
9 |
| -## Code scaffolding |
| 9 | +> `UI Style` — for UI i used [PrimeNG](https://www.primefaces.org/primeng/#/) but you can use any Angular UI Library. |
10 | 10 |
|
11 |
| -Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`. |
| 11 | +- Dynamically created form only from JSON configuration |
| 12 | +- Full gird layout for forms |
| 13 | +- Dummy fields without UI input fields |
| 14 | +- You can group fields in same column |
| 15 | +- Responsive forms |
| 16 | +- 👇 Easy to [add more supported fields](#add-more-fields) |
12 | 17 |
|
13 |
| -## Build |
14 | 18 |
|
15 |
| -Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. |
| 19 | +## Installation |
16 | 20 |
|
17 |
| -## Running unit tests |
| 21 | +### Install via NPM |
18 | 22 |
|
19 |
| -Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). |
| 23 | +Clone repo and run: |
20 | 24 |
|
21 |
| -## Running end-to-end tests |
| 25 | +```shell |
| 26 | +npm install |
| 27 | +``` |
22 | 28 |
|
23 |
| -Run `ng e2e` to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities. |
| 29 | +```shell |
| 30 | +ng serve -o |
| 31 | +``` |
24 | 32 |
|
25 |
| -## Further help |
26 | 33 |
|
27 |
| -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. |
| 34 | +## Usage |
| 35 | + |
| 36 | +There are two ways you can use dynamic forms. The first way is if you have no additional controls other than those in the configuration. |
| 37 | + |
| 38 | +#### Example 1 |
| 39 | + |
| 40 | +```typescript |
| 41 | + constructor( |
| 42 | + public formFactory: FormBuilderService |
| 43 | + ) {} |
| 44 | + |
| 45 | + ngOnInit(): void { |
| 46 | + /** |
| 47 | + * Call formFactory service and pass form fields |
| 48 | + * JSON configuration to create new form group |
| 49 | + */ |
| 50 | + this.exampleForm = this.formFactory.createForm(this.exampleFields); |
| 51 | + /** |
| 52 | + * Example how to add additional formControl |
| 53 | + * to form ifyou need |
| 54 | + */ |
| 55 | + this.exampleForm.addControl('id', new FormControl(1, Validators.required)); |
| 56 | + } |
| 57 | +``` |
| 58 | + |
| 59 | +```HTML |
| 60 | +<app-form-factory |
| 61 | + [form]="exampleForm" |
| 62 | + [fields]="exampleFields" |
| 63 | +></app-form-factory> |
| 64 | +``` |
| 65 | + |
| 66 | +#### Example 2 |
| 67 | + |
| 68 | +If you want to insert dynamic fields into one control and add the rest of the field via the form builder, you can do so as follows. |
| 69 | + |
| 70 | +```typescript |
| 71 | + constructor( |
| 72 | + private fb: FormBuilder, |
| 73 | + public formFactory: FormBuilderService |
| 74 | + ) {} |
| 75 | + |
| 76 | + ngOnInit(): void { |
| 77 | + this.exampleForm = this.fb.group({ |
| 78 | + dynamic: this.formFactory.createForm(this.exampleFields), |
| 79 | + id: ['1', Validators.required], |
| 80 | + }); |
| 81 | + } |
| 82 | +``` |
| 83 | + |
| 84 | +```HTML |
| 85 | +<app-form-factory |
| 86 | + [form]="formFactory.getFormGroup(exampleForm,'dynamic')" |
| 87 | + [fields]="exampleFields" |
| 88 | +></app-form-factory> |
| 89 | +``` |
| 90 | + |
| 91 | +## Available configuration |
| 92 | + |
| 93 | +### Enabling / disabling services |
| 94 | + |
| 95 | +Embed Tool supports some services by default (see above). You can specify services you would like to use: |
| 96 | + |
| 97 | +```javascript |
| 98 | +var editor = EditorJS({ |
| 99 | + ... |
| 100 | + |
| 101 | + tools: { |
| 102 | + ... |
| 103 | + embed: { |
| 104 | + class: Embed, |
| 105 | + config: { |
| 106 | + services: { |
| 107 | + youtube: true, |
| 108 | + coub: true |
| 109 | + } |
| 110 | + } |
| 111 | + }, |
| 112 | + }, |
| 113 | + |
| 114 | + ... |
| 115 | +}); |
| 116 | +``` |
| 117 | + |
| 118 | +> Note that if you pass services you want to use like in the example above, others will not be enabled. |
| 119 | +
|
| 120 | +### Add more services |
| 121 | + |
| 122 | +You can provide your own services using simple configuration. |
| 123 | + |
| 124 | +First, you should create a Service configuration object. It contains following fields: |
| 125 | + |
| 126 | +| Field | Type | Description | |
| 127 | +| ---------- | ---------- | ----------- | |
| 128 | +| `regex` | `RegExp` | Pattern of pasted URLs. You should use regexp groups to extract resource id |
| 129 | +| `embedUrl` | `string` | Url of resource\`s embed page. Use `<%= remote_id %>` to substitute resource identifier |
| 130 | +| `html` | `string` | HTML code of iframe with embedded content. `embedUrl` will be set as iframe `src` |
| 131 | +| `height` | `number` | _Optional_. Height of inserted iframe |
| 132 | +| `width` | `number` | _Optional_. Width of inserted iframe |
| 133 | +| `id` | `Function` | _Optional_. If your id is complex you can provide function to make the id from extraced regexp groups |
| 134 | + |
| 135 | +Example: |
| 136 | + |
| 137 | +```javascript |
| 138 | +{ |
| 139 | + regex: /https?:\/\/codepen.io\/([^\/\?\&]*)\/pen\/([^\/\?\&]*)/, |
| 140 | + embedUrl: 'https://codepen.io/<%= remote_id %>?height=300&theme-id=0&default-tab=css,result&embed-version=2', |
| 141 | + html: "<iframe height='300' scrolling='no' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'></iframe>", |
| 142 | + height: 300, |
| 143 | + width: 600, |
| 144 | + id: (groups) => groups.join('/embed/') |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +When you create a Service configuration object, you can provide it with Tool\`s configuration: |
| 149 | + |
| 150 | +```javascript |
| 151 | +var editor = EditorJS({ |
| 152 | + ... |
| 153 | + |
| 154 | + tools: { |
| 155 | + ... |
| 156 | + embed: { |
| 157 | + class: Embed, |
| 158 | + config: { |
| 159 | + services: { |
| 160 | + youtube: true, |
| 161 | + coub: true, |
| 162 | + codepen: { |
| 163 | + regex: /https?:\/\/codepen.io\/([^\/\?\&]*)\/pen\/([^\/\?\&]*)/, |
| 164 | + embedUrl: 'https://codepen.io/<%= remote_id %>?height=300&theme-id=0&default-tab=css,result&embed-version=2', |
| 165 | + html: "<iframe height='300' scrolling='no' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'></iframe>", |
| 166 | + height: 300, |
| 167 | + width: 600, |
| 168 | + id: (groups) => groups.join('/embed/') |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + }, |
| 173 | + }, |
| 174 | + |
| 175 | + ... |
| 176 | +}); |
| 177 | +``` |
| 178 | + |
| 179 | +#### Inline Toolbar |
| 180 | +Editor.js provides useful inline toolbar. You can allow it\`s usage in the Embed Tool caption by providing `inlineToolbar: true`. |
| 181 | + |
| 182 | +```javascript |
| 183 | +var editor = EditorJS({ |
| 184 | + ... |
| 185 | + |
| 186 | + tools: { |
| 187 | + ... |
| 188 | + embed: { |
| 189 | + class: Embed, |
| 190 | + inlineToolbar: true |
| 191 | + }, |
| 192 | + }, |
| 193 | + |
| 194 | + ... |
| 195 | +}); |
| 196 | +``` |
| 197 | + |
| 198 | +## Output data |
| 199 | + |
| 200 | +| Field | Type | Description |
| 201 | +| ------- | -------- | ----------- |
| 202 | +| service | `string` | service unique name |
| 203 | +| source | `string` | source URL |
| 204 | +| embed | `string` | URL for source embed page |
| 205 | +| width | `number` | embedded content width |
| 206 | +| height | `number` | embedded content height |
| 207 | +| caption | `string` | content caption |
| 208 | + |
| 209 | + |
| 210 | +```json |
| 211 | +{ |
| 212 | + "type" : "embed", |
| 213 | + "data" : { |
| 214 | + "service" : "coub", |
| 215 | + "source" : "https://coub.com/view/1czcdf", |
| 216 | + "embed" : "https://coub.com/embed/1czcdf", |
| 217 | + "width" : 580, |
| 218 | + "height" : 320, |
| 219 | + "caption" : "My Life" |
| 220 | + } |
| 221 | +} |
| 222 | +``` |
| 223 | + |
| 224 | +# About CodeX |
| 225 | + |
| 226 | +<img align="right" width="120" height="120" src="https://codex.so/public/app/img/codex-logo.svg" hspace="50"> |
| 227 | + |
| 228 | +CodeX is a team of digital specialists around the world interested in building high-quality open source products on a global market. We are [open](https://codex.so/join) for young people who want to constantly improve their skills and grow professionally with experiments in cutting-edge technologies. |
| 229 | + |
| 230 | +| 🌐 | Join 👋 | Twitter | Instagram | |
| 231 | +| -- | -- | -- | -- | |
| 232 | +| [codex.so](https://codex.so) | [codex.so/join](https://codex.so/join) |[@codex_team](http://twitter.com/codex_team) | [@codex_team](http://instagram.com/codex_team) | |
0 commit comments