Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion cot-core/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl HtmlTag {
}
}

/// Creates a new `HtmlTag` instance for an input element.
/// Creates a new `HtmlTag` instance for an [input] element.
///
/// # Examples
///
Expand All @@ -182,13 +182,48 @@ impl HtmlTag {
/// let input = HtmlTag::input("text");
/// assert_eq!(input.render().as_str(), "<input type=\"text\"/>");
/// ```
///
/// [input]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input
#[must_use]
pub fn input(input_type: &str) -> Self {
let mut input = Self::new("input");
input.attr("type", input_type);
input
}

/// Creates a new `HtmlTag` instance for a [datalist] element.
///
/// # Examples
/// ```
/// use cot::html::HtmlTag;
/// let data_list = HtmlTag::data_list(vec!["Option 1", "Option 2"], "my-datalist");
/// let rendered = data_list.render();
/// ```
///
/// [datalist]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/datalist
#[must_use]
pub fn data_list<I, S>(list: I, id: &str) -> Self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should force providing id in the constructor, maybe getting rid of it and expecting users to do .attr("id", id") is enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm, The thing about the datalist element is that the id attr is tightly coupled with the list attribute in the input element (i.e the list attr in the input should match the id attr in the datalist) so if the user forgets to call .attr("id", id) it wont work. I had that as a parameter because it's required for correctness

where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut data_list = Self::new("datalist");
data_list.attr("id", id);

let mut options: Vec<HtmlNode> = Vec::new();

for item in list {
let l = item.as_ref();
let mut option = HtmlTag::new("option");
option.attr("value", l);
// option.push_str(l);
options.push(HtmlNode::Tag(option));
}

data_list.children = options;
data_list
}

/// Adds an attribute to the HTML tag.
///
/// # Safety
Expand Down
Loading
Loading