@uifabric/utilities
Version:
Fluent UI React utilities for building components.
1 lines • 2.49 kB
Source Map (JSON)
{"version":3,"file":"initializeComponentRef.js","sourceRoot":"../src/","sources":["initializeComponentRef.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CAAoC,GAAoC;IAC5G,eAAe,CAAC,GAAG,EAAE;QACnB,iBAAiB,EAAE,QAAQ;QAC3B,kBAAkB,EAAE,SAAS;QAC7B,oBAAoB,EAAE,UAAU;KACjC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,QAAQ;IACf,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;AAClD,CAAC;AAED,SAAS,SAAS,CAAC,SAAqB;IACtC,IAAI,SAAS,CAAC,YAAY,KAAK,IAAI,CAAC,KAAK,CAAC,YAAY,EAAE;QACtD,8DAA8D;QAC9D,gBAAgB,CAAE,SAAiB,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;QACxD,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;KACjD;AACH,CAAC;AAED,SAAS,UAAU;IACjB,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;AAClD,CAAC;AAED,SAAS,gBAAgB,CAAa,YAAyC,EAAE,KAAwB;IACvG,IAAI,YAAY,EAAE;QAChB,IAAI,OAAO,YAAY,KAAK,QAAQ,EAAE;YACnC,YAA+C,CAAC,OAAO,GAAG,KAAK,CAAC;SAClE;aAAM,IAAI,OAAO,YAAY,KAAK,UAAU,EAAE;YAC5C,YAAyB,CAAC,KAAK,CAAC,CAAC;SACnC;KACF;AACH,CAAC","sourcesContent":["import * as React from 'react';\nimport { IBaseProps } from './BaseComponent.types';\nimport { extendComponent } from './extendComponent';\n\n/**\n * Helper to manage componentRef resolution. Internally appends logic to\n * lifetime methods to resolve componentRef to the passed in object.\n *\n * Usage: call initializeComponentRef(this) in the constructor,\n */\nexport function initializeComponentRef<TProps extends IBaseProps, TState>(obj: React.Component<TProps, TState>): void {\n extendComponent(obj, {\n componentDidMount: _onMount,\n componentDidUpdate: _onUpdate,\n componentWillUnmount: _onUnmount,\n });\n}\n\nfunction _onMount(): void {\n _setComponentRef(this.props.componentRef, this);\n}\n\nfunction _onUpdate(prevProps: IBaseProps): void {\n if (prevProps.componentRef !== this.props.componentRef) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _setComponentRef((prevProps as any).componentRef, null);\n _setComponentRef(this.props.componentRef, this);\n }\n}\n\nfunction _onUnmount(): void {\n _setComponentRef(this.props.componentRef, null);\n}\n\nfunction _setComponentRef<TInterface>(componentRef: React.RefObject<TInterface>, value: TInterface | null): void {\n if (componentRef) {\n if (typeof componentRef === 'object') {\n (componentRef as { current: TInterface | null }).current = value;\n } else if (typeof componentRef === 'function') {\n (componentRef as Function)(value);\n }\n }\n}\n"]}