DioxusLabs / DioxusLabs/dioxus

Provide type checking support for HTML attribute values in `rsx` macro

Open
#2,398 2 comments 0 reactions 0 assignees View on GitHub
html
Dominant language
Rust
Stars
39.1k
Forks
1.9k
Avg merge
4d 10h
Merged PRs (30d)
4

Description

## Feature Request

Since Rust is a strongly typed language, it seems sensible to check types in HTML attribute values as well. Currently, `rsx` macro allows the following usage:

```rust
pub fn component() -> Element {
rsx! {
input {
r#type: "text",
maxlength: "10",
}
}
}
```
However, the `maxlength` attribute should actually be an integer:

```rust
pub fn component() -> Element {
rsx! {
input {
r#type: "text",
maxlength: 10,
}
}
}
```

## Implement Suggestion

Instead of forcing developers to use strongly typed HTML, we can provide two options:

1. **Comprehensive Type Definitions:** Attributes should have type definitions that can include primitive data types, enums, and custom structs, depending on the attribute's expected values.
2. **String Literal Validation:** When attributes are specified using string literals, the `rsx` macro should validate these strings against the expected data types.

**Examples;**

```rs
pub fn component() -> Element {
rsx! {
input {
r#type: InputType::Email,
maxlength: 10
}
input {
r#type: "email"
maxlength: "10"
}
input {
r#type: "emaaill" // `rsx` should show an error: invalid input type
maxlength: "10"
}
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.