Content-Length: 785706 | pFad | http://github.com/postgresml/postgresml/pull/1048/commits/d722b51c28c0b6c70749e587d6f388bc78cfb249

7E Dan select component by chillenberger · Pull Request #1048 · postgresml/postgresml · GitHub
Skip to content

Dan select component #1048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
create select component from dropdown
  • Loading branch information
chillenberger committed Sep 22, 2023
commit d722b51c28c0b6c70749e587d6f388bc78cfb249
6 changes: 5 additions & 1 deletion pgml-dashboard/src/components/dropdown/dropdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
--bs-btn-active-color: #{$gray-100};
--bs-btn-hover-color: #{$gray-100};

&.error {
border-color: #{$error};
}

.material-symbols-outlined {
color: #{$neon-shade-100};
}
Expand All @@ -73,7 +77,7 @@
}

.menu-item {
a {
a, div {
padding: 8px 12px;
overflow: hidden;
text-overflow: ellipsis;
Expand Down
46 changes: 40 additions & 6 deletions pgml-dashboard/src/components/dropdown/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::components::navigation::dropdown_link::DropdownLink;
use crate::components::stimulus::stimulus_target::StimulusTarget;
use pgml_components::component;
use pgml_components::Component;
use sailfish::TemplateOnce;
Expand All @@ -21,35 +23,57 @@ pub struct Dropdown {
//github.com/ The currently selected value.
value: DropdownValue,

//github.com/ The list of dropdown links to render.
links: Vec<StaticNavLink>,
//github.com/ The list of dropdown items to render.
items: Vec<Component>,

//github.com/ Position of the dropdown menu.
offset: String,

//github.com/ Whether or not the dropdown is collapsble.
//github.com/ Whether or not the dropdown is collapsable.
collapsable: bool,
offset_collapsed: String,

//github.com/ Where the dropdown menu should appear
menu_position: String,
expandable: bool,

//github.com/ target to control value
value_target: StimulusTarget,
}

impl Dropdown {
pub fn new(links: Vec<StaticNavLink>) -> Self {
pub fn new() -> Self {
Dropdown {
items: Vec::new(),
value: DropdownValue::Text("Dropdown".to_owned().into()),
offset: "0, 10".to_owned(),
offset_collapsed: "68, -44".to_owned(),
menu_position: "".to_owned(),
..Default::default()
}
}

pub fn nav(links: Vec<StaticNavLink>) -> Self {
let binding = links
.iter()
.filter(|link| link.active)
.collect::<Vec<&StaticNavLink>>();

let active = binding.first();
let value = if let Some(active) = active {
active.name.to_owned()
} else {
"Menu".to_owned()
"Dropdown Nav".to_owned()
};

let mut items = Vec::new();
for link in links {
let item = DropdownLink::new(link);
items.push(item.into());
}

Dropdown {
links,
items,
value: DropdownValue::Text(value.into()),
offset: "0, 10".to_owned(),
offset_collapsed: "68, -44".to_owned(),
Expand All @@ -58,6 +82,11 @@ impl Dropdown {
}
}

pub fn items(mut self, items: Vec<Component>) -> Self {
self.items = items;
self
}

pub fn text(mut self, value: Component) -> Self {
self.value = DropdownValue::Text(value);
self
Expand Down Expand Up @@ -97,6 +126,11 @@ impl Dropdown {
self.expandable = true;
self
}

pub fn value_target(mut self, value_target: StimulusTarget) -> Self {
self.value_target = value_target;
self
}
}

component!(Dropdown);
13 changes: 4 additions & 9 deletions pgml-dashboard/src/components/dropdown/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
role="button"
data-bs-toggle="dropdown"
data-bs-offset="<%= offset %>"
aria-expanded="false">
aria-expanded="false"
<%- value_target %>>
<span class="btn-dropdown-text"><%+ text %></span>
<span class="material-symbols-outlined">
expand_more
Expand All @@ -42,14 +43,8 @@
<% } %>

<ul class="dropdown-menu <%= menu_position %>">
<% for link in links { %>
<li class="menu-item d-flex align-items-center <% if link.disabled { %>disabled<% } %>">
<% if link.disabled { %>
<a type="button" class="dropdown-item" disabled href="#"><%= link.name %></a>
<% } else { %>
<a type="button" class="dropdown-item" href="<%- link.href %>"><%= link.name %></a>
<% } %>
</li>
<% for item in items { %>
<%+ item %>
<% } %>
</ul>
</div>
Expand Down
4 changes: 4 additions & 0 deletions pgml-dashboard/src/components/inputs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
pub mod range_group;
pub use range_group::RangeGroup;

// src/components/inputs/select
pub mod select;
pub use select::Select;

// src/components/inputs/text
pub mod text;
111 changes: 111 additions & 0 deletions pgml-dashboard/src/components/inputs/select/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use crate::components::stimulus::stimulus_action::{StimulusAction, StimulusEvents};
use pgml_components::component;
use pgml_components::Component;
use sailfish::TemplateOnce;

#[derive(TemplateOnce, Default)]
#[template(path = "inputs/select/template.html")]
pub struct Select {
options: Vec<Component>,
value: String,
offset: String,
collapsable: bool,
offset_collapsed: String,
menu_position: String,
expandable: bool,
name: String,
}

impl Select {
pub fn new() -> Select {
Select {
options: Vec::new(),
value: "Select".to_owned(),
offset: "0, 10".to_owned(),
offset_collapsed: "68, -44".to_owned(),
menu_position: "".to_owned(),
name: "input_name".to_owned(),
..Default::default()
}
.options(vec![
"option1".to_owned(),
"option2".to_owned(),
"option3".to_owned(),
])
}

pub fn options(mut self, values: Vec<String>) -> Self {
let mut options = Vec::new();
self.value = values.first().unwrap().to_owned();

for value in values {
let item = Option::new(
value,
StimulusAction::new()
.controller("inputs-select")
.method("choose")
.action(StimulusEvents::Click),
);
options.push(item.into());
}

self.options = options;
self
}

pub fn name(mut self, name: &str) -> Self {
self.name = name.to_owned();
self
}

pub fn text(mut self, value: String) -> Self {
self.value = value;
self
}

pub fn collapsable(mut self) -> Self {
self.collapsable = true;
self
}

pub fn menu_end(mut self) -> Self {
self.menu_position = "dropdown-menu-end".to_owned();
self
}

pub fn menu_start(mut self) -> Self {
self.menu_position = "dropdown-menu-start".to_owned();
self
}

pub fn offset(mut self, offset: &str) -> Self {
self.offset = offset.to_owned();
self
}

pub fn offset_collapsed(mut self, offset: &str) -> Self {
self.offset_collapsed = offset.to_owned();
self
}

pub fn expandable(mut self) -> Self {
self.expandable = true;
self
}
}

#[derive(TemplateOnce)]
#[template(path = "inputs/select/option.html")]
pub struct Option {
value: String,
action: StimulusAction,
}

impl Option {
pub fn new(value: String, action: StimulusAction) -> Self {
Option { value, action }
}
}

component!(Option);
component!(Select);
4 changes: 4 additions & 0 deletions pgml-dashboard/src/components/inputs/select/option.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

<li class="menu-item d-flex align-items-center">
<button type="button" class="dropdown-item" data-action="<%- action %>"><%= value %></button>
</li>
3 changes: 3 additions & 0 deletions pgml-dashboard/src/components/inputs/select/select.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
div[data-controller="inputs-select"] {

}
11 changes: 11 additions & 0 deletions pgml-dashboard/src/components/inputs/select/select_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
static targets = ["input", "value"]

choose(e) {
this.inputTarget.value = e.target.innerHTML
this.valueTarget.innerHTML = e.target.innerHTML
}

}
33 changes: 33 additions & 0 deletions pgml-dashboard/src/components/inputs/select/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%
use crate::components::dropdown::Dropdown;
use crate::components::stimulus::stimulus_target::StimulusTarget;
%>
<div data-controller="inputs-select">

<% let mut dropdown = Dropdown::new()
.items(options)
.offset(&offset)
.offset_collapsed(&offset_collapsed)
.text(value.clone().into());

if menu_position == "dropdown-menu-end" {
dropdown = dropdown.menu_end();
} else if menu_position == "dropdown-menu-start" {
dropdown = dropdown.menu_start();
}

if collapsable {
dropdown = dropdown.collapsable();
}

if expandable {
dropdown = dropdown.expandable();
}

dropdown = dropdown.value_target(StimulusTarget::new().controller("inputs-select").name("value"));
%>

<%+ dropdown %>

<input type="hidden" name="<%= name %>" value="<%= value %>" data-inputs-select-target="input">
</div>
40 changes: 1 addition & 39 deletions pgml-dashboard/src/components/inputs/text/editable_header/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::components::stimulus::stimulus_target::StimulusTarget;
use pgml_components::component;
use sailfish::runtime::{Buffer, Render};
use sailfish::TemplateOnce;
use std::fmt;

Expand All @@ -25,44 +25,6 @@ impl fmt::Display for Headers {
}
}

pub struct StimulusTarget {
controller: Option<String>,
target_name: Option<String>,
}

impl StimulusTarget {
pub fn new() -> StimulusTarget {
StimulusTarget {
controller: None,
target_name: None,
}
}

pub fn controller(mut self, controller: &str) -> Self {
self.controller = Some(controller.to_string());
self
}

pub fn target_name(mut self, target_name: &str) -> Self {
self.target_name = Some(target_name.to_string());
self
}
}

impl Render for StimulusTarget {
fn render(&self, b: &mut Buffer) -> Result<(), sailfish::RenderError> {
if self.controller.is_none() || self.target_name.is_none() {
return format!("").render(b);
}
format!(
"data-{}-target=\"{}\"",
self.controller.to_owned().unwrap(),
self.target_name.to_owned().unwrap()
)
.render(b)
}
}

#[derive(TemplateOnce)]
#[template(path = "inputs/text/editable_header/template.html")]
pub struct EditableHeader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</button>

<div class="mb-4">
<%+ Dropdown::new(dropdown_nav.links).collapsable() %>
<%+ Dropdown::nav(dropdown_nav.links).collapsable() %>
</div>
<%+ LeftNavMenu { nav: upper_nav } %>
</div>
Expand Down
3 changes: 3 additions & 0 deletions pgml-dashboard/src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ pub use static_nav::StaticNav;
pub mod static_nav_link;
pub use static_nav_link::StaticNavLink;

// src/components/stimulus
pub mod stimulus;

// src/components/tables
pub mod tables;

Expand Down
2 changes: 1 addition & 1 deletion pgml-dashboard/src/components/navbar_web_app/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<% if !account_management_nav.links.is_empty() { %>
<li class="d-none d-lg-flex nav-item align-items-center">
<%+
Dropdown::new(account_management_nav.links.clone())
Dropdown::nav(account_management_nav.links.clone())
.icon(ProfileIcon::new().into())
.expandable()
%>
Expand Down
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/postgresml/postgresml/pull/1048/commits/d722b51c28c0b6c70749e587d6f388bc78cfb249

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy