Component.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import Calendar from '../Calendar'
  2. import View from '../View'
  3. import Theme from '../theme/Theme'
  4. import { DateEnv } from '../datelib/env'
  5. import { isPropsEqual, assignTo, EqualityFuncHash } from '../util/object'
  6. let guid = 0
  7. export interface ComponentContext {
  8. options: any
  9. dateEnv: DateEnv
  10. theme: Theme
  11. calendar: Calendar
  12. view?: View
  13. }
  14. export default class Component<PropsType> {
  15. equalityFuncs: EqualityFuncHash
  16. uid: string
  17. props: PropsType | null // non-null signals that a render happened
  18. // context vars
  19. context: ComponentContext
  20. dateEnv: DateEnv
  21. theme: Theme
  22. view: View
  23. calendar: Calendar
  24. isRtl: boolean
  25. constructor(context: ComponentContext) {
  26. this.uid = String(guid++)
  27. this.context = context
  28. this.dateEnv = context.dateEnv
  29. this.theme = context.theme
  30. this.view = context.view
  31. this.calendar = context.calendar
  32. this.isRtl = this.opt('dir') === 'rtl'
  33. }
  34. static addEqualityFuncs(newFuncs: EqualityFuncHash) {
  35. this.prototype.equalityFuncs = assignTo(
  36. {},
  37. this.prototype.equalityFuncs,
  38. newFuncs
  39. )
  40. }
  41. opt(name) {
  42. return this.context.options[name]
  43. }
  44. receiveProps(props: PropsType) {
  45. if (!this.props || !isPropsEqual(this.props, props, this.equalityFuncs)) {
  46. this.props = props
  47. this.render(props)
  48. }
  49. }
  50. protected render(props: PropsType) {
  51. }
  52. // after destroy is called, this component won't ever be used again
  53. destroy() {
  54. }
  55. }