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
31 changes: 31 additions & 0 deletions cot-core/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,37 @@ impl HtmlTag {
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();
/// ```
Comment on lines +192 to +199
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
/// 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();
/// ```
/// 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?

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);
Copy link
Member

Choose a reason for hiding this comment

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

As I look in the MDN docs and HTML standard, option only needs to have the content in the value field, not s a children.

Suggested change
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