Files
data-fetch-plugin/vue-project/my-kintone-plugin/src/js/conditions.ts
2025-01-22 23:50:54 +08:00

58 lines
2.0 KiB
TypeScript

import type { FieldsInfo } from '@/types/model';
import type { FieldType } from './kintone-rest-api-client';
// conditionValue = '' | 'eq' | 'ne'
// conditionItem = { value: 'eq', label: '=(等しい)', type: 'input', func: (a: string, b: string) => a === b }
// = conditionMap[conditionValue]
export type ConditionValue = '' | 'eq' | 'ne' | 'test';
type ConditionItem = {
value: ConditionValue;
label: string;
type: ComponentType;
func: (a: string, b: string) => boolean;
};
export const conditionList: ConditionItem[] = [
{ value: '', label: '--------', type: '', func: (a: string, b: string) => true },
{ value: 'eq', label: '=(等しい)', type: 'input', func: (a: string, b: string) => a === b },
{ value: 'ne', label: '≠ (等しくない)', type: 'input', func: (a: string, b: string) => a !== b },
{ value: 'test', label: 'test combobox', type: 'select', func: (a: string, b: string) => a < b },
];
// search from conditionList
// conditionItem = conditionMap[conditionValue]
export const conditionMap: Record<ConditionValue, ConditionItem> = conditionList.reduce(
(map, item) => {
map[item.value] = item;
return map;
},
{} as Record<ConditionValue, ConditionItem>,
);
type FieldConditions = Partial<Record<FieldType, ConditionValue[]>>;
// FieldType -> ConditionValue[]
const fieldConditions: FieldConditions = {
SINGLE_LINE_TEXT: ['eq', 'ne'],
NUMBER: ['ne', 'test'],
} as const;
// fieldCode -> conditionList: ConditionItem[]
export const getAvailableCondition = (fieldCode: string, { fields }: FieldsInfo) => {
if (!fieldCode || !fields) return;
const conditions = fieldConditions[fields[fieldCode]?.type] || [];
return conditions.map((condition) => conditionMap[condition]);
};
const component = {
'': undefined,
input: 'kuc-text',
select: 'kuc-combobox',
};
export type ComponentType = keyof typeof component;
export const getComponent = (value: ConditionValue) => {
if (!value) return;
return component[conditionMap[value].type];
};