import type { FieldsInfo } from '@/types/model'; import { isType, type FieldType, type OneOf } from './kintone-rest-api-client'; import { getFieldObj } from './helper'; // conditionValue = '' | 'eq' | 'ne' // conditionItem = { value: 'eq', label: '=(等しい)', type: 'input', func: (a: string, b: string) => a === b } // = conditionMap[conditionValue] export type ConditionValue = ""| "=" | "!=" | ">=" | "<=" | "<" | ">" | "like" | "not like" | "in" | "not in"; type ConditionItem = { value: ConditionValue; label: string | ((field: OneOf) => string); type: ComponentType; // | ((field: OneOf) => ComponentType); }; export const conditionList: ConditionItem[] = [ { value: '=', label: '=(等しい)', type: 'input' }, { value: '!=', label: '≠ (等しくない)', type: 'input' }, { value: '<=', label: (field) => isDateTimeType(field) ? '≦ (以前)' : '≦ (以下)', type: 'input' }, { value: '<', label: '< (より前)', type: 'input' }, { value: '>', label: '> (より後)', type: 'input' }, { value: '>=', label: (field) => isDateTimeType(field) ? '≧ (以降)' : '≧ (以上)', type: 'input' }, { value: 'like', label: '次のキーワードを含む', type: 'input' }, { value: 'not like', label:'次のキーワードを含まない', type: 'input' }, { value: 'in', label: '次のいずれかを含む', type: 'input' }, { value: 'not in', label: '次のいずれも含まない', type: 'input' }, ]; export const isDateTimeType = (field: OneOf) => { return isType.DATETIME(field) || isType.TIME(field) || isType.DATE(field) || isType.CREATED_TIME(field) || isType.UPDATED_TIME(field); } // search from conditionList // conditionItem = conditionMap[conditionValue] export const conditionMap: Record = conditionList.reduce( (map, item) => { map[item.value] = item; return map; }, {} as Record, ); type FieldConditions = Partial>; const textCondition: ConditionValue[] = ['=', '!=', 'in', 'like','not like']; const numberCondition: ConditionValue[] = ['=', '!=', '<=', '>=']; const timeCondition: ConditionValue[] = ['=', '!=', '<=', '>=', '<', '>']; const containsCondition: ConditionValue[] = ['in', 'not in']; // FieldType -> ConditionValue[] const fieldConditions: FieldConditions = { SINGLE_LINE_TEXT: textCondition, MULTI_LINE_TEXT: containsCondition, RICH_TEXT: containsCondition, NUMBER: numberCondition, CHECK_BOX: containsCondition, RADIO_BUTTON: containsCondition, DROP_DOWN: containsCondition, MULTI_SELECT: containsCondition, USER_SELECT: containsCondition, ORGANIZATION_SELECT: containsCondition, GROUP_SELECT: containsCondition, LINK: textCondition, CALC: numberCondition, TIME: timeCondition, DATE: timeCondition, DATETIME: timeCondition, CREATED_TIME: timeCondition, CREATOR: containsCondition, UPDATED_TIME: timeCondition, MODIFIER: containsCondition, RECORD_NUMBER: numberCondition, } as const; // fieldCode -> conditionList: ConditionItem[] export const getAvailableCondition = (fieldCode: string, fieldsInfo: FieldsInfo, subTableCode: string | '') => { if (!fieldCode || !fieldsInfo.fields) return; const fieldObj = getFieldObj(fieldCode, fieldsInfo, ''); if (!fieldObj) return; const conditions = fieldConditions[fieldObj.type] || textCondition; // TODO a fallback here return conditions.map((condition) => { const res = {...conditionMap[condition]} res.label = typeof res.label === 'function' ? res.label(fieldObj) : res.label; return res; }); }; const component = { input: 'kuc-text', select: 'kuc-combobox', time: 'time', date: 'date', datetime: 'datetime', }; const dateTimeComponent: Partial> = { TIME: 'time', DATE: 'date', DATETIME: 'datetime', CREATED_TIME: 'datetime', UPDATED_TIME: 'datetime', } export type ComponentType = keyof typeof component; export const getComponent = (value: ConditionValue) => { if (!value) return; return component[conditionMap[value].type]; };