perspective_viewer/components/style_controls/number_string_format/
digits_section.rs1use itertools::Itertools;
14use perspective_client::ColumnType;
15use yew::html;
16
17use super::CustomNumberFormat;
18use crate::components::containers::select::{Select, SelectItem};
19use crate::components::form::number_field::NumberField;
20use crate::components::form::number_range_field::NumberRangeField;
21use crate::components::form::optional_field::OptionalField;
22use crate::components::form::select_field::SelectEnumField;
23use crate::components::style_controls::CustomNumberFormatMsg;
24use crate::config::{
25 ROUNDING_INCREMENTS, RoundingIncrement, RoundingMode, RoundingPriority, TrailingZeroDisplay,
26};
27
28impl CustomNumberFormat {
29 pub fn digits_section(&self, ctx: &yew::prelude::Context<Self>) -> yew::prelude::Html {
30 let is_float = matches!(ctx.props().view_type, ColumnType::Float);
31 html! {
32 <>
33 <NumberField
34 label="minimum-integer-digits"
35 min=1.
36 max=21.
37 step=1.
38 default=1.
39 current_value={self.config.minimum_integer_digits}
40 on_change={ctx.link().callback(CustomNumberFormatMsg::MinimumIntegerDigits)}
41 />
42 if is_float { { self.float_section(ctx) } } else { { self.rounding_increment(ctx) } }
43 </>
44 }
45 }
46
47 fn rounding_increment(&self, ctx: &yew::prelude::Context<Self>) -> yew::prelude::Html {
48 let values = ROUNDING_INCREMENTS
50 .iter()
51 .map(|val| RoundingIncrement::Custom(*val))
52 .map(SelectItem::Option)
53 .collect_vec();
54 let values = [vec![SelectItem::Option(RoundingIncrement::Auto)], values].concat();
55 let on_check = ctx
56 .link()
57 .callback(|_| CustomNumberFormatMsg::RoundingIncrement(RoundingIncrement::default()));
58 let selected = self
59 .config
60 .rounding_increment
61 .map(RoundingIncrement::Custom)
62 .unwrap_or_default();
63 html! {
64 <div class="row">
65 <OptionalField
66 label="rounding-increment"
67 {on_check}
68 checked={self.config.rounding_increment.is_some()}
70 >
71 <Select<RoundingIncrement>
72 {values}
73 {selected}
74 on_select={ctx.link().callback(CustomNumberFormatMsg::RoundingIncrement)}
75 />
76 </OptionalField>
77 </div>
78 }
79 }
80
81 fn float_section(&self, ctx: &yew::prelude::Context<Self>) -> yew::prelude::Html {
82 let fractional_value = Some((
83 self.config.minimum_fraction_digits.unwrap_or(2.),
84 self.config.maximum_fraction_digits.unwrap_or(2.),
85 ));
86
87 let significant_value = Some((
88 self.config.minimum_significant_digits.unwrap_or(1.),
89 self.config.maximum_significant_digits.unwrap_or(21.),
90 ));
91
92 html! {
93 <>
94 <div class="row">
95 <NumberRangeField
96 label="fractional-digits"
97 min=0.
98 max=20.
99 step=1.
100 default={(2., 2.)}
101 current_value={fractional_value}
102 on_change={ctx.link().callback(CustomNumberFormatMsg::FracChange)}
103 />
104 </div>
105 <div class="row">
106 <NumberRangeField
107 label="significant-digits"
108 min=1.
109 max=21.
110 step=1.
111 default={(1., 21.)}
112 current_value={significant_value}
113 on_change={ctx.link().callback(CustomNumberFormatMsg::SigChange)}
114 />
115 </div>
116 { self.rounding_increment(ctx) }
117 <SelectEnumField<RoundingPriority>
118 label="rounding-priority"
119 current_value={self.config.rounding_priority}
120 on_change={ctx.link().callback(CustomNumberFormatMsg::RoundingPriority)}
121 />
122 <SelectEnumField<RoundingMode>
123 label="rounding-mode"
124 current_value={self.config.rounding_mode}
125 on_change={ctx.link().callback(CustomNumberFormatMsg::RoundingMode)}
126 />
127 <SelectEnumField<TrailingZeroDisplay>
128 label="trailing-zero-display"
129 current_value={self.config.trailing_zero_display}
130 on_change={ctx.link().callback(CustomNumberFormatMsg::TrailingZero)}
131 />
132 </>
133 }
134 }
135}