moment.py 4.0 KB

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