moment.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """Moment component for humanized date rendering."""
  2. from typing import Any, Dict, List, Optional
  3. from reflex.base import Base
  4. from reflex.components.component import Component, NoSSRComponent
  5. from reflex.utils import imports
  6. from reflex.vars import Var
  7. class MomentDelta(Base):
  8. """A delta used for add/subtract prop in Moment."""
  9. years: Optional[int]
  10. quarters: Optional[int]
  11. months: Optional[int]
  12. weeks: Optional[int]
  13. days: Optional[int]
  14. hours: Optional[int]
  15. minutess: Optional[int]
  16. seconds: Optional[int]
  17. milliseconds: Optional[int]
  18. class Moment(NoSSRComponent):
  19. """The Moment component."""
  20. tag: str = "Moment"
  21. is_default = True
  22. library: str = "react-moment"
  23. lib_dependencies: List[str] = ["moment"]
  24. # How often the date update (how often time update / 0 to disable).
  25. interval: Var[int]
  26. # Formats the date according to the given format string.
  27. format: Var[str]
  28. # When formatting duration time, the largest-magnitude tokens are automatically trimmed when they have no value.
  29. trim: Var[bool]
  30. # Use the parse attribute to tell moment how to parse the given date when non-standard.
  31. parse: Var[str]
  32. # Add a delta to the base date (keys are "years", "quarters", "months", "weeks", "days", "hours", "minutes", "seconds")
  33. add: Var[MomentDelta]
  34. # Subtract a delta to the base date (keys are "years", "quarters", "months", "weeks", "days", "hours", "minutes", "seconds")
  35. subtract: Var[MomentDelta]
  36. # Displays the date as the time from now, e.g. "5 minutes ago".
  37. from_now: Var[bool]
  38. # Setting fromNowDuring will display the relative time as with fromNow but just during its value in milliseconds, after that format will be used instead.
  39. from_now_during: Var[int]
  40. # Similar to fromNow, but gives the opposite interval.
  41. to_now: Var[bool]
  42. # Adds a title attribute to the element with the complete date.
  43. with_title: Var[bool]
  44. # How the title date is formatted when using the withTitle attribute.
  45. title_format: Var[str]
  46. # Show the different between this date and the rendered child.
  47. diff: Var[str]
  48. # Display the diff as decimal.
  49. decimal: Var[bool]
  50. # Display the diff in given unit.
  51. unit: Var[str]
  52. # Shows the duration (elapsed time) between two dates. duration property should be behind date property time-wise.
  53. duration: Var[str]
  54. # The date to display (also work if passed as children).
  55. date: Var[str]
  56. # Shows the duration (elapsed time) between now and the provided datetime.
  57. duration_from_now: Var[bool]
  58. # Tells Moment to parse the given date value as a unix timestamp.
  59. unix: Var[bool]
  60. # Outputs the result in local time.
  61. local: Var[bool]
  62. # Display the date in the given timezone.
  63. tz: Var[str]
  64. def _get_imports(self) -> imports.ImportDict:
  65. merged_imports = super()._get_imports()
  66. if self.tz is not None:
  67. merged_imports = imports.merge_imports(
  68. merged_imports,
  69. {"moment-timezone": {imports.ImportVar(tag="")}},
  70. )
  71. return merged_imports
  72. def get_event_triggers(self) -> Dict[str, Any]:
  73. """Get the events triggers signatures for the component.
  74. Returns:
  75. The signatures of the event triggers.
  76. """
  77. return {
  78. **super().get_event_triggers(),
  79. "on_change": lambda date: [date],
  80. }
  81. @classmethod
  82. def create(cls, *children, **props) -> Component:
  83. """Create a Moment component.
  84. Args:
  85. *children: The children of the component.
  86. **props: The properties of the component.
  87. Returns:
  88. The Moment Component.
  89. """
  90. comp = super().create(*children, **props)
  91. if "tz" in props:
  92. comp.lib_dependencies.append("moment-timezone")
  93. return comp