@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
214 lines • 9.17 kB
JavaScript
/**
*
* 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/>.
*/
import { z } from 'zod';
import { DatetimeLiteralSchema, LocalDatetimeLiteralSchema, TimeLiteralSchema, } from '../../_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. */
export const DataGridStyleRuleLiteralSchema = z.union([
z.string(),
z.number(),
z.boolean(),
z.null(),
]);
/** A reference to a column whose cell value is resolved at evaluation time. */
export const DataGridColumnRefSchema = z.object({ column: 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.
*/
export const DataGridDatetimeLiteralSchema = 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.
*/
export const DataGridLocalDatetimeLiteralSchema = 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"`
*/
export const DataGridTimeLiteralSchema = 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. */
export const DataGridStyleRuleOperandSchema = z.union([
DataGridColumnRefSchema,
DataGridDatetimeLiteralSchema,
DataGridLocalDatetimeLiteralSchema,
DataGridTimeLiteralSchema,
DataGridStyleRuleLiteralSchema,
]);
export const DataGridStyleRuleWhereSchema = z.lazy(() => z.union([
z.object({ not: DataGridStyleRuleWhereSchema }),
z.object({ and: z.array(DataGridStyleRuleWhereSchema) }),
z.object({ or: z.array(DataGridStyleRuleWhereSchema) }),
z.object({
equal: z.tuple([
DataGridStyleRuleOperandSchema,
DataGridStyleRuleOperandSchema,
]),
}),
z.object({
lessThan: z.tuple([
DataGridStyleRuleOperandSchema,
DataGridStyleRuleOperandSchema,
]),
}),
z.object({
lessThanOrEqual: z.tuple([
DataGridStyleRuleOperandSchema,
DataGridStyleRuleOperandSchema,
]),
}),
z.object({
greaterThan: z.tuple([
DataGridStyleRuleOperandSchema,
DataGridStyleRuleOperandSchema,
]),
}),
z.object({
greaterThanOrEqual: z.tuple([
DataGridStyleRuleOperandSchema,
DataGridStyleRuleOperandSchema,
]),
}),
z.object({
contains: z.tuple([
DataGridStyleRuleOperandSchema,
DataGridStyleRuleOperandSchema,
]),
}),
z.object({
startsWith: z.tuple([
DataGridStyleRuleOperandSchema,
DataGridStyleRuleOperandSchema,
]),
}),
z.object({
endsWith: z.tuple([
DataGridStyleRuleOperandSchema,
DataGridStyleRuleOperandSchema,
]),
}),
z.object({ isNull: 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.
*/
export const DataGridColorRangeSchema = z.object({
/** Disable this color range without removing it. */
isDisabled: z.boolean().optional(),
/** Hex color (#rrggbb) at the maximum value. */
maxColor: z.string(),
/** The high end of the numeric range. Values above this clamp to maxColor. */
maxValue: z.number(),
/** Hex color (#rrggbb) at the optional mid-point for a 3-stop gradient. */
midColor: z.string().optional(),
/** Optional inflection point between minValue and maxValue for a 3-stop gradient. */
midValue: z.number().optional(),
/** Hex color (#rrggbb) at the minimum value. */
minColor: z.string(),
/** The low end of the numeric range. Values below this clamp to minColor. */
minValue: z.number(),
/** The column whose numeric value drives the interpolation. */
onColumn: z.string(),
});
/** A disable-able static color value. */
export const DataGridDisableableColorSchema = z.object({
/** Disable this style without removing it. */
isDisabled: z.boolean().optional(),
/** The CSS color value (e.g. '#fee2e2'). */
value: z.string(),
});
/** Visual properties that a style rule can apply to a cell or row. */
export const DataGridCellStyleSchema = z.object({
/** CSS background color / cell fill (e.g. '#fee2e2'). Matches the graph `Style.color`. */
color: DataGridDisableableColorSchema.optional(),
/** Gradient color interpolation applied to the cell fill. Matches the graph `Style.colorRange`. */
colorRange: DataGridColorRangeSchema.optional(),
/** CSS text color (e.g. '#991b1b'). Grid-only; no graph equivalent. */
textColor: DataGridDisableableColorSchema.optional(),
/** Gradient color interpolation applied to the text color. Grid-only; no graph equivalent. */
textColorRange: 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 = z.record(z.string(), z.unknown());
/** A single declarative rule that conditionally applies styles to DataGrid cells. */
export const DataGridStyleRuleSchema = z.object({
/** Style properties to apply when the rule matches. */
apply: DataGridCellStyleSchema,
/** Which column this rule targets. Set to null to apply styles to all columns (row-level effect). */
column: z.string().nullable(),
/** Disable this rule without removing it. */
isDisabled: 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: z.number().nonnegative().optional(),
/** Condition to evaluate. If omitted, rule always applies to matched column(s). */
where: 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(),
* });
* ```
*/
export const isDataGridStyleRule = (value) => DataGridStyleRuleSchema.safeParse(value).success;
//# sourceMappingURL=types.js.map