UNPKG

@yandex/ymaps3-types

Version:

Types for ymaps3 maps library

142 lines (141 loc) 5.12 kB
import type { YMap } from "./YMap"; import { GenericComplexEntity, GenericEntity, GenericGroupEntity, Context } from "./Entities"; /** * Entity Base Class. It has event handlers for attaching, detaching and updating props. Has a method for providing and using context. * @typeParam Props - Type of input props of the Entity. * @typeParam DefaultProps - Type of default input props of the Entity. * @example * ```ts * type YMapSomeEntityProps = { * name?: string; * }; * const defaultProps = { * name: 'entity' * }; * class YMapSomeEntity extends YMapEntity<YMapSomeEntityProps, typeof defaultProps> { * static defaultProps = defaultProps; * public isAttached: boolean; * constructor(props: YMapSomeEntityProps) { * super(props); * this.isAttached = false * // Additional actions can be taken in the constructor of a class. * } * protected _onAttach(): void { * this.isAttached = true; * // Additional actions can be taken when an Entity is attached. * } * // ... * } * ``` */ declare abstract class YMapEntity<Props, DefaultProps extends {} = {}> extends GenericEntity<Props, DefaultProps, YMap> { get root(): null | YMap; get parent(): null | YMapComplexEntity<unknown>; } /** * Entity that aggregates multiple Entities but looks basic from the outside. * @typeParam Props - Type of input props of the Entity. * @typeParam DefaultProps - Type of default input props of the Entity. * @typeParam Root - Root Entity Class. * @example * ```ts * type YMapSomeComplexEntityProps = { * name?: string; * }; * const defaultProps = { * name: 'entity' * }; * class YMapSomeComplexEntity extends YMapComplexEntity<YMapSomeComplexEntityProps, typeof defaultProps> { * static defaultProps = defaultProps; * private _someEntity?: YMapSomeEntity; // YMapSomeEntity extends GenericEntity * protected _onAttach(): void { * this._someEntity = new YMapSomeEntity(); * this.addChild(this._someEntity); // add someEntity as children * // ... * } * // ... * } * ``` */ declare abstract class YMapComplexEntity<Props, DefaultProps extends {} = {}> extends GenericComplexEntity<Props, DefaultProps, YMap> implements YMapEntity<Props, DefaultProps> { get root(): null | YMap; get parent(): null | YMapComplexEntity<unknown>; } /** * Entity that aggregates multiple Entities, and allows you to publicly add and remove entities to a subtree. * @typeParam Props - Type of input props of the Entity. * @typeParam DefaultProps - Type of default input props of the Entity. * @typeParam Root - Root Entity Class. * @example * ```ts * type YMapSomeGroupEntityProps = { * name?: string; * }; * const defaultProps = { * name: 'entity' * }; * class YMapSomeGroupEntity extends YMapGroupEntity<YMapSomeGroupEntityProps, typeof defaultProps> { * static defaultProps = defaultProps; * // ... * } * const groupEntity = new YMapSomeGroupEntity() * const someEntity = new YMapSomeEntity(); // YMapSomeEntity extends GenericEntity * groupEntity.addChild(someEntity); // add someEntity in YMapSomeGroupEntity object * groupEntity.removeChild(someEntity); // remove someEntity from YMapSomeGroupEntity object * ``` */ declare abstract class YMapGroupEntity<Props, DefaultProps extends {} = {}> extends GenericGroupEntity<Props, DefaultProps, YMap> implements YMapComplexEntity<Props, DefaultProps> { get root(): null | YMap; get parent(): null | YMapComplexEntity<unknown>; readonly children: readonly YMapEntity<unknown>[]; addChild(child: YMapEntity<unknown>, index?: number): this; removeChild(child: YMapEntity<unknown>): this; } /** * Context for YMap. It allows you to provide a value to the context and use it in the children of the context provider. * ```ts * const YMapSomeContext = new ymaps3.YMapContext('YMapSomeContext'); * class SomeValueProvider extends ymaps3.YMapGroupEntity<{}> { * constructor() { * super({}); * this._provideContext(YMapSomeContext, {your: 'value'}); // Special method to provide context value * } * } * ``` * And some child entity can use this context: * ```ts * class SomeContextConsumer extends ymaps3.YMapEntity<{}> { * constructor() { * super({}); * } * * _onAttach() { * const value = this._consumeContext(YMapSomeContext); // Special method to get context value * console.log(value); // {your: 'value'} * } * } * ``` * In any level of nesting: * ```tsx * <SomeValueProvider> * <YMapContainer> * <YMapContainer> * <SomeContextConsumer /> * </YMapContainer> * </YMapContainer> * </SomeEntity> * ``` * You can set the context value using [[YMapContextProvider]]: * ```tsx * <YMapContextProvider context={YMapSomeContext} value={{your: 'value'}}> * <YMapContainer> * <YMapContainer> * <SomeContextConsumer /> * </YMapContainer> * </YMapContainer> * </SomeEntity> * ``` */ declare class YMapContext<T> extends Context<T> { } export { YMapEntity, YMapComplexEntity, YMapGroupEntity, YMapContext };