1
//! Data attribute definitions for autocomplete components.
2
//!
3
//! This module provides a single source of truth for HTML data attribute names
4
//! used by autocomplete components. Both the WASM frontend (reader) and the web
5
//! server templates (writer) use these definitions to ensure consistency.
6

            
7
/// Attribute names for autocomplete components.
8
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9
pub enum AutocompleteAttr {
10
    /// The type of autocomplete (e.g., "account", "commodity", "tag-name", "tag-value")
11
    Type,
12
    /// URL to fetch suggestions from
13
    FetchUrl,
14
    /// HTTP method for fetching (GET or POST, default: GET)
15
    FetchMethod,
16
    /// ID of the visible display input field
17
    DisplayInput,
18
    /// ID of the hidden value input field
19
    HiddenInput,
20
    /// ID of the currency/secondary input field (optional)
21
    CurrencyInput,
22
    /// ID of the dependency input (for dependent autocompletes like tag-value)
23
    DependsOn,
24
    /// Marker attribute indicating the component is initialized
25
    Initialized,
26
    /// Index for suggestion items in the dropdown
27
    Index,
28
}
29

            
30
impl AutocompleteAttr {
31
    /// Returns the HTML attribute name as a string slice.
32
    #[must_use]
33
9
    pub const fn as_str(self) -> &'static str {
34
9
        match self {
35
1
            Self::Type => "data-autocomplete",
36
1
            Self::FetchUrl => "data-fetch-url",
37
1
            Self::FetchMethod => "data-fetch-method",
38
1
            Self::DisplayInput => "data-display-input",
39
1
            Self::HiddenInput => "data-hidden-input",
40
1
            Self::CurrencyInput => "data-currency-input",
41
1
            Self::DependsOn => "data-depends-on",
42
1
            Self::Initialized => "data-initialized",
43
1
            Self::Index => "data-index",
44
        }
45
9
    }
46
}
47

            
48
/// CSS selectors used by autocomplete components.
49
pub mod selectors {
50
    /// Selector for the autocomplete results container
51
    pub const RESULTS_CONTAINER: &str = ".autocomplete-results";
52
}
53

            
54
/// Values for the autocomplete type attribute.
55
pub mod autocomplete_type {
56
    /// Account autocomplete type
57
    pub const ACCOUNT: &str = "account";
58
    /// Commodity autocomplete type
59
    pub const COMMODITY: &str = "commodity";
60
    /// Tag name autocomplete type
61
    pub const TAG_NAME: &str = "tag-name";
62
    /// Tag value autocomplete type (depends on tag-name)
63
    pub const TAG_VALUE: &str = "tag-value";
64
}
65

            
66
#[cfg(test)]
67
mod tests {
68
    use super::*;
69

            
70
    #[test]
71
1
    fn test_attr_names() {
72
1
        assert_eq!(AutocompleteAttr::Type.as_str(), "data-autocomplete");
73
1
        assert_eq!(AutocompleteAttr::FetchUrl.as_str(), "data-fetch-url");
74
1
        assert_eq!(AutocompleteAttr::FetchMethod.as_str(), "data-fetch-method");
75
1
        assert_eq!(
76
1
            AutocompleteAttr::DisplayInput.as_str(),
77
            "data-display-input"
78
        );
79
1
        assert_eq!(AutocompleteAttr::HiddenInput.as_str(), "data-hidden-input");
80
1
        assert_eq!(
81
1
            AutocompleteAttr::CurrencyInput.as_str(),
82
            "data-currency-input"
83
        );
84
1
        assert_eq!(AutocompleteAttr::DependsOn.as_str(), "data-depends-on");
85
1
        assert_eq!(AutocompleteAttr::Initialized.as_str(), "data-initialized");
86
1
        assert_eq!(AutocompleteAttr::Index.as_str(), "data-index");
87
1
    }
88

            
89
    #[test]
90
1
    fn test_autocomplete_types() {
91
1
        assert_eq!(autocomplete_type::ACCOUNT, "account");
92
1
        assert_eq!(autocomplete_type::COMMODITY, "commodity");
93
1
        assert_eq!(autocomplete_type::TAG_NAME, "tag-name");
94
1
        assert_eq!(autocomplete_type::TAG_VALUE, "tag-value");
95
1
    }
96
}