From 391d7a912bb9ff029cbcac53895aa991f26e69b6 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Tue, 23 Apr 2019 08:15:17 -0600 Subject: [PATCH] Added some type safety to Step --- attribute/generated.go | 8 -------- attribute/step.go | 25 +++++++++++++++++++++++++ attribute/step_test.go | 18 ++++++++++++++++++ defs.json | 1 - 4 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 attribute/step.go create mode 100644 attribute/step_test.go diff --git a/attribute/generated.go b/attribute/generated.go index 8370fe4..2cc76af 100644 --- a/attribute/generated.go +++ b/attribute/generated.go @@ -763,14 +763,6 @@ func Start(value string) hatmill.Attrib { } } -// Step creates a "step" attribute -func Step(value string) hatmill.Attrib { - return hatmill.Attrib{ - Key: "step", - Value: value, - } -} - // Style creates a "style" attribute func Style(value string) hatmill.Attrib { return hatmill.Attrib{ diff --git a/attribute/step.go b/attribute/step.go new file mode 100644 index 0000000..87e2d8c --- /dev/null +++ b/attribute/step.go @@ -0,0 +1,25 @@ +package attribute + +import "strconv" +import "gitlab.codemonkeysoftware.net/b/hatmill" + +type StepValue *float32 + +func StepAny() StepValue { + return nil +} + +func StepFloat(value float32) StepValue { + return &value +} + +// Step indicates the minimum allowed change to a number input. +func Step(value StepValue) hatmill.Attrib { + var attr = hatmill.Attrib{Key: "step"} + if value == nil { + attr.Value = "any" + } else { + attr.Value = strconv.FormatFloat(float64(*value), 'G', -1, 32) + } + return attr +} diff --git a/attribute/step_test.go b/attribute/step_test.go new file mode 100644 index 0000000..f380301 --- /dev/null +++ b/attribute/step_test.go @@ -0,0 +1,18 @@ +package attribute_test + +import ( + "gitlab.codemonkeysoftware.net/b/hatmill/attribute" + "gitlab.codemonkeysoftware.net/b/hatmill/element" + "os" +) + +func ExampleStep() { + s1 := attribute.Step(attribute.StepAny()) + s2 := attribute.Step(attribute.StepFloat(0.5)) + e := element.Div()( + element.Input(s1), + element.Input(s2), + ) + e.WriteTo(os.Stdout) + // Output:
+} diff --git a/defs.json b/defs.json index 16b0e49..0560759 100644 --- a/defs.json +++ b/defs.json @@ -97,7 +97,6 @@ {"name": "srclang", "type": "string"}, {"name": "srcset", "type": "string"}, {"name": "start", "type": "string"}, - {"name": "step", "type": "string"}, {"name": "style", "type": "string"}, {"name": "summary", "type": "string"}, {"name": "tabindex", "type": "int"},