Add condition part
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
import TableCombobox from '@/components/basic/condition/TableCombobox.vue';
|
||||
import TableInput from '@/components/basic/condition/TableInput.vue';
|
||||
import type { FieldsInfo } from '@/types/model';
|
||||
import type { FieldType } from './kintone-rest-api-client';
|
||||
|
||||
const component = {
|
||||
'': undefined,
|
||||
input: TableInput,
|
||||
select: TableCombobox,
|
||||
};
|
||||
export type ComponentType = keyof typeof component;
|
||||
|
||||
export type ConditionValue = '' | 'eq' | 'ne';
|
||||
// Condition
|
||||
export type ConditionValue = '' | 'eq' | 'ne' | 'test';
|
||||
|
||||
type ConditionItem = {
|
||||
value: ConditionValue;
|
||||
@@ -21,10 +15,9 @@ 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 },
|
||||
];
|
||||
|
||||
// type ConditionItem = (typeof conditionList)[number];
|
||||
|
||||
export const conditionMap: Record<ConditionValue, ConditionItem> = conditionList.reduce(
|
||||
(map, item) => {
|
||||
map[item.value] = item;
|
||||
@@ -33,6 +26,29 @@ export const conditionMap: Record<ConditionValue, ConditionItem> = conditionList
|
||||
{} as Record<ConditionValue, ConditionItem>,
|
||||
);
|
||||
|
||||
// Field Type -> Condition
|
||||
type FieldConditions = Partial<Record<FieldType, ConditionValue[]>>;
|
||||
|
||||
const fieldConditions: FieldConditions = {
|
||||
SINGLE_LINE_TEXT: ['eq', 'ne'],
|
||||
NUMBER: ['ne', 'test'],
|
||||
} as const;
|
||||
|
||||
export const getAvailableCondition = (fieldCode: string, { fields }: FieldsInfo) => {
|
||||
if (!fieldCode||!fields) return;
|
||||
const conditions = fieldConditions[fields[fieldCode]?.type] || [];
|
||||
return conditions.map((condition) => conditionMap[condition]);
|
||||
};
|
||||
|
||||
// Condition -> Component
|
||||
|
||||
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];
|
||||
};
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import type { FieldsInfo, JoinTable, WhereCondition } from '@/types/model';
|
||||
import { client, isType, type OneOf, type App, type Layout } from './kintone-rest-api-client';
|
||||
import { client, isType, type FieldType, type App, type Layout } from './kintone-rest-api-client';
|
||||
import type { DropdownItem } from 'kintone-ui-component';
|
||||
|
||||
|
||||
export const EMPTY_OPTION = {
|
||||
value: '',
|
||||
label: '--------',
|
||||
} as DropdownItem;
|
||||
|
||||
export const getEmptyWhereCondition = () => ({ field: '', condition: '', data: '' } as WhereCondition);
|
||||
export const getEmptyWhereCondition = () => ({ field: '', condition: '', data: '' }) as WhereCondition;
|
||||
export const getEmptyOnCondition = () => ({ leftField: '', rightField: '' });
|
||||
export const getEmptyFieldsMapping = () => ({ leftField: '', rightField: '' });
|
||||
|
||||
@@ -50,10 +49,10 @@ export const loadAppFieldsAndLayout = async (appId: string | number = kintone.ap
|
||||
} as FieldsInfo;
|
||||
};
|
||||
|
||||
type Param = { subTableCode?: string; filterType?: FieldType; defaultLabel?: string };
|
||||
export const getFieldsDropdownItems = (
|
||||
{ fields, layout }: FieldsInfo,
|
||||
subTableCode?: string,
|
||||
filterType?: OneOf['type'],
|
||||
{ subTableCode, filterType, defaultLabel }: Param = {},
|
||||
) => {
|
||||
// get used field codes
|
||||
let fieldOrder: string[];
|
||||
@@ -61,26 +60,31 @@ export const getFieldsDropdownItems = (
|
||||
if (subTableCode) {
|
||||
const subTableFields = layout.find((each) => each.type === 'SUBTABLE' && each.code === subTableCode) as any;
|
||||
fieldOrder = subTableFields?.fields.map((field: { code: string }) => field.code) || [];
|
||||
fieldMap = fieldMap[subTableCode].fields;
|
||||
const subTableFieldMap = fieldMap[subTableCode] as { fields: Record<string, any> } | undefined;
|
||||
fieldMap = subTableFieldMap?.fields || {};
|
||||
} else {
|
||||
fieldOrder = extractNoSubTableFields(layout);
|
||||
}
|
||||
|
||||
// create labels
|
||||
return fieldOrder.reduce(
|
||||
(acc, fieldCode) => {
|
||||
const field = fieldMap[fieldCode];
|
||||
if (!fieldCode || filterType && !isType[filterType](field)) return acc;
|
||||
acc.push({
|
||||
value: fieldCode,
|
||||
label: field.label + '(FC: ' + fieldCode + ')',
|
||||
});
|
||||
return acc;
|
||||
const labels = [
|
||||
{
|
||||
value: EMPTY_OPTION.value,
|
||||
label: defaultLabel || EMPTY_OPTION.label,
|
||||
},
|
||||
[EMPTY_OPTION],
|
||||
);
|
||||
];
|
||||
return fieldOrder.reduce((acc, fieldCode) => {
|
||||
const field = fieldMap[fieldCode];
|
||||
if (!fieldCode || (filterType && !isType[filterType](field))) return acc;
|
||||
acc.push({
|
||||
value: fieldCode,
|
||||
label: field.label + '(FC: ' + fieldCode + ')',
|
||||
});
|
||||
return acc;
|
||||
}, labels);
|
||||
};
|
||||
|
||||
export const getTableFieldsDropdownItems = ({ fields }: FieldsInfo, filterType?: OneOf['type']) => {
|
||||
export const getTableFieldsDropdownItems = ({ fields }: FieldsInfo, filterType?: FieldType) => {
|
||||
return Object.keys(fields).reduce(
|
||||
(acc, fieldCode) => {
|
||||
const field = fields[fieldCode];
|
||||
|
||||
@@ -9,7 +9,9 @@ export type App = {
|
||||
|
||||
export type Properties = Awaited<ReturnType<typeof client.app.getFormFields>>['properties'];
|
||||
export type Layout = Awaited<ReturnType<typeof client.app.getFormLayout>>['layout'];
|
||||
export type OneOf = Properties[string];
|
||||
|
||||
type OneOf = Properties[string];
|
||||
export type FieldType = OneOf['type'];
|
||||
|
||||
const typeNames = [
|
||||
'RECORD_NUMBER',
|
||||
@@ -40,7 +42,7 @@ const typeNames = [
|
||||
'GROUP',
|
||||
'REFERENCE_TABLE',
|
||||
'SUBTABLE',
|
||||
] as const satisfies readonly OneOf['type'][];
|
||||
] as const satisfies readonly FieldType[];
|
||||
|
||||
export const types = typeNames.reduce(
|
||||
(acc, name) => {
|
||||
@@ -50,11 +52,11 @@ export const types = typeNames.reduce(
|
||||
{} as Record<(typeof typeNames)[number], (typeof typeNames)[number]>,
|
||||
);
|
||||
|
||||
type ExtractOneOf<T extends OneOf['type']> = Extract<OneOf, { type: T }>;
|
||||
function createTypeGuard<T extends OneOf['type']>(type: T) {
|
||||
type ExtractOneOf<T extends FieldType> = Extract<OneOf, { type: T }>;
|
||||
function createTypeGuard<T extends FieldType>(type: T) {
|
||||
return (value: OneOf): value is ExtractOneOf<T> => value.type === type;
|
||||
}
|
||||
|
||||
export const isType = Object.fromEntries(
|
||||
typeNames.map((typeName) => [typeName, createTypeGuard(typeName as OneOf['type'])]),
|
||||
typeNames.map((typeName) => [typeName, createTypeGuard(typeName as FieldType)]),
|
||||
) as { [K in (typeof typeNames)[number]]: (value: OneOf) => value is ExtractOneOf<K> };
|
||||
|
||||
Reference in New Issue
Block a user