New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@trufla/ngx-tru-forms

Package Overview
Dependencies
Maintainers
2
Versions
132
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@trufla/ngx-tru-forms

Angular 5+ module for generating forms from JSON schema. Refer to documentation for structure of JSON Schema [PDF](https://spacetelescope.github.io/understanding-json-schema/UnderstandingJSONSchema.pdf)/[HTML](https://spacetelescope.github.io/understandin

  • 0.1.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
41
increased by78.26%
Maintainers
2
Weekly downloads
 
Created
Source

Tru Forms

Angular 5+ module for generating forms from JSON schema. Refer to documentation for structure of JSON Schema PDF/HTML. This component utilizes Reactive Forms for most of its functionality.

Table of Contents

Installation

npm install -g @trufla/ngx-tru-forms

Usage

In module add following:

import { JsonFormModule } from '@trufla/ngx-tru-forms';

@NgModule({
  imports: [
    JsonFormModule
  ]
})

In component add following:

<jf-form></jf-form>

Options

KeyDescriptionRequired
[schema]JSON Schema objectYes
[data]JSON Schema default values
[style]Extra classes and style overrides
[submit]Text label for submit button
[cancel]Text label for cancel button
[outerClass]Wrapper class for the form component
[submitClass]Class for submit button
[cancelClass]Class for cancel button
[isWorking]Toggle form state if using async data process
(handleSubmit)Watch for form submission. Return JSON Schema response data
(handleChange)Watch for form changes
(handleCancel)Watch for cancel click

Example

const schema = {
	"type": "object",
	"properties": {
		"first_name": {
			"type": "string"
		},
		"last_name": {
    	"type": "string"
    }
	},
	"required": ["make"]
};

const data = {
  "first_name": "Test",
  "last_name": "Me"
}

const onFormSubmit = (form) => console.log(form);
<jf-form 
  [schema]="schema" 
  [data]="data"
  (handleChange)="onFormSubmit($event)"
></jf-form> 

Quick Reference

type: string, number, object, array, boolean
format (optional): date, photo, textarea description (optional): hover text to describe purpose of field

Extending

This module allows for extension via injectors.

constructor(
  jfDefaultsService: JsonFormDefaultsService,
  jfFieldsService: JsonFormFieldsService
  jfValidatorsService: JsonFormValidatorsService
)
Defaults

Extend values in default tag.

this.jfDefaultsService.register('now', () => new Date());
Validations

Add JSON validator.

const ValidatorJSON = (control: AbstractControl) => {
  try {
    JSON.parse(control.value);
    return null;
  } catch (err) {
    return { customError: err.message };
  }
};
this.jfValidatorsService.register('field_name', ValidatorJSON);

field_name has to be valid field including any nesting (as it is display on data and without properties)

const schema = {
	"type": "object",
	"properties": {
		"first_name": {
			"type": "string"
		},
		"last_name": {
    	"type": "string",
    	"prefix": {
    	  "type": "object",
        "properties": {
          "custom": {
            "type": "string"
          }
        }
    	}
    }
	},
	"required": ["make"]
};

Fields would be first_name, last_name, last_name.prefix.custom.

Fields

Add new field type. Create a component that extends CommonComponent. Add the following as a starting template (or copy from string field).

import { CommonComponent } from '@trufla/ngx-tru-forms';

@Component({
  template: `
    <label [ngClass]="['jf-label', schema.key, (isRequired() ? 'required' : '')]">
      {{title()}}<sup *ngIf="isRequired()">*</sup>
    </label>
    <input
      class="form-control"
      [name]="schema.key"
      [attr.type]="type()"
      [formControl]="control"
      [(colorPicker)]="color"
      [style.background]="color"
      [style.color]="color"
      [style.width]="'40px'"
      (colorPickerChange)="handleColorPickerChange($event)"
    />
  `
})
export class CustomComponent extends CommonComponent {
  color: '#0000ff';

  handleColorPickerChange(val) {
    this.control.setValue(val);
  }
}

Add it to your module:

entryComponents: [
  ColourPickerComponent
]

Register it inside your component:

this.jfFieldsService.register('new_format', CustomComponent);

Now objects of format new_format will show the CustomComponent.

Styling

We prefer csswizardry-grids to align and order fields. Form, groups and labels are assigned classes which can be utilized globally or per form. Certain forms fields can be assigned classes on top of current defaults.

{
  first_name: {
    default: 'one-half grid__item'
  },
  last_name: {
    default: 'one-half grid__item'
  }
}

Keywords

FAQs

Package last updated on 13 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc