@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
218 lines • 10.3 kB
JavaScript
"use strict";
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDataGridStyleRule = exports.DataGridStyleRuleSchema = exports.DataGridCellStyleSchema = exports.DataGridDisableableColorSchema = exports.DataGridColorRangeSchema = exports.DataGridStyleRuleWhereSchema = exports.DataGridStyleRuleOperandSchema = exports.DataGridTimeLiteralSchema = exports.DataGridLocalDatetimeLiteralSchema = exports.DataGridDatetimeLiteralSchema = exports.DataGridColumnRefSchema = exports.DataGridStyleRuleLiteralSchema = void 0;
const zod_1 = require("zod");
const style_rules_1 = require("../../_generated/style-rules");
/*
* Style types defined in Zod, so that we can use the schemas to generate JSON Schema.
* This is useful for generating documentation, importing and for future integration into editors.
*/
/** A scalar literal that can appear as an operand in a style-rule where clause. */
exports.DataGridStyleRuleLiteralSchema = zod_1.z.union([
zod_1.z.string(),
zod_1.z.number(),
zod_1.z.boolean(),
zod_1.z.null(),
]);
/** A reference to a column whose cell value is resolved at evaluation time. */
exports.DataGridColumnRefSchema = zod_1.z.object({ column: zod_1.z.string() });
/**
* An ISO 8601 or RFC 9557 datetime literal that resolves to a Unix millisecond
* timestamp at evaluation time. Wraps the shared `DatetimeLiteralSchema` with a
* DataGrid-specific name. Supported formats:
* - RFC 9557 with IANA timezone: `"2024-06-15T14:00:00[Europe/Stockholm]"` (offset
* inferred) or `"2024-06-15T14:00:00+02:00[Europe/Stockholm]"` (offset explicit)
* - UTC or offset datetime: `"2024-01-01T00:00:00Z"`, `"2024-01-01T00:00:00+02:00"`
* - Plain datetime (treated as UTC): `"2024-01-01T14:30:00"`
* - Date-only (treated as UTC midnight): `"2024-01-01"`
*
* Unzoned datetimes without `Z` or an offset are treated as UTC wall-clock time.
*/
exports.DataGridDatetimeLiteralSchema = style_rules_1.DatetimeLiteralSchema;
/**
* A plain (unzoned) ISO 8601 date or datetime literal that resolves to a Unix
* millisecond timestamp in the browser's local timezone at evaluation time.
* Wraps the shared `LocalDatetimeLiteralSchema` with a DataGrid-specific name.
*
* Only plain date/datetime strings without timezone qualifiers are accepted:
* - Plain datetime: `"2024-01-01T14:30:00"`
* - Date-only (treated as local midnight): `"2024-01-01"`
*
* Strings with `Z`, a UTC offset, or an IANA bracket are rejected — use
* `{ datetime }` for timezone-aware instants.
*/
exports.DataGridLocalDatetimeLiteralSchema = style_rules_1.LocalDatetimeLiteralSchema;
/**
* An ISO 8601 time literal that resolves to milliseconds since UTC midnight at
* evaluation time. Wraps the shared `TimeLiteralSchema` with a DataGrid-specific
* name. Supported formats:
* - Local time (treated as UTC): `"14:30:00"`, `"14:30:00.500"`
* - Offset time (UTC-normalised): `"14:30:00+02:00"`
*/
exports.DataGridTimeLiteralSchema = style_rules_1.TimeLiteralSchema;
/** An operand in a style-rule where clause -- either a literal value, a datetime literal, a localdatetime literal, a time literal, or a column reference. */
exports.DataGridStyleRuleOperandSchema = zod_1.z.union([
exports.DataGridColumnRefSchema,
exports.DataGridDatetimeLiteralSchema,
exports.DataGridLocalDatetimeLiteralSchema,
exports.DataGridTimeLiteralSchema,
exports.DataGridStyleRuleLiteralSchema,
]);
exports.DataGridStyleRuleWhereSchema = zod_1.z.lazy(() => zod_1.z.union([
zod_1.z.object({ not: exports.DataGridStyleRuleWhereSchema }),
zod_1.z.object({ and: zod_1.z.array(exports.DataGridStyleRuleWhereSchema) }),
zod_1.z.object({ or: zod_1.z.array(exports.DataGridStyleRuleWhereSchema) }),
zod_1.z.object({
equal: zod_1.z.tuple([
exports.DataGridStyleRuleOperandSchema,
exports.DataGridStyleRuleOperandSchema,
]),
}),
zod_1.z.object({
lessThan: zod_1.z.tuple([
exports.DataGridStyleRuleOperandSchema,
exports.DataGridStyleRuleOperandSchema,
]),
}),
zod_1.z.object({
lessThanOrEqual: zod_1.z.tuple([
exports.DataGridStyleRuleOperandSchema,
exports.DataGridStyleRuleOperandSchema,
]),
}),
zod_1.z.object({
greaterThan: zod_1.z.tuple([
exports.DataGridStyleRuleOperandSchema,
exports.DataGridStyleRuleOperandSchema,
]),
}),
zod_1.z.object({
greaterThanOrEqual: zod_1.z.tuple([
exports.DataGridStyleRuleOperandSchema,
exports.DataGridStyleRuleOperandSchema,
]),
}),
zod_1.z.object({
contains: zod_1.z.tuple([
exports.DataGridStyleRuleOperandSchema,
exports.DataGridStyleRuleOperandSchema,
]),
}),
zod_1.z.object({
startsWith: zod_1.z.tuple([
exports.DataGridStyleRuleOperandSchema,
exports.DataGridStyleRuleOperandSchema,
]),
}),
zod_1.z.object({
endsWith: zod_1.z.tuple([
exports.DataGridStyleRuleOperandSchema,
exports.DataGridStyleRuleOperandSchema,
]),
}),
zod_1.z.object({ isNull: exports.DataGridStyleRuleOperandSchema }),
]));
/**
* Gradient coloring based on a continuous numeric column value.
* Colors should be hex strings (#rrggbb).
* If the column value is missing or non-numeric, the colorRange is ignored.
*/
exports.DataGridColorRangeSchema = zod_1.z.object({
/** Disable this color range without removing it. */
isDisabled: zod_1.z.boolean().optional(),
/** Hex color (#rrggbb) at the maximum value. */
maxColor: zod_1.z.string(),
/** The high end of the numeric range. Values above this clamp to maxColor. */
maxValue: zod_1.z.number(),
/** Hex color (#rrggbb) at the optional mid-point for a 3-stop gradient. */
midColor: zod_1.z.string().optional(),
/** Optional inflection point between minValue and maxValue for a 3-stop gradient. */
midValue: zod_1.z.number().optional(),
/** Hex color (#rrggbb) at the minimum value. */
minColor: zod_1.z.string(),
/** The low end of the numeric range. Values below this clamp to minColor. */
minValue: zod_1.z.number(),
/** The column whose numeric value drives the interpolation. */
onColumn: zod_1.z.string(),
});
/** A disable-able static color value. */
exports.DataGridDisableableColorSchema = zod_1.z.object({
/** Disable this style without removing it. */
isDisabled: zod_1.z.boolean().optional(),
/** The CSS color value (e.g. '#fee2e2'). */
value: zod_1.z.string(),
});
/** Visual properties that a style rule can apply to a cell or row. */
exports.DataGridCellStyleSchema = zod_1.z.object({
/** CSS background color / cell fill (e.g. '#fee2e2'). Matches the graph `Style.color`. */
color: exports.DataGridDisableableColorSchema.optional(),
/** Gradient color interpolation applied to the cell fill. Matches the graph `Style.colorRange`. */
colorRange: exports.DataGridColorRangeSchema.optional(),
/** CSS text color (e.g. '#991b1b'). Grid-only; no graph equivalent. */
textColor: exports.DataGridDisableableColorSchema.optional(),
/** Gradient color interpolation applied to the text color. Grid-only; no graph equivalent. */
textColorRange: exports.DataGridColorRangeSchema.optional(),
});
/**
* metaData is an free-form object that can be used to store additional information about a style rule.
* The data is not taken into account when evaluating the rule.
*/
const metaDataSchema = zod_1.z.record(zod_1.z.string(), zod_1.z.unknown());
/** A single declarative rule that conditionally applies styles to DataGrid cells. */
exports.DataGridStyleRuleSchema = zod_1.z.object({
/** Style properties to apply when the rule matches. */
apply: exports.DataGridCellStyleSchema,
/** Which column this rule targets. Set to null to apply styles to all columns (row-level effect). */
column: zod_1.z.string().nullable(),
/** Disable this rule without removing it. */
isDisabled: zod_1.z.boolean().optional(),
/** Additional metadata about the rule. */
metaData: metaDataSchema.optional(),
/** Higher priority rules are applied later (override lower). Must be >= 0; negative values are reserved for internal use. Rules without explicit priority preserve their array order but are always applied before rules with explicit priority. */
priority: zod_1.z.number().nonnegative().optional(),
/** Condition to evaluate. If omitted, rule always applies to matched column(s). */
where: exports.DataGridStyleRuleWhereSchema.optional(),
});
/**
* Runtime type guard for {@link DataGridStyleRule}, backed by the internal Zod
* schema. Narrows an `unknown` value to `DataGridStyleRule` when it is a valid
* style rule.
*
* This lets consumers validate values without taking a dependency on Zod or on
* our schema export. For example, it can be passed to another validation
* library's custom check:
*
* @example
* ```ts
* import { z } from 'zod';
* import { isDataGridStyleRule, type DataGridStyleRule } from '@neo4j-ndl/react';
*
* const formSchema = z.object({
* rule: z.custom<DataGridStyleRule>(isDataGridStyleRule),
* notes: z.string(),
* });
* ```
*/
const isDataGridStyleRule = (value) => exports.DataGridStyleRuleSchema.safeParse(value).success;
exports.isDataGridStyleRule = isDataGridStyleRule;
//# sourceMappingURL=types.js.map