Property Attributes

Exploring Property Attributes in Fortis: A Powerful Data-Passing Mechanism

In Fortis, property attributes offer a robust way to pass data to elements, whether you're working with Fortis components or native HTML elements. This feature provides greater flexibility compared to traditional HTML attributes, as it allows you to pass various data types, including arrays, files, and custom objects. In this article, we'll delve into property attributes in Fortis, how to use them, and the benefits they bring to your web development projects.

Understanding Property Attributes

Property attributes are a unique aspect of Fortis, providing a means to pass data to elements without relying on standard HTML attributes. This is especially valuable when you need to pass complex or non-string data types. To define a property attribute, you use the dollar sign ($) as a prefix, making it clear that this is not a standard attribute but a property meant for data manipulation.

Using Property Attributes in Fortis Components

Let's consider a practical example with a Fortis component, a file manager, that needs to accept an array of files. To achieve this, you can define a property attribute as follows:

class FileManager extends Component({
    maxCount: Required.number
}) {
    // Property to hold files
    files: File[] = [];
 
    render() {
        // ...
    }
}

Here, we define the files property as an array of File objects. This property allows us to store and manipulate files within the component.

Now, when you want to use the FileManager component and pass a file to it, you can do so using a property attribute:

<FileManager $files={new File([blob], "a.svg")} />

The $files property attribute indicates that this is not a standard HTML attribute but a property meant to hold file data. It seamlessly integrates with the FileManager component.

Property Attributes for Native Elements

Property attributes can also be used with native HTML elements. Let's say you want to set the innerHTML of a <span> element to display "Hello!" in a unique way. You can use a property attribute to achieve this:

<span $innerHTML="Hello!" />

Here, the $innerHTML property attribute allows you to set the inner content of the <span> element to "Hello!".

Benefits of Property Attributes

Property attributes offer several advantages in Fortis:

  1. Data Flexibility: You can pass various data types, including arrays, files, and custom objects, without constraints imposed by standard HTML attributes.

  2. Clarity and Semantics: The use of the $ prefix makes it clear that you're working with a property attribute. This enhances code readability and prevents potential conflicts with standard attributes.

  3. Consistency: Property attributes maintain consistency in your codebase, as they are applicable to both Fortis components and native HTML elements. This unification simplifies data handling across your project.

TypeScript

TypeScript seamlessly integrates with Fortis property attributes, allowing for strong typing and improved code quality. TypeScript can accurately infer and enforce the types of data passed via property attributes, enhancing the development experience and reducing the likelihood of runtime errors.

Conclusion

Property attributes in Fortis provide a versatile way to pass data to elements, empowering you to work with complex data types and enriching your web development projects. By using the $ prefix, property attributes distinguish themselves from standard HTML attributes, ensuring clarity and avoiding naming conflicts. Whether you're working with Fortis components or native HTML elements, property attributes offer a powerful data-passing mechanism to meet your project's specific requirements.