@ya-grid/core 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -65,7 +65,7 @@ var ColumnController = class {
65
65
  });
66
66
  this.columns = [...system, ...ordered];
67
67
  }
68
- setColumnDefs(columnDefs, defaultColDef, selectionConfig, masterDetail = false) {
68
+ setColumnDefs(columnDefs, defaultColDef, selectionConfig, masterDetail = false, columnTypes) {
69
69
  const usedIds = /* @__PURE__ */ new Set();
70
70
  const resolved = [];
71
71
  if (selectionConfig?.checkboxes) {
@@ -112,6 +112,22 @@ var ColumnController = class {
112
112
  }
113
113
  let leafIdx = 0;
114
114
  let groupCounter = 0;
115
+ const resolveTypeDefs = (typeSpec, visited = /* @__PURE__ */ new Set()) => {
116
+ if (!typeSpec || !columnTypes) return {};
117
+ const names = Array.isArray(typeSpec) ? typeSpec : [typeSpec];
118
+ let acc = {};
119
+ for (const name of names) {
120
+ if (visited.has(name)) continue;
121
+ const preset = columnTypes[name];
122
+ if (!preset) continue;
123
+ visited.add(name);
124
+ if (preset.type) {
125
+ acc = { ...acc, ...resolveTypeDefs(preset.type, visited) };
126
+ }
127
+ acc = { ...acc, ...preset };
128
+ }
129
+ return acc;
130
+ };
115
131
  const visit = (items, groupPath) => {
116
132
  for (const item of items) {
117
133
  if (isColGroupDef(item)) {
@@ -123,8 +139,10 @@ var ColumnController = class {
123
139
  visit(item.children, [...groupPath, groupMeta]);
124
140
  continue;
125
141
  }
142
+ const typeDef = resolveTypeDefs(item.type ?? defaultColDef?.type);
126
143
  const merged = {
127
144
  ...defaultColDef,
145
+ ...typeDef,
128
146
  ...item
129
147
  };
130
148
  const baseId = merged.colId ?? merged.field ?? `col_${leafIdx}`;
@@ -457,6 +475,399 @@ var ColumnController = class {
457
475
  }
458
476
  };
459
477
 
478
+ // src/columns/column-types.ts
479
+ var VANILLA_RENDERER_SYMBOL = /* @__PURE__ */ Symbol.for("ya-grid.vanilla-renderer");
480
+ function markVanillaRenderer(fn) {
481
+ Object.defineProperty(fn, VANILLA_RENDERER_SYMBOL, { value: true, configurable: true });
482
+ return fn;
483
+ }
484
+ function toFiniteNumber(value) {
485
+ if (typeof value === "number") return Number.isFinite(value) ? value : null;
486
+ if (typeof value === "string" && value.trim() !== "") {
487
+ const n = Number(value);
488
+ return Number.isFinite(n) ? n : null;
489
+ }
490
+ return null;
491
+ }
492
+ function signClassFor(value, colorizeSign) {
493
+ const classes = ["ya-col-numeric"];
494
+ if (!colorizeSign) return classes;
495
+ const n = toFiniteNumber(value);
496
+ if (n === null) return classes;
497
+ if (n > 0) classes.push("ya-num-pos");
498
+ else if (n < 0) classes.push("ya-num-neg");
499
+ else classes.push("ya-num-zero");
500
+ return classes;
501
+ }
502
+ function currencyColumn(options = {}) {
503
+ const {
504
+ locale = "en-US",
505
+ currency = "USD",
506
+ minimumFractionDigits = 0,
507
+ maximumFractionDigits = options.compact ? 1 : 2,
508
+ currencySign = "standard",
509
+ signDisplay = "auto",
510
+ compact = false,
511
+ colorizeSign = false,
512
+ align = "right"
513
+ } = options;
514
+ const formatter = new Intl.NumberFormat(locale, {
515
+ style: "currency",
516
+ currency,
517
+ minimumFractionDigits,
518
+ maximumFractionDigits,
519
+ currencySign,
520
+ signDisplay,
521
+ notation: compact ? "compact" : "standard"
522
+ });
523
+ return {
524
+ dataType: "number",
525
+ filter: "number",
526
+ cellEditor: "number",
527
+ align,
528
+ cellClass: ({ value }) => signClassFor(value, colorizeSign),
529
+ valueFormatter: ({ value }) => {
530
+ const n = toFiniteNumber(value);
531
+ return n === null ? "" : formatter.format(n);
532
+ }
533
+ };
534
+ }
535
+ function percentColumn(options = {}) {
536
+ const {
537
+ scale = "percent",
538
+ locale = "en-US",
539
+ minimumFractionDigits = 0,
540
+ maximumFractionDigits = 1,
541
+ signDisplay = "auto",
542
+ colorizeSign = false,
543
+ align = "right"
544
+ } = options;
545
+ const formatter = new Intl.NumberFormat(locale, {
546
+ style: "percent",
547
+ minimumFractionDigits,
548
+ maximumFractionDigits,
549
+ signDisplay
550
+ });
551
+ return {
552
+ dataType: "number",
553
+ filter: "number",
554
+ cellEditor: "number",
555
+ align,
556
+ cellClass: ({ value }) => signClassFor(value, colorizeSign),
557
+ valueFormatter: ({ value }) => {
558
+ const n = toFiniteNumber(value);
559
+ if (n === null) return "";
560
+ return formatter.format(scale === "percent" ? n / 100 : n);
561
+ }
562
+ };
563
+ }
564
+ function barColumn(options = {}) {
565
+ const {
566
+ min = 0,
567
+ max = 100,
568
+ showValue = true,
569
+ valueFormatter,
570
+ color,
571
+ thresholds,
572
+ height = 6
573
+ } = options;
574
+ const formatNum = (n) => {
575
+ if (valueFormatter) return valueFormatter(n);
576
+ if (min === 0 && max === 100) return `${Math.round(n)}%`;
577
+ return String(n);
578
+ };
579
+ const resolveTone = (n) => {
580
+ if (!thresholds) return "accent";
581
+ const { warn, danger, invert } = thresholds;
582
+ if (invert) {
583
+ if (danger !== void 0 && n <= danger) return "danger";
584
+ if (warn !== void 0 && n <= warn) return "warning";
585
+ return "success";
586
+ }
587
+ if (danger !== void 0 && n >= danger) return "danger";
588
+ if (warn !== void 0 && n >= warn) return "warning";
589
+ return "success";
590
+ };
591
+ const renderer = markVanillaRenderer(
592
+ ({ value, formattedValue }) => {
593
+ const n = toFiniteNumber(value);
594
+ if (n === null) return "";
595
+ const span = max - min;
596
+ const pct = span > 0 ? Math.min(100, Math.max(0, (n - min) / span * 100)) : 0;
597
+ const wrap = document.createElement("div");
598
+ wrap.className = "ya-bar-cell";
599
+ const track = document.createElement("div");
600
+ track.className = "ya-bar-track";
601
+ track.setAttribute("role", "progressbar");
602
+ track.setAttribute("aria-valuemin", String(min));
603
+ track.setAttribute("aria-valuemax", String(max));
604
+ track.setAttribute("aria-valuenow", String(n));
605
+ if (height !== 6) track.style.height = `${height}px`;
606
+ const fill = document.createElement("div");
607
+ fill.className = "ya-bar-fill";
608
+ fill.dataset.tone = resolveTone(n);
609
+ fill.style.width = `${pct.toFixed(1)}%`;
610
+ if (color) fill.style.backgroundColor = color;
611
+ track.appendChild(fill);
612
+ wrap.appendChild(track);
613
+ if (showValue) {
614
+ const label = document.createElement("span");
615
+ label.className = "ya-bar-value";
616
+ label.textContent = formattedValue || formatNum(n);
617
+ wrap.appendChild(label);
618
+ }
619
+ return wrap;
620
+ }
621
+ );
622
+ return {
623
+ dataType: "number",
624
+ filter: "number",
625
+ cellEditor: "number",
626
+ valueFormatter: ({ value }) => {
627
+ const n = toFiniteNumber(value);
628
+ return n === null ? "" : formatNum(n);
629
+ },
630
+ cellRenderer: renderer
631
+ };
632
+ }
633
+ function badgeColumn(options = {}) {
634
+ const { map = {}, defaultTone = "neutral", dot = false } = options;
635
+ const renderer = markVanillaRenderer(
636
+ ({ value, formattedValue }) => {
637
+ if (value === null || value === void 0 || value === "") return "";
638
+ const key = String(value);
639
+ const entry = map[key];
640
+ const cfg = typeof entry === "string" ? { tone: entry } : entry ?? {};
641
+ const tone = cfg.tone ?? defaultTone;
642
+ const text = cfg.label ?? (formattedValue || key);
643
+ const pill = document.createElement("span");
644
+ pill.className = "ya-badge";
645
+ pill.dataset.tone = tone;
646
+ if (cfg.bg) pill.style.backgroundColor = cfg.bg;
647
+ if (cfg.color) pill.style.color = cfg.color;
648
+ if (dot) {
649
+ const dotEl = document.createElement("span");
650
+ dotEl.className = "ya-badge-dot";
651
+ dotEl.setAttribute("aria-hidden", "true");
652
+ pill.appendChild(dotEl);
653
+ }
654
+ pill.appendChild(document.createTextNode(text));
655
+ return pill;
656
+ }
657
+ );
658
+ return {
659
+ filter: "set",
660
+ cellRenderer: renderer
661
+ };
662
+ }
663
+ var SVG_NS = "http://www.w3.org/2000/svg";
664
+ function booleanColumn(options = {}) {
665
+ const { style = "icon", trueLabel = "Yes", falseLabel = "No" } = options;
666
+ const renderer = markVanillaRenderer(
667
+ ({ value }) => {
668
+ if (value === null || value === void 0) return "";
669
+ const truthy = Boolean(value);
670
+ const label = truthy ? trueLabel : falseLabel;
671
+ if (style === "text") {
672
+ const span = document.createElement("span");
673
+ span.className = truthy ? "ya-bool-text ya-bool-true" : "ya-bool-text ya-bool-false";
674
+ span.textContent = label;
675
+ return span;
676
+ }
677
+ if (style === "pill") {
678
+ const pill = document.createElement("span");
679
+ pill.className = "ya-badge";
680
+ pill.dataset.tone = truthy ? "success" : "neutral";
681
+ pill.textContent = label;
682
+ return pill;
683
+ }
684
+ const wrap = document.createElement("span");
685
+ wrap.className = truthy ? "ya-bool-icon ya-bool-true" : "ya-bool-icon ya-bool-false";
686
+ wrap.setAttribute("role", "img");
687
+ wrap.setAttribute("aria-label", label);
688
+ wrap.title = label;
689
+ const svg = document.createElementNS(SVG_NS, "svg");
690
+ svg.setAttribute("width", "15");
691
+ svg.setAttribute("height", "15");
692
+ svg.setAttribute("viewBox", "0 0 16 16");
693
+ svg.setAttribute("fill", "none");
694
+ svg.setAttribute("aria-hidden", "true");
695
+ const path = document.createElementNS(SVG_NS, "path");
696
+ path.setAttribute("d", truthy ? "M3.5 8.5 6.5 11.5 12.5 4.5" : "M4.5 8h7");
697
+ path.setAttribute("stroke", "currentColor");
698
+ path.setAttribute("stroke-width", "2");
699
+ path.setAttribute("stroke-linecap", "round");
700
+ path.setAttribute("stroke-linejoin", "round");
701
+ svg.appendChild(path);
702
+ wrap.appendChild(svg);
703
+ return wrap;
704
+ }
705
+ );
706
+ return {
707
+ dataType: "boolean",
708
+ align: "center",
709
+ filter: "set",
710
+ cellEditor: "checkbox",
711
+ valueFormatter: ({ value }) => {
712
+ if (value === null || value === void 0) return "";
713
+ return value ? trueLabel : falseLabel;
714
+ },
715
+ cellRenderer: renderer
716
+ };
717
+ }
718
+ function extractSeries(value) {
719
+ if (!Array.isArray(value)) return [];
720
+ const nums = [];
721
+ for (const item of value) {
722
+ const n = toFiniteNumber(item);
723
+ if (n !== null) nums.push(n);
724
+ }
725
+ return nums;
726
+ }
727
+ function sparklineColumn(options = {}) {
728
+ const {
729
+ variant = "area",
730
+ width = 88,
731
+ height = 22,
732
+ strokeWidth = 1.6,
733
+ color,
734
+ trendColor = true,
735
+ showLastValue = false,
736
+ valueFormatter = (v) => String(Math.round(v * 10) / 10)
737
+ } = options;
738
+ const renderer = markVanillaRenderer(
739
+ ({ value }) => {
740
+ const series = extractSeries(value);
741
+ if (series.length === 0) return "";
742
+ const first = series[0];
743
+ const last = series[series.length - 1];
744
+ let minVal = first;
745
+ let maxVal = first;
746
+ for (let i = 1; i < series.length; i++) {
747
+ const v = series[i];
748
+ if (v < minVal) minVal = v;
749
+ if (v > maxVal) maxVal = v;
750
+ }
751
+ const trend = last > first ? "up" : last < first ? "down" : "flat";
752
+ const wrap = document.createElement("div");
753
+ wrap.className = "ya-sparkline-cell";
754
+ if (trendColor && !color) {
755
+ wrap.dataset.trend = trend;
756
+ }
757
+ if (color) {
758
+ wrap.style.color = color;
759
+ }
760
+ const svg = document.createElementNS(SVG_NS, "svg");
761
+ svg.setAttribute("class", "ya-sparkline-svg");
762
+ svg.setAttribute("width", String(width));
763
+ svg.setAttribute("height", String(height));
764
+ svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
765
+ svg.setAttribute("role", "img");
766
+ svg.setAttribute(
767
+ "aria-label",
768
+ `Trend: ${valueFormatter(first)} to ${valueFormatter(last)} (min ${valueFormatter(minVal)}, max ${valueFormatter(maxVal)})`
769
+ );
770
+ const padY = 2.5;
771
+ const innerH = Math.max(1, height - padY * 2);
772
+ const range = maxVal - minVal;
773
+ if (variant === "bar") {
774
+ const gap = 1.5;
775
+ const barW = Math.max(1.5, (width - gap * (series.length - 1)) / series.length);
776
+ const baseline = Math.min(minVal, 0);
777
+ const barSpan = Math.max(1e-6, maxVal - baseline);
778
+ for (let i = 0; i < series.length; i++) {
779
+ const v = series[i];
780
+ const h = Math.max(2, (v - baseline) / barSpan * innerH);
781
+ const rect = document.createElementNS(SVG_NS, "rect");
782
+ rect.setAttribute("x", (i * (barW + gap)).toFixed(1));
783
+ rect.setAttribute("y", (height - padY - h).toFixed(1));
784
+ rect.setAttribute("width", barW.toFixed(1));
785
+ rect.setAttribute("height", h.toFixed(1));
786
+ rect.setAttribute("rx", "1");
787
+ rect.setAttribute("fill", "currentColor");
788
+ rect.setAttribute("fill-opacity", i === series.length - 1 ? "1" : "0.68");
789
+ svg.appendChild(rect);
790
+ }
791
+ } else {
792
+ const padX = 2;
793
+ const innerW = Math.max(1, width - padX * 2);
794
+ const stepX = series.length > 1 ? innerW / (series.length - 1) : 0;
795
+ const points = series.map((v, i) => {
796
+ const x = series.length > 1 ? padX + i * stepX : width / 2;
797
+ const norm = range > 0 ? (v - minVal) / range : 0.5;
798
+ const y = padY + (1 - norm) * innerH;
799
+ return [x, y];
800
+ });
801
+ const linePath = points.map(([x, y], i) => `${i === 0 ? "M" : "L"}${x.toFixed(1)} ${y.toFixed(1)}`).join(" ");
802
+ if (variant === "area" && points.length > 1) {
803
+ const firstPt = points[0];
804
+ const lastPt2 = points[points.length - 1];
805
+ const area = document.createElementNS(SVG_NS, "path");
806
+ area.setAttribute(
807
+ "d",
808
+ `${linePath} L${lastPt2[0].toFixed(1)} ${(height - padY).toFixed(1)} L${firstPt[0].toFixed(1)} ${(height - padY).toFixed(1)} Z`
809
+ );
810
+ area.setAttribute("fill", "currentColor");
811
+ area.setAttribute("fill-opacity", "0.16");
812
+ svg.appendChild(area);
813
+ }
814
+ if (points.length > 1) {
815
+ const stroke = document.createElementNS(SVG_NS, "path");
816
+ stroke.setAttribute("d", linePath);
817
+ stroke.setAttribute("fill", "none");
818
+ stroke.setAttribute("stroke", "currentColor");
819
+ stroke.setAttribute("stroke-width", String(strokeWidth));
820
+ stroke.setAttribute("stroke-linecap", "round");
821
+ stroke.setAttribute("stroke-linejoin", "round");
822
+ svg.appendChild(stroke);
823
+ }
824
+ const lastPt = points[points.length - 1];
825
+ const dot = document.createElementNS(SVG_NS, "circle");
826
+ dot.setAttribute("cx", lastPt[0].toFixed(1));
827
+ dot.setAttribute("cy", lastPt[1].toFixed(1));
828
+ dot.setAttribute("r", "2.1");
829
+ dot.setAttribute("fill", "currentColor");
830
+ svg.appendChild(dot);
831
+ }
832
+ wrap.appendChild(svg);
833
+ if (showLastValue) {
834
+ const lbl = document.createElement("span");
835
+ lbl.className = "ya-sparkline-value";
836
+ lbl.textContent = valueFormatter(last);
837
+ wrap.appendChild(lbl);
838
+ }
839
+ return wrap;
840
+ }
841
+ );
842
+ return {
843
+ sortable: true,
844
+ filter: false,
845
+ comparator: (a, b) => {
846
+ const sa = extractSeries(a);
847
+ const sb = extractSeries(b);
848
+ const la = sa.length > 0 ? sa[sa.length - 1] : Number.NEGATIVE_INFINITY;
849
+ const lb = sb.length > 0 ? sb[sb.length - 1] : Number.NEGATIVE_INFINITY;
850
+ return la - lb;
851
+ },
852
+ valueFormatter: ({ value }) => {
853
+ const series = extractSeries(value);
854
+ return series.map(valueFormatter).join(", ");
855
+ },
856
+ cellRenderer: renderer
857
+ };
858
+ }
859
+ function createBuiltInColumnTypes() {
860
+ return {
861
+ currency: currencyColumn(),
862
+ accounting: currencyColumn({ currencySign: "accounting", colorizeSign: true }),
863
+ percent: percentColumn(),
864
+ bar: barColumn(),
865
+ badge: badgeColumn(),
866
+ boolean: booleanColumn(),
867
+ sparkline: sparklineColumn()
868
+ };
869
+ }
870
+
460
871
  // src/editing/undo-redo-stack.ts
461
872
  var UndoRedoStack = class {
462
873
  undoStack = [];
@@ -4762,7 +5173,8 @@ var YaGridCore = class {
4762
5173
  options.columnDefs,
4763
5174
  options.defaultColDef,
4764
5175
  this.selectionController.getConfig(),
4765
- Boolean(options.masterDetail)
5176
+ Boolean(options.masterDetail),
5177
+ options.columnTypes
4766
5178
  );
4767
5179
  if (options.initialState?.columns) {
4768
5180
  this.columnController.applyColumnState(options.initialState.columns);
@@ -5395,7 +5807,8 @@ var YaGridCore = class {
5395
5807
  columnDefs,
5396
5808
  this.options.defaultColDef,
5397
5809
  this.selectionController.getConfig(),
5398
- Boolean(this.options.masterDetail)
5810
+ Boolean(this.options.masterDetail),
5811
+ this.options.columnTypes
5399
5812
  );
5400
5813
  const w = this.renderer.getViewportWidth();
5401
5814
  if (w > 0) this.columnController.setViewportWidth(w);
@@ -5405,6 +5818,7 @@ var YaGridCore = class {
5405
5818
  const prevRowData = this.options.rowData;
5406
5819
  const prevColDefs = this.options.columnDefs;
5407
5820
  const prevDefaultColDef = this.options.defaultColDef;
5821
+ const prevColumnTypes = this.options.columnTypes;
5408
5822
  const prevSelection = this.options.rowSelection;
5409
5823
  const prevMasterDetail = this.options.masterDetail;
5410
5824
  const prevPagination = this.options.pagination;
@@ -5468,12 +5882,13 @@ var YaGridCore = class {
5468
5882
  this.selectionController.setConfig(normalizeSelectionConfig(partial.rowSelection));
5469
5883
  structuralChange = true;
5470
5884
  }
5471
- if (partial.columnDefs !== void 0 && partial.columnDefs !== prevColDefs || partial.defaultColDef !== void 0 && !shallowEqual(partial.defaultColDef, prevDefaultColDef) || partial.rowSelection !== void 0 && !shallowEqual(partial.rowSelection, prevSelection) || partial.masterDetail !== void 0 && partial.masterDetail !== prevMasterDetail) {
5885
+ if (partial.columnDefs !== void 0 && partial.columnDefs !== prevColDefs || partial.defaultColDef !== void 0 && !shallowEqual(partial.defaultColDef, prevDefaultColDef) || partial.columnTypes !== void 0 && !shallowEqual(partial.columnTypes, prevColumnTypes) || partial.rowSelection !== void 0 && !shallowEqual(partial.rowSelection, prevSelection) || partial.masterDetail !== void 0 && partial.masterDetail !== prevMasterDetail) {
5472
5886
  this.columnController.setColumnDefs(
5473
5887
  this.options.columnDefs,
5474
5888
  this.options.defaultColDef,
5475
5889
  this.selectionController.getConfig(),
5476
- Boolean(this.options.masterDetail)
5890
+ Boolean(this.options.masterDetail),
5891
+ this.options.columnTypes
5477
5892
  );
5478
5893
  const w = this.renderer.getViewportWidth();
5479
5894
  if (w > 0) this.columnController.setViewportWidth(w);
@@ -5862,7 +6277,8 @@ var YaGridCore = class {
5862
6277
  this.options.columnDefs,
5863
6278
  this.options.defaultColDef,
5864
6279
  this.selectionController.getConfig(),
5865
- Boolean(this.options.masterDetail)
6280
+ Boolean(this.options.masterDetail),
6281
+ this.options.columnTypes
5866
6282
  );
5867
6283
  const w = this.renderer.getViewportWidth();
5868
6284
  if (w > 0) this.columnController.setViewportWidth(w);
@@ -5997,20 +6413,29 @@ export {
5997
6413
  SELECTION_COL_ID,
5998
6414
  SelectionController,
5999
6415
  UndoRedoStack,
6416
+ VANILLA_RENDERER_SYMBOL,
6000
6417
  YaGridCore,
6418
+ badgeColumn,
6419
+ barColumn,
6420
+ booleanColumn,
6001
6421
  buildCsvString,
6002
6422
  buildQuickFilterHaystack,
6003
6423
  buildTsvString,
6424
+ createBuiltInColumnTypes,
6004
6425
  createGrid,
6426
+ currencyColumn,
6005
6427
  downloadCsvFile,
6006
6428
  filterRowNodes,
6007
6429
  getFormattedCellValue,
6008
6430
  getRawCellValue,
6009
6431
  isColGroupDef,
6432
+ markVanillaRenderer,
6010
6433
  matchesColumnFilter,
6011
6434
  normalizeSelectionConfig,
6012
6435
  parseQuickFilterTokens,
6436
+ percentColumn,
6013
6437
  resolveColumnFilterType,
6014
- sortRowNodes
6438
+ sortRowNodes,
6439
+ sparklineColumn
6015
6440
  };
6016
6441
  //# sourceMappingURL=index.js.map