@lowdefy/blocks-aggrid
Version:
AgGrid Blocks for Lowdefy.
120 lines (116 loc) • 4.36 kB
JavaScript
/*
Copyright 2020-2026 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ import React from 'react';
import { type } from '@lowdefy/helpers';
import NullCell from './NullCell.js';
import { resolvePath } from './resolveFieldRefs.js';
const ANTD_TAG_COLOR_TOKENS = {
red: 'var(--ant-color-error)',
volcano: 'var(--ant-color-volcano, var(--ant-color-error))',
orange: 'var(--ant-color-warning)',
gold: 'var(--ant-color-gold, var(--ant-color-warning))',
yellow: 'var(--ant-color-warning)',
lime: 'var(--ant-color-lime, var(--ant-color-success))',
green: 'var(--ant-color-success)',
cyan: 'var(--ant-color-cyan, var(--ant-color-info))',
blue: 'var(--ant-color-info)',
geekblue: 'var(--ant-color-geekblue, var(--ant-color-info))',
purple: 'var(--ant-color-purple, var(--ant-color-info))',
magenta: 'var(--ant-color-magenta, var(--ant-color-error))',
default: 'var(--ant-color-text-secondary)'
};
const SEED_PALETTE = [
'red',
'volcano',
'orange',
'gold',
'yellow',
'lime',
'green',
'cyan',
'blue',
'geekblue',
'purple',
'magenta'
];
function colorSeed(s) {
if (type.isNone(s)) return 0;
const str = String(s);
let hash = 0;
for(let i = 0; i < str.length; i += 1)hash = hash * 31 + str.charCodeAt(i) >>> 0;
return hash;
}
function seededColor(item) {
return SEED_PALETTE[colorSeed(item) % SEED_PALETTE.length];
}
function resolveColor(value) {
if (type.isNone(value)) return ANTD_TAG_COLOR_TOKENS.default;
return ANTD_TAG_COLOR_TOKENS[value] ?? value;
}
function tagStyle(resolved) {
return {
display: 'inline-flex',
alignItems: 'center',
padding: 'var(--ant-padding-xxs, 4px) var(--ant-padding-xs, 8px)',
borderRadius: 'var(--ant-border-radius-sm, 4px)',
fontSize: 'var(--ant-font-size-sm, 12px)',
fontWeight: 600,
lineHeight: 1,
color: resolved,
background: `color-mix(in srgb, ${resolved} 12%, transparent)`,
border: `1px solid color-mix(in srgb, ${resolved} 30%, transparent)`
};
}
function TagCell(params) {
const { value, data, cellConfig } = params;
if (type.isNone(value) || value === '') {
return /*#__PURE__*/ React.createElement(NullCell, null);
}
const { colorMap, colorFrom, default: defaultColor } = cellConfig ?? {};
const useColorFrom = type.isString(colorFrom);
const useColorMap = type.isObject(colorMap);
const fromColor = useColorFrom ? resolvePath(colorFrom, data) : undefined;
const seedingActive = !useColorFrom && !useColorMap && type.isNone(defaultColor);
function colorFor(item) {
if (useColorFrom) return fromColor;
if (useColorMap) return colorMap[item];
return undefined;
}
function pickColor(item) {
return colorFor(item) ?? defaultColor ?? (seedingActive ? seededColor(item) : undefined);
}
if (type.isArray(value)) {
const items = value.filter((item)=>!type.isNone(item) && item !== '');
if (items.length === 0) {
return /*#__PURE__*/ React.createElement(NullCell, null);
}
const containerStyle = {
display: 'inline-flex',
flexWrap: 'wrap',
gap: 4
};
return /*#__PURE__*/ React.createElement("span", {
style: containerStyle
}, items.map((item, index)=>{
const resolved = resolveColor(pickColor(item));
return /*#__PURE__*/ React.createElement("span", {
key: `${index}-${item}`,
style: tagStyle(resolved)
}, String(item));
}));
}
const resolved = resolveColor(pickColor(value));
return /*#__PURE__*/ React.createElement("span", {
style: tagStyle(resolved)
}, String(value));
}
export default TagCell;