Skip to content
Snippets Groups Projects
Commit a75c1416 authored by Jakub Ziebinski's avatar Jakub Ziebinski
Browse files

Implementation of the PercentInput

parent 6c463a09
No related branches found
No related tags found
No related merge requests found
from __future__ import annotations
from typing import TYPE_CHECKING
from clive.__private.ui.widgets.inputs.integer_input import IntegerInput
from clive.__private.validators.percent_validator import PercentValidator
if TYPE_CHECKING:
from collections.abc import Iterable
from textual.widgets._input import InputValidationOn
class PercentInput(IntegerInput):
"""An input for a values between 1 and 100."""
def __init__(
self,
title: str,
value: str | int | None = None,
*,
always_show_title: bool = False,
include_title_in_placeholder_when_blurred: bool = True,
show_invalid_reasons: bool = True,
required: bool = True,
validate_on: Iterable[InputValidationOn] | None = None,
valid_empty: bool = False,
id: str | None = None, # noqa: A002
classes: str | None = None,
disabled: bool = False,
) -> None:
super().__init__(
title=title,
value=str(value) if value is not None else None,
always_show_title=always_show_title,
include_title_in_placeholder_when_blurred=include_title_in_placeholder_when_blurred,
show_invalid_reasons=show_invalid_reasons,
required=required,
validators=PercentValidator(),
validate_on=validate_on,
valid_empty=valid_empty,
id=id,
classes=classes,
disabled=disabled,
)
from __future__ import annotations
from typing import Final
from textual.validation import ValidationResult, Validator
class PercentValidator(Validator):
INVALID_PERCENT_FAILURE_DESCRIPTION: str = "Invalid percent value."
MIN_PERCENT_VALUE: Final[int] = 1
MAX_PERCENT_VALUE: Final[int] = 100
def validate(self, value: str) -> ValidationResult:
if not value:
return self.failure(self.INVALID_PERCENT_FAILURE_DESCRIPTION, value)
if self.MIN_PERCENT_VALUE <= int(value) <= self.MAX_PERCENT_VALUE:
return self.success()
return self.failure(self.INVALID_PERCENT_FAILURE_DESCRIPTION, value)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment