moment.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """Moment component for humanized date rendering."""
  2. import dataclasses
  3. from datetime import date, datetime, time, timedelta
  4. from typing import List, Optional, Union
  5. from reflex.components.component import NoSSRComponent
  6. from reflex.event import EventHandler, passthrough_event_spec
  7. from reflex.utils.imports import ImportDict
  8. from reflex.vars.base import LiteralVar, Var
  9. @dataclasses.dataclass(frozen=True)
  10. class MomentDelta:
  11. """A delta used for add/subtract prop in Moment."""
  12. years: Optional[int] = dataclasses.field(default=None)
  13. quarters: Optional[int] = dataclasses.field(default=None)
  14. months: Optional[int] = dataclasses.field(default=None)
  15. weeks: Optional[int] = dataclasses.field(default=None)
  16. days: Optional[int] = dataclasses.field(default=None)
  17. hours: Optional[int] = dataclasses.field(default=None)
  18. minutes: Optional[int] = dataclasses.field(default=None)
  19. seconds: Optional[int] = dataclasses.field(default=None)
  20. milliseconds: Optional[int] = dataclasses.field(default=None)
  21. class Moment(NoSSRComponent):
  22. """The Moment component."""
  23. tag: str | None = "Moment"
  24. is_default = True
  25. library: str | None = "react-moment"
  26. lib_dependencies: List[str] = ["moment"]
  27. # How often the date update (how often time update / 0 to disable).
  28. interval: Var[int]
  29. # Formats the date according to the given format string.
  30. format: Var[str]
  31. # When formatting duration time, the largest-magnitude tokens are automatically trimmed when they have no value.
  32. trim: Var[bool]
  33. # Use the parse attribute to tell moment how to parse the given date when non-standard.
  34. parse: Var[str]
  35. # Add a delta to the base date (keys are "years", "quarters", "months", "weeks", "days", "hours", "minutes", "seconds")
  36. add: Var[MomentDelta]
  37. # Subtract a delta to the base date (keys are "years", "quarters", "months", "weeks", "days", "hours", "minutes", "seconds")
  38. subtract: Var[MomentDelta]
  39. # Displays the date as the time from now, e.g. "5 minutes ago".
  40. from_now: Var[bool]
  41. # Setting fromNowDuring will display the relative time as with fromNow but just during its value in milliseconds, after that format will be used instead.
  42. from_now_during: Var[int]
  43. # Similar to fromNow, but gives the opposite interval.
  44. to_now: Var[bool]
  45. # Adds a title attribute to the element with the complete date.
  46. with_title: Var[bool]
  47. # How the title date is formatted when using the withTitle attribute.
  48. title_format: Var[str]
  49. # Show the different between this date and the rendered child.
  50. diff: Var[str]
  51. # Display the diff as decimal.
  52. decimal: Var[bool]
  53. # Display the diff in given unit.
  54. unit: Var[str]
  55. # Shows the duration (elapsed time) between two dates. duration property should be behind date property time-wise.
  56. duration: Var[str]
  57. # The date to display (also work if passed as children).
  58. date: Var[Union[str, datetime, date, time, timedelta]]
  59. # Shows the duration (elapsed time) between now and the provided datetime.
  60. duration_from_now: Var[bool]
  61. # Tells Moment to parse the given date value as a unix timestamp.
  62. unix: Var[bool]
  63. # Outputs the result in local time.
  64. local: Var[bool]
  65. # Display the date in the given timezone.
  66. tz: Var[str]
  67. # The locale to use when rendering.
  68. locale: Var[str]
  69. # Fires when the date changes.
  70. on_change: EventHandler[passthrough_event_spec(str)]
  71. def add_imports(self) -> ImportDict:
  72. """Add the imports for the Moment component.
  73. Returns:
  74. The import dict for the component.
  75. """
  76. imports = {}
  77. if isinstance(self.locale, LiteralVar):
  78. imports[""] = f"moment/locale/{self.locale._var_value}"
  79. elif self.locale is not None:
  80. # If the user is using a variable for the locale, we can't know the
  81. # value at compile time so import all locales available.
  82. imports[""] = "moment/min/locales"
  83. if self.tz is not None:
  84. imports["moment-timezone"] = ""
  85. return imports