add color input

This commit is contained in:
long2ice
2021-05-06 22:56:57 +08:00
parent 251f33afee
commit fe301a84a6
2 changed files with 61 additions and 16 deletions

View File

@ -0,0 +1,10 @@
<div class="form-group mb-3">
<label class="form-label">{{ label }}</label>
<input {% if not null %}required{% endif %} type="color" class="form-control form-control-color" name="{{ name }}"
placeholder="{{ placeholder }}" {% if disabled %}disabled{% endif %} value="{{ value }}">
{% if help_text %}
<small class="form-hint">
{{ help_text }}
</small>
{% endif %}
</div>

View File

@ -14,8 +14,10 @@ from fastapi_admin.widgets import Widget
class Input(Widget): class Input(Widget):
template = "widgets/inputs/input.html" template = "widgets/inputs/input.html"
def __init__(self, default: Any = None, null: bool = False, **context): def __init__(
super().__init__(null=null, **context) self, help_text: Optional[str] = None, default: Any = None, null: bool = False, **context
):
super().__init__(null=null, help_text=help_text, **context)
self.default = default self.default = default
async def parse_value(self, request: Request, value: Any): async def parse_value(self, request: Request, value: Any):
@ -42,7 +44,12 @@ class Text(Input):
input_type: Optional[str] = "text" input_type: Optional[str] = "text"
def __init__( def __init__(
self, default: Any = None, null: bool = False, placeholder: str = "", disabled: bool = False self,
help_text: Optional[str] = None,
default: Any = None,
null: bool = False,
placeholder: str = "",
disabled: bool = False,
): ):
super().__init__( super().__init__(
null=null, null=null,
@ -50,14 +57,21 @@ class Text(Input):
input_type=self.input_type, input_type=self.input_type,
placeholder=placeholder, placeholder=placeholder,
disabled=disabled, disabled=disabled,
help_text=help_text,
) )
class Select(Input): class Select(Input):
template = "widgets/inputs/select.html" template = "widgets/inputs/select.html"
def __init__(self, default: Any = None, null: bool = False, disabled: bool = False): def __init__(
super().__init__(null=null, default=default, disabled=disabled) self,
help_text: Optional[str] = None,
default: Any = None,
null: bool = False,
disabled: bool = False,
):
super().__init__(help_text=help_text, null=null, default=default, disabled=disabled)
@abc.abstractmethod @abc.abstractmethod
async def get_options(self): async def get_options(self):
@ -82,8 +96,9 @@ class ForeignKey(Select):
default: Any = None, default: Any = None,
null: bool = False, null: bool = False,
disabled: bool = False, disabled: bool = False,
help_text: Optional[str] = None,
): ):
super().__init__(default=default, null=null, disabled=disabled) super().__init__(help_text=help_text, default=default, null=null, disabled=disabled)
self.model = model self.model = model
async def get_options(self): async def get_options(self):
@ -104,8 +119,9 @@ class ManyToMany(Select):
self, self,
model: Type[Model], model: Type[Model],
disabled: bool = False, disabled: bool = False,
help_text: Optional[str] = None,
): ):
super().__init__(disabled=disabled) super().__init__(help_text=help_text, disabled=disabled)
self.model = model self.model = model
async def get_options(self): async def get_options(self):
@ -134,8 +150,9 @@ class Enum(Select):
enum_type: Type = int, enum_type: Type = int,
null: bool = False, null: bool = False,
disabled: bool = False, disabled: bool = False,
help_text: Optional[str] = None,
): ):
super().__init__(default=default, null=null, disabled=disabled) super().__init__(help_text=help_text, default=default, null=null, disabled=disabled)
self.enum = enum self.enum = enum
self.enum_type = enum_type self.enum_type = enum_type
@ -156,12 +173,17 @@ class Email(Text):
class Json(Input): class Json(Input):
template = "widgets/inputs/json.html" template = "widgets/inputs/json.html"
def __init__(self, null: bool = False, options: Optional[dict] = None): def __init__(
self,
help_text: Optional[str] = None,
null: bool = False,
options: Optional[dict] = None,
):
""" """
options config to jsoneditor, see https://github.com/josdejong/jsoneditor options config to jsoneditor, see https://github.com/josdejong/jsoneditor
:param options: :param options:
""" """
super().__init__(null=null) super().__init__(null=null, help_text=help_text)
if not options: if not options:
options = {} options = {}
self.context.update(options=options) self.context.update(options=options)
@ -190,22 +212,25 @@ class File(Input):
def __init__( def __init__(
self, self,
upload_provider: FileUpload, upload: FileUpload,
default: Any = None, default: Any = None,
null: bool = False, null: bool = False,
disabled: bool = False, disabled: bool = False,
help_text: Optional[str] = None,
): ):
super().__init__( super().__init__(
null=null, null=null,
default=default, default=default,
input_type=self.input_type, input_type=self.input_type,
disabled=disabled, disabled=disabled,
help_text=help_text,
) )
self.upload_provider = upload_provider self.upload = upload
async def parse_value(self, request: Request, value: Optional[UploadFile]): async def parse_value(self, request: Request, value: Optional[UploadFile]):
if value: if value and value.filename:
return await self.upload_provider.upload(value) return await self.upload.upload(value)
return ""
class Image(File): class Image(File):
@ -215,8 +240,14 @@ class Image(File):
class Radio(Select): class Radio(Select):
template = "widgets/inputs/radio.html" template = "widgets/inputs/radio.html"
def __init__(self, options: List[Tuple[str, Any]], default: Any = None, disabled: bool = False): def __init__(
super().__init__(default=default, disabled=disabled) self,
options: List[Tuple[str, Any]],
help_text: Optional[str] = None,
default: Any = None,
disabled: bool = False,
):
super().__init__(default=default, disabled=disabled, help_text=help_text)
self.options = options self.options = options
async def get_options(self): async def get_options(self):
@ -242,3 +273,7 @@ class Password(Text):
class Number(Text): class Number(Text):
input_type = "number" input_type = "number"
class Color(Text):
template = "widgets/inputs/color.html"