1.0.0 • Published 1 year ago

@sgng/dyn-forms v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

@sgng/dyn-forms

'@sgng/dyn-forms' is an npm package tailored for Angular projects. It offers Angular components, an interface, and an enum to simplify the creation of forms encompassing various input types like text, number, and select. These input types seamlessly integrate with Angular's data-binding functionality. The package dynamically renders forms in the browser by accepting input field metadata or an array thereof.

Table of contents

Supported Input Fields

Input TypeVersionDesctiptions
Checkbox1.0.0Checkbox buttons let a user select one or more options of a limited number of choices.
Color1.0.0Used for input fields that should contain a color. Depending on browser support, a color picker can show up in the input field.
DataList1.0.0Provide a list of pre-defined options for an input element.
Date1.0.0Used for input fields that should contain a date. Depending on browser support, a date picker can show up in the input field.
Datetime1.0.0Specifies a date and time input field, with no time zone. Depending on browser support, a date and time picker can show up in the input field.
Email1.0.0Used for input fields that should contain an e-mail address. Depending on browser support, the e-mail address can be automatically validated when submitted. Some smartphones recognize the email type, and add ".com" to the keyboard to match email input.
File1.0.0Defines a file-select field and a "Browse" button for file uploads. Don't support two-way data-binding functionality.
Month1.0.0Allows the user to select a month and year. Depending on browser support, a month picker can show up in the input field.
Number1.0.0Defines a numeric input field.
Password1.0.0Defines a password input field.
Radio1.0.0Radio buttons let a user select only one of a limited number of choices.
Range1.0.0Defines a control for entering a number whose exact value is not important (like a slider control).
Select1.0.0Define the available options in the drop-down list.
Tel1.0.0Used for input fields that should contain a telephone number.
Text1.0.0Defines a single-line text input field.
Textarea1.0.0Defines a multi-line text input control
Time1.0.0Allows the user to select a time (no time zone). Depending on browser support, a time picker can show up in the input field.
URL1.0.0Used for input fields that should contain a URL address.
Week1.0.0Allows the user to select a week and year. Depending on browser support, a week picker can show up in the input field.

Properties

(*) marked properties are required.

Common

The below properties are common for all the input types.

NameVersionData TypeDefault Value
id*1.0.0string
type*1.0.0InputType
label1.0.0string'' (empty string)

Specific for each input types

Checkbox

NameVersionData TypeDefault Value
options*1.0.0string[][] (empty string array)
value1.0.0string[]undefined

Color

NameVersionData TypeDefault Value
value1.0.0stringundefined

DataList

NameVersionData TypeDefault Value
options*1.0.0string[][] (empty string array)
value1.0.0stringundefined

Date

NameVersionData TypeDefault Value
value1.0.0Dateundefined

Datetime

NameVersionData TypeDefault Value
value1.0.0Dateundefined

Email

NameVersionData TypeDefault Value
value1.0.0stringundefined

File

NameVersionData TypeDefault Value
value1.0.0Fileundefined

Month

NameVersionData TypeDefault Value
value1.0.0stringundefined

Number

NameVersionData TypeDefault Value
value1.0.0numberundefined

Password

NameVersionData TypeDefault Value
value1.0.0stringundefined

Radio

NameVersionData TypeDefault Value
options*1.0.0string[][] (empty string array)
value1.0.0stringundefined

Range

NameVersionData TypeDefault Value
min*1.0.0number
max*1.0.0number
value1.0.0numberundefined
step1.0.0number1

Select

NameVersionData TypeDefault Value
options*1.0.0string[][] (empty string array)
value1.0.0stringundefined

Tel

NameVersionData TypeDefault Value
value1.0.0stringundefined

Text

NameVersionData TypeDefault Value
value1.0.0stringundefined

Textarea

NameVersionData TypeDefault Value
value1.0.0stringundefined

Time

NameVersionData TypeDefault Value
value1.0.0stringundefined

URL

NameVersionData TypeDefault Value
value1.0.0stringundefined

Week

NameVersionData TypeDefault Value
value1.0.0stringundefined

Installation

With npm:

npm install @sgng/dyn-forms

Setup

Import the DynFormsModule from the @sgng/dyn-forms package into the module file.

app.module.ts

...
import { DynFormsModule } from  '@sgng/dyn-forms';
...

@NgModule({
	declarations: [...],
	imports: [
		...
		DynFormsModule,
		...
	],
	providers: [...],
	bootstrap: [...]
})
export  class  AppModule { }

Usage/Examples

Import Statement

Imports FormItem and InputType from the @sgng/dyn-forms library into the component file.

app.component.ts

import { FormItem, InputType } from  "@sgng/dyn-forms";

Single Input Field

  1. Define a Property:

    • In your component file, define a property named inputField with the type FormItem<InputType.TEXT>. This indicates it is a FormItem with a specific InputType such as TEXT.
  2. Initialize the Property:

    • Initialize the inputField property as an object with the relevant properties. Refer to the Properties section for the common and specific properties for each input type:
      • Set the type property to match the type specified for inputField, e.g., type: InputType.TEXT.
      • Add other required and optional properties with appropriate values as detailed in the Properties section.

app.component.ts

export class AppComponent {
	...
	inputField: FormItem<InputType.TEXT> = {
		type: InputType.TEXT,
		id: 'formItem'
	};
	...
}
  1. Use the Custom Component:
    • In your HTML file, use the <sgng-form-item> custom component provided by the @sgng/dyn-forms library.
    • Bind the inputField property of your component class to the item input property of the <sgng-form-item> component using property binding.

app.component.html

<sgng-form-item [item]="inputField"></sgng-form-item>

Input Fields Array

  1. Define a Property:

    • In your component file, define a property named inputFields with the type FormItem<InputType>[]. This indicates it is an array of FormItem objects, each with a type from the InputType enumeration.
  2. Initialize the Property:

    • Initialize the inputFields property as an array containing multiple form item objects with their relevant properties:
      • Set the type property of each form item object to an item from the InputType enumeration, e.g., type: InputType.TEXT or type: InputType.NUMBER.
      • Add other required and optional properties with appropriate values as detailed in the Properties section.

app.component.ts

export class AppComponent {
	...
	inputFields:  FormItem<InputType>[] = [{
		type:  InputType.TEXT,
		id:  'textInput'
	}, {
		type:  InputType.NUMBER,
		id:  'numberInput'
	}];
	...
}
  1. Use the Custom Component:
    • In your HTML file, use the <sgng-form-item> custom component provided by the @sgng/dyn-forms library.
    • Bind the inputFields property of your component class to the items input property of the <sgng-form-item> component using property binding.

app.component.html

<sgng-form-item [item]="inputField"></sgng-form-item>

Managing Input Field Values

Utilize the value property to retrieve or assign values to the input field(s).

Bug Reports or Feature Requests

Help us provide you with better service. Click here to submit a Bug Report or Feature Request form.

Developers

Subhendu Ghosh
Subhendu Ghosh

Support

If you find this project useful and would like to support its development, please consider making a small donation. Your contributions help maintain and improve the project for everyone.

Donate

Thank you for your support!

1.0.0

1 year ago