/
LabeledField.purs
164 lines (138 loc) · 4.39 KB
/
LabeledField.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
module Lumi.Components.LabeledField where
import Prelude
import Color (cssStringHSLA)
import Data.Maybe (Maybe(..), maybe)
import Data.Nullable (toNullable)
import Effect.Unsafe (unsafePerformEffect)
import JSS (JSS, jss)
import Lumi.Components.Color (colorNames, colors)
import Lumi.Components.Input (alignToInput)
import Lumi.Components.Input as Input
import Lumi.Components.Text (subtext, text, nbsp)
import React.Basic.Classic (Component, JSX, createComponent, element, empty, makeStateless)
import React.Basic.DOM (CSS, css, unsafeCreateDOMComponent)
import React.Basic.DOM as R
data RequiredField
= Required
| Optional
| Neither
derive instance eqRequiredField :: Eq RequiredField
data ValidationMessage = Error String | Warning String
type LabeledFieldProps =
{ label :: JSX
, value :: JSX
, validationError :: Maybe ValidationMessage
, required :: RequiredField
, forceTopLabel :: Boolean
, style :: CSS
}
defaults :: LabeledFieldProps
defaults =
{ label: R.text "Label"
, value: Input.input Input.text_
, validationError: Nothing
, required: Neither
, forceTopLabel: false
, style: css {}
}
component :: Component LabeledFieldProps
component = createComponent "LabeledField"
labeledField :: LabeledFieldProps -> JSX
labeledField = makeStateless component render
where
_label = element (unsafePerformEffect $ unsafeCreateDOMComponent "label")
_column = element (unsafePerformEffect $ unsafeCreateDOMComponent "lumi-column")
render props =
_label
{ className: "lumi labeled-field"
, "data-force-top-label": props.forceTopLabel
, style: props.style
, children:
[ columnLeft props.label props.required
, columnRight props.value props.validationError
]
}
columnLeft label required =
_column
{ "class": "labeled-field--left"
, children:
[ alignToInput
case required of
Neither -> label
Optional ->
label <> R.text nbsp <>
text subtext
{ children = [ R.text "- optional" ]
, color = toNullable (Just colorNames.black1)
}
Required ->
label <> R.text nbsp <>
text subtext
{ children = [ R.text " *" ]
, color = toNullable (Just colorNames.accent3)
}
]
}
columnRight value validationError =
_column
{ "class": "labeled-field--right"
, children:
[ value
, maybe empty renderValidationError validationError
]
}
renderValidationError =
case _ of
Error error ->
text subtext
{ className = toNullable (Just "labeled-field--validation-error")
, children = [ R.text error ]
}
Warning warn ->
text subtext
{ className = toNullable (Just "labeled-field--validation-warning")
, children = [ R.text warn ]
}
styles :: JSS
styles = jss
{ "@global":
{ "label.lumi.labeled-field":
{ display: "flex"
, flexFlow: "row nowrap"
, "& .labeled-field--left":
{ flex: "0 1 30%"
, whiteSpace: "nowrap"
}
, "& .labeled-field--right":
{ flex: "0 1 70%"
, maxWidth: "70%"
}
, "&[data-force-top-label=\"true\"]": labeledFieldTopLabelStyles
, "@media (max-width: 448px)": labeledFieldTopLabelStyles
, "& .labeled-field--validation-error": labeledFieldValidationErrorStyles
, "& .labeled-field--validation-warning": labeledFieldValidationWarningStyles
}
}
}
labeledFieldTopLabelStyles :: JSS
labeledFieldTopLabelStyles = jss
{ flexFlow: "column nowrap"
, "& .labeled-field--left":
{ flex: "initial"
, whiteSpace: "normal"
}
, "& .labeled-field--right":
{ flex: "initial"
, maxWidth: "none"
}
}
labeledFieldValidationErrorStyles :: JSS
labeledFieldValidationErrorStyles = jss
{ color: cssStringHSLA colors.accent3
, marginTop: "4px"
}
labeledFieldValidationWarningStyles :: JSS
labeledFieldValidationWarningStyles = jss
{ color: cssStringHSLA colors.accent2
, marginTop: "4px"
}