Docs: Attributes - rollthecloudinc/quell GitHub Wiki

Documentation for Angular Attributes Library


Table of Contents

  1. Introduction
  2. Installation
  3. Overview and Features
  4. Key Components
    • AttributesModule
    • Components (AttributesBuilderComponent, AttributeWidgetComponent)
  5. Core Concepts
    • Widgets
    • Attribute Management
    • Serialization/Deserialization
  6. Services
    • WidgetPluginManager
    • AttributeMatcherService
  7. Usage
    • Dynamic Form Generation
    • Creating Custom Widgets
  8. API Reference
  9. Examples
  10. Testing

1. Introduction

The Angular Attributes Library is designed to help developers manage dynamic form attributes in Angular applications. It provides serialization, widget plugins, and a modular system for defining and rendering custom forms and attributes. The library's flexibility allows developers to create forms dynamically, customize widgets, and define attributes without needing static components.


2. Installation

Install the library using npm:

npm install @your-org/attributes

You may also need other dependencies like @rollthecloudinc/material and @rollthecloudinc/plugin, which are used for widget rendering and plugin management.


3. Overview and Features

The AttributesModule is the core module in this library that enables the following features:

  • Dynamic Form Creation: Create forms dynamically based on attribute data.
  • Custom Widgets: Render attribute values using widgets like text boxes, dropdowns, and sliders.
  • Plugin-based Widget Management: Extend the library by registering widget plugins using a modular approach.
  • Attribute Serialization and Deserialization: Serialize object data into attributes and deserialize attributes back into object data.
  • Reactive Form Integration: Leverage Angular Forms for validation and state management.

4. Key Components

4.1 AttributesModule

The AttributesModule orchestrates all components and services in the library. It imports supporting modules like ReactiveFormsModule and MaterialModule to enable dynamic forms and material design.

@NgModule({
  imports: [
    CommonModule,
    ReactiveFormsModule,
    MaterialModule,
    HttpClientJsonpModule,
  ],
  declarations: [
    AttributesBuilderComponent,
    AttributeWidgetComponent,
    TextWidgetComponent,
    MinMaxWidgetComponent,
    AttributePipe,
  ],
  exports: [
    AttributesBuilderComponent,
    AttributeWidgetComponent,
    AttributePipe,
  ],
  providers: [
    {
      provide: ATTRIBUTE_WIDGET,
      useFactory: attrFactories.textFactory,
      multi: true,
    },
    {
      provide: ATTRIBUTE_WIDGET,
      useFactory: attrFactories.minmaxFactory,
      multi: true,
    },
  ],
})
export class AttributesModule {}

4.2 Components

AttributesBuilderComponent
The AttributesBuilderComponent provides functionality for dynamically creating attribute-based forms. It integrates widgets as plugins and allows nesting of attributes.

AttributeWidgetComponent
The AttributeWidgetComponent dynamically renders widgets based on the provided attribute schema and widget plugin.


5. Core Concepts

5.1 Widgets

Widgets are reusable form components that dynamically render attributes based on schema definitions. The library comes with the following predefined widgets:

  • TextWidgetComponent: A single-line text input.
  • MinMaxWidgetComponent: A numeric range widget (min and max values).

You can also define custom widgets and register them as plugins for dynamic rendering.

5.2 Attribute Management

Attributes are structured data models defined in attributes.models.ts. The key models include:

  • Attribute: Represents the schema definition of an attribute.
  • AttributeWidget: Represents a plugin-based widget along with its associated schema.
  • AttributeValue: Represents the value of an attribute at runtime.

6. Services

6.1 WidgetPluginManager

The WidgetPluginManager is a plugin-based service for managing custom widgets. It extends the base PluginManager class from @rollthecloudinc/plugin and provides methods to define and fetch widgets dynamically.

Example:

pluginManager.register(new AttributeWidget<string>({
  id: 'custom-widget',
  name: 'Custom Widget',
  title: 'My Custom Widget',
  component: CustomWidgetComponent,
  schema: customSchema,
}));

6.2 AttributeMatcherService

This service handles the logic for matching attribute values and computing derived values. Use it to process complex attribute relationships and nested attributes.


7. Usage

7.1 Dynamic Form Generation

To create a dynamic form, pass an array of Attribute objects to the AttributesBuilderComponent.

<classifieds-ui-attributes-builder
  [attributes]="attributes"
  [appearance]="'material'"
>
</classifieds-ui-attributes-builder>
attributes: Attribute[] = [
  {
    name: 'Username',
    type: AttributeTypes.Text,
    label: 'Enter your username',
    required: true,
    widget: 'text',
    options: {},
    attributes: [],
  },
  {
    name: 'Age',
    type: AttributeTypes.Number,
    label: 'Enter your age',
    required: true,
    widget: 'minmax',
    options: { min: 18, max: 99 },
    attributes: [],
  },
];

7.2 Creating Custom Widgets

You can create custom widgets by extending the AttributeWidget model and defining a new Angular component.

@Component({
  selector: 'app-custom-widget',
  templateUrl: './custom-widget.component.html',
})
export class CustomWidgetComponent {
  @Input() attribute: Attribute;
}

Register your widget as a plugin:

const customWidgetFactory = () => {
  const schema = new Attribute({
    name: 'custom',
    type: AttributeTypes.Complex,
    label: 'Custom Widget',
    required: true,
    widget: 'custom',
    options: {},
    attributes: [],
  });
  return new AttributeWidget<string>({
    id: 'custom-widget',
    name: 'custom-widget',
    title: 'Custom Widget',
    component: CustomWidgetComponent,
    schema,
  });
};

8. API Reference

Models

  • AttributeWidget: Represents a widget plugin and schema.
  • Attribute: Schema definition for an attribute.
  • AttributeValue: Runtime representation of attribute values.

Services

  • WidgetPluginManager: Plugin-based widget manager.
  • AttributeSerializerService: Serializes and deserializes attributes.
  • AttributeMatcherService: Matches and computes derived values.

Components

  • AttributesBuilderComponent: Builds dynamic attribute forms.
  • AttributeWidgetComponent: Dynamically renders a widget.

9. Examples

Example 1: Simple Form Generation

<classifieds-ui-attributes-builder
  [attributes]="attributes"
></classifieds-ui-attributes-builder>

Example 2: Using a Custom Widget

<classifieds-ui-attribute-widget
  [widget]="customWidgetPlugin$"
  [attribute]="attribute"
></classifieds-ui-attribute-widget>

10. Testing

Unit tests are provided for all components and services. Run tests using the command:

npm run test

Example Spec Test (TextWidgetComponent):

describe('TextWidgetComponent', () => {
  let component: TextWidgetComponent;
  let fixture: ComponentFixture<TextWidgetComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [TextWidgetComponent],
    }).compileComponents();

    fixture = TestBed.createComponent(TextWidgetComponent);
    component = fixture.componentInstance;
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

Conclusion

The Angular Attributes Library provides powerful tools for dynamic form creation, attribute serialization, and custom widget management. Developers can extend the library to handle complex forms and modular data attributes, improving reusability and maintainability in Angular applications.

Let us know if you need further assistance!

⚠️ **GitHub.com Fallback** ⚠️