@progress/kendo-react-barcodes
Version:
React Barcodes provide a set of React components to build beautiful and customizable barcodes. KendoReact Barcodes package
122 lines (121 loc) • 3.86 kB
JavaScript
/**
* @license
*-------------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the package root for more information
*-------------------------------------------------------------------------------------------
*/
import * as n from "react";
import i from "prop-types";
import { createStore as m } from "./store/store.mjs";
import { observersReducer as v } from "./store/reducer.mjs";
import { isOptionsChanged as g } from "./utils/common.mjs";
import { BarcodeContext as u } from "./BarcodeContext.mjs";
import { validatePackage as b, WatermarkOverlay as f } from "@progress/kendo-react-common";
import { packageMetadata as O } from "./package-metadata.mjs";
const o = class o extends n.Component {
constructor(e) {
super(e), this.barcodeInstance = null, this.surface = null, this._element = null, this.observersStore = {}, this.showLicenseWatermark = !1, this.onWindowResize = () => {
this.barcodeInstance !== null && (this.barcodeInstance.redraw(), this.trigger("render", { sender: this }));
}, this.showLicenseWatermark = !b(O, { component: "Barcode" }), this.observersStore = m(v), this.contextValue = {
observersStore: this.observersStore
};
}
/**
* @hidden
*/
get element() {
return this._element;
}
/**
* @hidden
*/
componentDidMount() {
this.instantiateCoreBarcode(), window.addEventListener("resize", this.onWindowResize), this.trigger("render", { sender: this });
}
/**
* @hidden
*/
componentDidUpdate(e) {
const {
children: r,
deriveOptionsFromParent: t,
getTarget: s,
barcodeConstructor: w,
className: C,
renderAs: c,
...d
} = this.props, {
children: I,
deriveOptionsFromParent: P,
getTarget: W,
barcodeConstructor: E,
className: S,
renderAs: h,
...l
} = e;
if (this.barcodeInstance !== null) {
const p = g(l, d);
(c || "svg") !== (h || "svg") ? (this.barcodeInstance !== null && (this.barcodeInstance.destroy(), this.barcodeInstance = null), this.instantiateCoreBarcode()) : p && this.refresh(), this.trigger("render", { sender: this });
}
}
/**
* @hidden
*/
componentWillUnmount() {
this.barcodeInstance !== null && (this.barcodeInstance.destroy(), this.barcodeInstance = null), window.removeEventListener("resize", this.onWindowResize);
}
/**
* @hidden
*/
render() {
const { style: e = {}, className: r, children: t } = this.props;
return /* @__PURE__ */ n.createElement(u.Provider, { value: this.contextValue }, /* @__PURE__ */ n.createElement("div", { style: e, ref: (s) => this._element = s, className: r }, t, this.showLicenseWatermark && /* @__PURE__ */ n.createElement(f, null)));
}
/**
* @hidden
*/
instantiateCoreBarcode() {
const { barcodeConstructor: e } = this.props, r = this.getBarcodeOptions();
this.barcodeInstance = new e(this.element, r, (t) => {
if (process.env.NODE_ENV !== "production")
throw t;
console.warn(t);
});
}
/**
* @hidden
*/
refresh() {
if (this.barcodeInstance !== null) {
const e = this.getBarcodeOptions();
this.barcodeInstance.setOptions(e);
}
}
/**
* @hidden
*/
getBarcodeOptions() {
const { renderAs: e, deriveOptionsFromParent: r } = this.props;
let t = { renderAs: e };
return r && (t = r(t)), t;
}
/**
* @hidden
*/
trigger(e, r) {
const t = this.observersStore.getState();
for (let s = 0; s < t.length; s++)
t[s].trigger(e, r);
}
};
o.propTypes = {
dir: i.string,
renderAs: i.oneOf(["svg", "canvas"])
}, o.defaultProps = {
renderAs: "svg"
};
let a = o;
export {
a as BaseBarcode
};