Skip to content

Commit fe301a8

Browse files
committed
add color input
1 parent 251f33a commit fe301a8

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div class="form-group mb-3">
2+
<label class="form-label">{{ label }}</label>
3+
<input {% if not null %}required{% endif %} type="color" class="form-control form-control-color" name="{{ name }}"
4+
placeholder="{{ placeholder }}" {% if disabled %}disabled{% endif %} value="{{ value }}">
5+
{% if help_text %}
6+
<small class="form-hint">
7+
{{ help_text }}
8+
</small>
9+
{% endif %}
10+
</div>

fastapi_admin/widgets/inputs.py

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
class Input(Widget):
1515
template = "widgets/inputs/input.html"
1616

17-
def __init__(self, default: Any = None, null: bool = False, **context):
18-
super().__init__(null=null, **context)
17+
def __init__(
18+
self, help_text: Optional[str] = None, default: Any = None, null: bool = False, **context
19+
):
20+
super().__init__(null=null, help_text=help_text, **context)
1921
self.default = default
2022

2123
async def parse_value(self, request: Request, value: Any):
@@ -42,22 +44,34 @@ class Text(Input):
4244
input_type: Optional[str] = "text"
4345

4446
def __init__(
45-
self, default: Any = None, null: bool = False, placeholder: str = "", disabled: bool = False
47+
self,
48+
help_text: Optional[str] = None,
49+
default: Any = None,
50+
null: bool = False,
51+
placeholder: str = "",
52+
disabled: bool = False,
4653
):
4754
super().__init__(
4855
null=null,
4956
default=default,
5057
input_type=self.input_type,
5158
placeholder=placeholder,
5259
disabled=disabled,
60+
help_text=help_text,
5361
)
5462

5563

5664
class Select(Input):
5765
template = "widgets/inputs/select.html"
5866

59-
def __init__(self, default: Any = None, null: bool = False, disabled: bool = False):
60-
super().__init__(null=null, default=default, disabled=disabled)
67+
def __init__(
68+
self,
69+
help_text: Optional[str] = None,
70+
default: Any = None,
71+
null: bool = False,
72+
disabled: bool = False,
73+
):
74+
super().__init__(help_text=help_text, null=null, default=default, disabled=disabled)
6175

6276
@abc.abstractmethod
6377
async def get_options(self):
@@ -82,8 +96,9 @@ def __init__(
8296
default: Any = None,
8397
null: bool = False,
8498
disabled: bool = False,
99+
help_text: Optional[str] = None,
85100
):
86-
super().__init__(default=default, null=null, disabled=disabled)
101+
super().__init__(help_text=help_text, default=default, null=null, disabled=disabled)
87102
self.model = model
88103

89104
async def get_options(self):
@@ -104,8 +119,9 @@ def __init__(
104119
self,
105120
model: Type[Model],
106121
disabled: bool = False,
122+
help_text: Optional[str] = None,
107123
):
108-
super().__init__(disabled=disabled)
124+
super().__init__(help_text=help_text, disabled=disabled)
109125
self.model = model
110126

111127
async def get_options(self):
@@ -134,8 +150,9 @@ def __init__(
134150
enum_type: Type = int,
135151
null: bool = False,
136152
disabled: bool = False,
153+
help_text: Optional[str] = None,
137154
):
138-
super().__init__(default=default, null=null, disabled=disabled)
155+
super().__init__(help_text=help_text, default=default, null=null, disabled=disabled)
139156
self.enum = enum
140157
self.enum_type = enum_type
141158

@@ -156,12 +173,17 @@ class Email(Text):
156173
class Json(Input):
157174
template = "widgets/inputs/json.html"
158175

159-
def __init__(self, null: bool = False, options: Optional[dict] = None):
176+
def __init__(
177+
self,
178+
help_text: Optional[str] = None,
179+
null: bool = False,
180+
options: Optional[dict] = None,
181+
):
160182
"""
161183
options config to jsoneditor, see https://github.com/josdejong/jsoneditor
162184
:param options:
163185
"""
164-
super().__init__(null=null)
186+
super().__init__(null=null, help_text=help_text)
165187
if not options:
166188
options = {}
167189
self.context.update(options=options)
@@ -190,22 +212,25 @@ class File(Input):
190212

191213
def __init__(
192214
self,
193-
upload_provider: FileUpload,
215+
upload: FileUpload,
194216
default: Any = None,
195217
null: bool = False,
196218
disabled: bool = False,
219+
help_text: Optional[str] = None,
197220
):
198221
super().__init__(
199222
null=null,
200223
default=default,
201224
input_type=self.input_type,
202225
disabled=disabled,
226+
help_text=help_text,
203227
)
204-
self.upload_provider = upload_provider
228+
self.upload = upload
205229

206230
async def parse_value(self, request: Request, value: Optional[UploadFile]):
207-
if value:
208-
return await self.upload_provider.upload(value)
231+
if value and value.filename:
232+
return await self.upload.upload(value)
233+
return ""
209234

210235

211236
class Image(File):
@@ -215,8 +240,14 @@ class Image(File):
215240
class Radio(Select):
216241
template = "widgets/inputs/radio.html"
217242

218-
def __init__(self, options: List[Tuple[str, Any]], default: Any = None, disabled: bool = False):
219-
super().__init__(default=default, disabled=disabled)
243+
def __init__(
244+
self,
245+
options: List[Tuple[str, Any]],
246+
help_text: Optional[str] = None,
247+
default: Any = None,
248+
disabled: bool = False,
249+
):
250+
super().__init__(default=default, disabled=disabled, help_text=help_text)
220251
self.options = options
221252

222253
async def get_options(self):
@@ -242,3 +273,7 @@ class Password(Text):
242273

243274
class Number(Text):
244275
input_type = "number"
276+
277+
278+
class Color(Text):
279+
template = "widgets/inputs/color.html"

0 commit comments

Comments
 (0)