@ys-knife-crud/taro 0.0.7 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +504 -464
- package/dist/index.js.map +1 -1
- package/dist/taro/src/components/filter-panel/index.d.ts +1 -0
- package/dist/taro/src/components/filter-panel/index.d.ts.map +1 -1
- package/dist/taro/src/index.d.ts +1 -0
- package/dist/taro/src/index.d.ts.map +1 -1
- package/package.json +13 -10
- package/src/components/command-bar/README.md +170 -0
- package/src/components/command-bar/command-bar.vue +135 -0
- package/src/components/filter-item/README.md +126 -0
- package/src/components/filter-item/date-filter-item.vue +87 -0
- package/src/components/filter-item/date-range-filter-item.vue +121 -0
- package/src/components/filter-item/enum-filter-item.vue +168 -0
- package/src/components/filter-item/filter-item-layout.vue +172 -0
- package/src/components/filter-item/index.ts +5 -0
- package/src/components/filter-item/text-filter-item.vue +70 -0
- package/src/components/filter-item/use-filter-item.ts +205 -0
- package/src/components/filter-panel/README.md +278 -0
- package/src/components/filter-panel/context.ts +43 -0
- package/src/components/filter-panel/filter-panel.vue +302 -0
- package/src/components/filter-panel/index.ts +2 -0
- package/src/components/filter-panel/root-filter-panel.vue +86 -0
- package/src/components/infinite-list/index.ts +1 -0
- package/src/components/infinite-list/infinite-list.vue +455 -0
- package/src/components/infinite-page/README.md +201 -0
- package/src/components/infinite-page/infinite-page.vue +284 -0
- package/src/index.ts +13 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, onMounted, ref, type PropType } from "vue";
|
|
3
|
+
import type {
|
|
4
|
+
EnumOption,
|
|
5
|
+
EnumOptionsSource,
|
|
6
|
+
FilterItemApi,
|
|
7
|
+
Operator,
|
|
8
|
+
} from "@ys-knife-crud/core";
|
|
9
|
+
import { useEnumFilterItem, type EnumFilterItemProps } from "./use-filter-item";
|
|
10
|
+
import YsFilterItemLayout from "./filter-item-layout.vue";
|
|
11
|
+
|
|
12
|
+
defineOptions({ name: "YsEnumFilterItem" });
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* YsEnumFilterItem(小程序版)—— 枚举单字段查询条件,支持单选 / 多选
|
|
16
|
+
*
|
|
17
|
+
* 对齐 element-plus 的 YsEnumFilterItem(el-select),逻辑复用 @ys-knife-crud/vue 的
|
|
18
|
+
* useEnumFilterItem,视图换成小程序原生元素:
|
|
19
|
+
*
|
|
20
|
+
* - 单选(multiple=false,默认):`<picker mode="selector">`,使用声明方指定的 op(常为 Equals)
|
|
21
|
+
* - 多选(multiple=true):小程序没有原生多选选择器,改用标签式选项(点击切换),
|
|
22
|
+
* op 由 composable 自动派生:0 项 → null,1 项 → Equals,2+ 项 → In
|
|
23
|
+
* - optionsSource:函数类型,返回 Promise<EnumOption[]>;onMounted 加载一次,
|
|
24
|
+
* 用 opt.value 作选中值、opt.label 作显示文本
|
|
25
|
+
* - clearable 为 true 时单选模式显示清除按钮(多选逐项取消即可)
|
|
26
|
+
* - 消费方需在页面模板占位 `<picker v-if="false" />`,否则 Taro 不会登记该原生组件
|
|
27
|
+
*/
|
|
28
|
+
const props = defineProps({
|
|
29
|
+
/** 控件上方的标签 */
|
|
30
|
+
label: { type: String, default: "" },
|
|
31
|
+
/** 运算符;仅单选模式使用(多选模式按 1 项→Equals、2+ 项→In 自动派生) */
|
|
32
|
+
op: { type: String as PropType<Operator>, required: true },
|
|
33
|
+
/** 实体属性路径(FilterInfo.left),如 "status" / "user.roleId" */
|
|
34
|
+
propertyPath: { type: String, required: true },
|
|
35
|
+
/** 初始值;单选为 string | number | null,多选为 (string | number)[] */
|
|
36
|
+
defaultValue: {
|
|
37
|
+
type: [String, Number, Array] as unknown as PropType<EnumFilterItemProps["defaultValue"]>,
|
|
38
|
+
default: undefined,
|
|
39
|
+
},
|
|
40
|
+
/** 占位文本 */
|
|
41
|
+
placeholder: { type: String, default: "请选择" },
|
|
42
|
+
/** 选项数据源,返回 { label, value } 数组 */
|
|
43
|
+
optionsSource: { type: Function as PropType<EnumOptionsSource>, required: true },
|
|
44
|
+
/** 是否多选;多选时用标签式选项渲染 */
|
|
45
|
+
multiple: { type: Boolean, default: false },
|
|
46
|
+
/** 单选模式是否显示清除按钮 */
|
|
47
|
+
clearable: { type: Boolean, default: true },
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const { value, filter: filterInfo, reset } = useEnumFilterItem(props);
|
|
51
|
+
|
|
52
|
+
/* 选项数据:onMounted 调用一次 optionsSource(),loading 控制占位文案 */
|
|
53
|
+
const options = ref<EnumOption[]>([]);
|
|
54
|
+
const loading = ref(false);
|
|
55
|
+
|
|
56
|
+
onMounted(async () => {
|
|
57
|
+
loading.value = true;
|
|
58
|
+
try {
|
|
59
|
+
options.value = await props.optionsSource();
|
|
60
|
+
} finally {
|
|
61
|
+
loading.value = false;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
/* ---------------- 单选 ---------------- */
|
|
66
|
+
|
|
67
|
+
/** picker 的 value 是下标;未选中时返回 -1,模板里再夹到 0 */
|
|
68
|
+
const selectedIndex = computed<number>(() => {
|
|
69
|
+
const v = Array.isArray(value.value) ? null : value.value;
|
|
70
|
+
if (v == null) return -1;
|
|
71
|
+
return options.value.findIndex((opt) => String(opt.value) === String(v));
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
/** 选中项显示文本;未匹配到选项时显示空串(回落到占位文本) */
|
|
75
|
+
const selectedLabel = computed<string>(() => {
|
|
76
|
+
const index = selectedIndex.value;
|
|
77
|
+
return index >= 0 ? (options.value[index]?.label ?? "") : "";
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
function onSelectChange(e: Event): void {
|
|
81
|
+
const detail = (e as unknown as { detail?: { value?: string | number } }).detail;
|
|
82
|
+
const index = Number(detail?.value ?? -1);
|
|
83
|
+
const opt = options.value[index];
|
|
84
|
+
// EnumOption.value 允许 boolean(如启用/禁用枚举),值逻辑里按 string|number 处理,
|
|
85
|
+
// 运行时 con 只做包装,布尔值同样可用
|
|
86
|
+
if (opt) value.value = opt.value as unknown as string | number;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function onClear(): void {
|
|
90
|
+
value.value = null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* ---------------- 多选 ---------------- */
|
|
94
|
+
|
|
95
|
+
const multiValues = computed<(string | number)[]>(() =>
|
|
96
|
+
Array.isArray(value.value) ? value.value : [],
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
function isActive(opt: EnumOption): boolean {
|
|
100
|
+
return multiValues.value.some((v) => String(v) === String(opt.value));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** 点击标签切换选中状态 */
|
|
104
|
+
function toggle(opt: EnumOption): void {
|
|
105
|
+
const next = [...multiValues.value];
|
|
106
|
+
const index = next.findIndex((v) => String(v) === String(opt.value));
|
|
107
|
+
if (index >= 0) next.splice(index, 1);
|
|
108
|
+
else next.push(opt.value as unknown as string | number);
|
|
109
|
+
value.value = next;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/* filter 用 getter 读 computed.value,使面板端的聚合 computed 能 track 到值变化 */
|
|
113
|
+
const api = {
|
|
114
|
+
get filter() {
|
|
115
|
+
return filterInfo.value;
|
|
116
|
+
},
|
|
117
|
+
reset,
|
|
118
|
+
} as unknown as FilterItemApi;
|
|
119
|
+
|
|
120
|
+
defineExpose(api);
|
|
121
|
+
</script>
|
|
122
|
+
|
|
123
|
+
<template>
|
|
124
|
+
<YsFilterItemLayout :label="label" :api="api">
|
|
125
|
+
<!-- 多选:标签式选项 -->
|
|
126
|
+
<view v-if="multiple" class="yk-filter-item__chips">
|
|
127
|
+
<text v-if="loading" class="yk-filter-item__chips-empty">加载中…</text>
|
|
128
|
+
<template v-else>
|
|
129
|
+
<view
|
|
130
|
+
v-for="opt in options"
|
|
131
|
+
:key="String(opt.value)"
|
|
132
|
+
class="yk-filter-item__chip"
|
|
133
|
+
:class="{ 'yk-filter-item__chip--active': isActive(opt) }"
|
|
134
|
+
@tap="toggle(opt)"
|
|
135
|
+
>
|
|
136
|
+
<text>{{ opt.label }}</text>
|
|
137
|
+
</view>
|
|
138
|
+
</template>
|
|
139
|
+
</view>
|
|
140
|
+
|
|
141
|
+
<!-- 单选:原生选择器 -->
|
|
142
|
+
<template v-else>
|
|
143
|
+
<picker
|
|
144
|
+
class="yk-filter-item__picker-trigger"
|
|
145
|
+
mode="selector"
|
|
146
|
+
:range="options"
|
|
147
|
+
range-key="label"
|
|
148
|
+
:value="selectedIndex < 0 ? 0 : selectedIndex"
|
|
149
|
+
@change="onSelectChange"
|
|
150
|
+
>
|
|
151
|
+
<view class="yk-filter-item__picker">
|
|
152
|
+
<text
|
|
153
|
+
class="yk-filter-item__picker-text"
|
|
154
|
+
:class="{ 'yk-filter-item__picker-text--placeholder': !selectedLabel }"
|
|
155
|
+
>{{ loading ? "加载中…" : selectedLabel || placeholder }}</text>
|
|
156
|
+
</view>
|
|
157
|
+
</picker>
|
|
158
|
+
|
|
159
|
+
<view
|
|
160
|
+
v-if="clearable && selectedLabel"
|
|
161
|
+
class="yk-filter-item__clear"
|
|
162
|
+
@tap.stop="onClear"
|
|
163
|
+
>
|
|
164
|
+
<text>✕</text>
|
|
165
|
+
</view>
|
|
166
|
+
</template>
|
|
167
|
+
</YsFilterItemLayout>
|
|
168
|
+
</template>
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { type PropType } from "vue";
|
|
3
|
+
import type { FilterItemApi } from "@ys-knife-crud/core";
|
|
4
|
+
import { useFilterPanelItem } from "../filter-panel/context";
|
|
5
|
+
|
|
6
|
+
defineOptions({ name: "YsFilterItemLayout" });
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* YsFilterItemLayout(小程序版)—— FilterItem 统一外壳
|
|
10
|
+
*
|
|
11
|
+
* 对齐 @ys-knife-crud/element-plus 的 YsFilterItemLayout,把各 FilterItem 共有的
|
|
12
|
+
* 「label + 控件容器 + 样式 + 面板注册」抽出来:
|
|
13
|
+
* 1. 具体 FilterItem 调自己的 composable 拿到 { value, filter, reset }
|
|
14
|
+
* 2. 组装稳定 api 对象(filter 用 getter 读 computed.value)
|
|
15
|
+
* 3. 把 api 与 label 传进本组件,控件通过默认插槽注入
|
|
16
|
+
*
|
|
17
|
+
* 注册/反注册复用 @ys-knife-crud/taro 自己的 useFilterPanelItem(inject FilterPanelKey),
|
|
18
|
+
* 不依赖 element-plus 的 YsFilterItemLayout。
|
|
19
|
+
*
|
|
20
|
+
* 元素约束:只用小程序内置元素(view / text / input / picker),不使用 div/label。
|
|
21
|
+
*/
|
|
22
|
+
const props = defineProps({
|
|
23
|
+
/** 控件上方的标签文本;空串时不渲染 */
|
|
24
|
+
label: { type: String, default: "" },
|
|
25
|
+
/** FilterItem 的 API 对象,由 layout 注册到最近的 YsFilterPanel */
|
|
26
|
+
api: { type: Object as PropType<FilterItemApi>, required: true },
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
useFilterPanelItem(props.api);
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<template>
|
|
33
|
+
<view class="yk-filter-item">
|
|
34
|
+
<text v-if="label" class="yk-filter-item__label">{{ label }}</text>
|
|
35
|
+
<view class="yk-filter-item__control">
|
|
36
|
+
<slot />
|
|
37
|
+
</view>
|
|
38
|
+
</view>
|
|
39
|
+
</template>
|
|
40
|
+
|
|
41
|
+
<style lang="scss">
|
|
42
|
+
/* 外壳:纵向「标签 + 控件」,铺满条件区宽度(条件区本身是纵向 flex 列) */
|
|
43
|
+
.yk-filter-item {
|
|
44
|
+
display: flex;
|
|
45
|
+
flex-direction: column;
|
|
46
|
+
width: 100%;
|
|
47
|
+
gap: 8rpx;
|
|
48
|
+
box-sizing: border-box;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.yk-filter-item__label {
|
|
52
|
+
font-size: 22rpx;
|
|
53
|
+
line-height: 1.4;
|
|
54
|
+
color: #909399;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
.yk-filter-item__control {
|
|
58
|
+
display: flex;
|
|
59
|
+
align-items: center;
|
|
60
|
+
width: 100%;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* ---------------- 各 FilterItem 共用的控件样式 ---------------- */
|
|
64
|
+
|
|
65
|
+
/* 输入框 / 选择器外容器:浅灰底 + 圆角,高度与面板顶部输入框一致(64rpx) */
|
|
66
|
+
.yk-filter-item__field {
|
|
67
|
+
flex: 1;
|
|
68
|
+
min-width: 0;
|
|
69
|
+
height: 64rpx;
|
|
70
|
+
padding: 0 20rpx;
|
|
71
|
+
box-sizing: border-box;
|
|
72
|
+
background-color: #f2f3f5;
|
|
73
|
+
border-radius: 8rpx;
|
|
74
|
+
overflow: hidden;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.yk-filter-item__input {
|
|
78
|
+
width: 100%;
|
|
79
|
+
height: 100%;
|
|
80
|
+
font-size: 26rpx;
|
|
81
|
+
line-height: 64rpx;
|
|
82
|
+
color: #303133;
|
|
83
|
+
background-color: transparent;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.yk-filter-item__placeholder {
|
|
87
|
+
color: #909399;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/* 日期/枚举选择器:picker 本身只做触发器(撑开剩余宽度),视觉放在内部 view 上 */
|
|
91
|
+
.yk-filter-item__picker-trigger {
|
|
92
|
+
flex: 1;
|
|
93
|
+
min-width: 0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/* 日期选择器触发器(picker 本身不可直接设样式,内部放一个 view 承载视觉) */
|
|
97
|
+
.yk-filter-item__picker {
|
|
98
|
+
width: 100%;
|
|
99
|
+
display: flex;
|
|
100
|
+
align-items: center;
|
|
101
|
+
height: 64rpx;
|
|
102
|
+
padding: 0 20rpx;
|
|
103
|
+
box-sizing: border-box;
|
|
104
|
+
background-color: #f2f3f5;
|
|
105
|
+
border-radius: 8rpx;
|
|
106
|
+
overflow: hidden;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
.yk-filter-item__picker-text {
|
|
110
|
+
font-size: 26rpx;
|
|
111
|
+
line-height: 1;
|
|
112
|
+
color: #303133;
|
|
113
|
+
white-space: nowrap;
|
|
114
|
+
overflow: hidden;
|
|
115
|
+
text-overflow: ellipsis;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
.yk-filter-item__picker-text--placeholder {
|
|
119
|
+
color: #909399;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/* 单个条件清除按钮 */
|
|
123
|
+
.yk-filter-item__clear {
|
|
124
|
+
flex-shrink: 0;
|
|
125
|
+
display: flex;
|
|
126
|
+
align-items: center;
|
|
127
|
+
justify-content: center;
|
|
128
|
+
width: 48rpx;
|
|
129
|
+
height: 48rpx;
|
|
130
|
+
margin-left: 8rpx;
|
|
131
|
+
font-size: 24rpx;
|
|
132
|
+
color: #c0c4cc;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/* 日期区间的「至」分隔符 */
|
|
136
|
+
.yk-filter-item__sep {
|
|
137
|
+
flex-shrink: 0;
|
|
138
|
+
padding: 0 12rpx;
|
|
139
|
+
font-size: 24rpx;
|
|
140
|
+
color: #909399;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/* 枚举多选:标签式选项,点击切换选中 */
|
|
144
|
+
.yk-filter-item__chips {
|
|
145
|
+
display: flex;
|
|
146
|
+
flex-wrap: wrap;
|
|
147
|
+
width: 100%;
|
|
148
|
+
gap: 12rpx;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
.yk-filter-item__chip {
|
|
152
|
+
padding: 8rpx 20rpx;
|
|
153
|
+
box-sizing: border-box;
|
|
154
|
+
border: 2rpx solid #dcdfe6;
|
|
155
|
+
border-radius: 8rpx;
|
|
156
|
+
background-color: #ffffff;
|
|
157
|
+
font-size: 24rpx;
|
|
158
|
+
line-height: 1.2;
|
|
159
|
+
color: #606266;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.yk-filter-item__chip--active {
|
|
163
|
+
border-color: #d9ecff;
|
|
164
|
+
background-color: #ecf5ff;
|
|
165
|
+
color: #409eff;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
.yk-filter-item__chips-empty {
|
|
169
|
+
font-size: 26rpx;
|
|
170
|
+
color: #909399;
|
|
171
|
+
}
|
|
172
|
+
</style>
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { default as YsFilterItemLayout } from "./filter-item-layout.vue";
|
|
2
|
+
export { default as YsTextFilterItem } from "./text-filter-item.vue";
|
|
3
|
+
export { default as YsDateFilterItem } from "./date-filter-item.vue";
|
|
4
|
+
export { default as YsDateRangeFilterItem } from "./date-range-filter-item.vue";
|
|
5
|
+
export { default as YsEnumFilterItem } from "./enum-filter-item.vue";
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { type PropType } from "vue";
|
|
3
|
+
import type { FilterItemApi, Operator } from "@ys-knife-crud/core";
|
|
4
|
+
import { useTextFilterItem, type TextFilterItemProps } from "./use-filter-item";
|
|
5
|
+
import YsFilterItemLayout from "./filter-item-layout.vue";
|
|
6
|
+
|
|
7
|
+
defineOptions({ name: "YsTextFilterItem" });
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* YsTextFilterItem(小程序版)—— 文本单字段查询条件
|
|
11
|
+
*
|
|
12
|
+
* 对齐 element-plus 的 YsTextFilterItem,逻辑完全复用 @ys-knife-crud/vue 的
|
|
13
|
+
* useTextFilterItem(UI 库无关),本文件只负责小程序视图:input + 外壳。
|
|
14
|
+
*
|
|
15
|
+
* - 只负责 string 类型,渲染小程序原生 input
|
|
16
|
+
* - op 由声明方指定(常为 Operator.Contains,也可 Equals / StartsWith / EndsWith)
|
|
17
|
+
* - 值为空(null/空串/纯空白)时 filter 返回 null,面板聚合时跳过
|
|
18
|
+
* - 注册/反注册由 YsFilterItemLayout 接管
|
|
19
|
+
*
|
|
20
|
+
* 同 filter-panel:input 外面必须包一层 view,直接做 flex 子项时宽度会塌陷。
|
|
21
|
+
* 消费方需在页面模板占位 `<input v-if="false" />`,否则 Taro 不会登记该原生组件。
|
|
22
|
+
*/
|
|
23
|
+
const props = defineProps({
|
|
24
|
+
/** 控件上方的标签 */
|
|
25
|
+
label: { type: String, default: "" },
|
|
26
|
+
/** 运算符(字符串 enum 跨包用 String as PropType<Operator>) */
|
|
27
|
+
op: { type: String as PropType<Operator>, required: true },
|
|
28
|
+
/** 实体属性路径(FilterInfo.left),如 "name" / "user.name" */
|
|
29
|
+
propertyPath: { type: String, required: true },
|
|
30
|
+
/** 初始值;缺省为空字符串 */
|
|
31
|
+
defaultValue: {
|
|
32
|
+
type: String as PropType<TextFilterItemProps["defaultValue"]>,
|
|
33
|
+
default: undefined,
|
|
34
|
+
},
|
|
35
|
+
/** 占位文本 */
|
|
36
|
+
placeholder: { type: String, default: "请输入" },
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const { value, filter: filterInfo, reset } = useTextFilterItem(props);
|
|
40
|
+
|
|
41
|
+
/** 小程序 input 的值在 event.detail.value(DOM 类型下 detail 非该结构,运行时按小程序取) */
|
|
42
|
+
function onInput(e: Event): void {
|
|
43
|
+
const detail = (e as unknown as { detail?: { value?: string } }).detail;
|
|
44
|
+
value.value = detail?.value ?? "";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/* filter 用 getter 读 computed.value,使面板端的聚合 computed 能 track 到值变化 */
|
|
48
|
+
const api = {
|
|
49
|
+
get filter() {
|
|
50
|
+
return filterInfo.value;
|
|
51
|
+
},
|
|
52
|
+
reset,
|
|
53
|
+
} as unknown as FilterItemApi;
|
|
54
|
+
|
|
55
|
+
defineExpose(api);
|
|
56
|
+
</script>
|
|
57
|
+
|
|
58
|
+
<template>
|
|
59
|
+
<YsFilterItemLayout :label="label" :api="api">
|
|
60
|
+
<view class="yk-filter-item__field">
|
|
61
|
+
<input
|
|
62
|
+
class="yk-filter-item__input"
|
|
63
|
+
:value="value"
|
|
64
|
+
:placeholder="placeholder"
|
|
65
|
+
placeholder-class="yk-filter-item__placeholder"
|
|
66
|
+
@input="onInput"
|
|
67
|
+
/>
|
|
68
|
+
</view>
|
|
69
|
+
</YsFilterItemLayout>
|
|
70
|
+
</template>
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { computed, ref } from "vue";
|
|
2
|
+
import { con, filter, Operator } from "@ys-knife-crud/core";
|
|
3
|
+
import type { FilterInfo, Operator as OperatorType } from "@ys-knife-crud/core";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* FilterItem 的查询条件逻辑(小程序内联版)。
|
|
7
|
+
*
|
|
8
|
+
* 与 @ys-knife-crud/vue 的 useTextFilterItem / useDateFilterItem /
|
|
9
|
+
* useDateRangeFilterItem / useEnumFilterItem 行为一致,这里内联一份是为了让
|
|
10
|
+
* @ys-knife-crud/taro 不新增对 @ys-knife-crud/vue 的依赖(小程序侧依赖越少越好)。
|
|
11
|
+
* 修改逻辑时请与 vue 包同步。
|
|
12
|
+
*
|
|
13
|
+
* 共同约定:
|
|
14
|
+
* - 每个具体类型一个工厂函数,不做 type 工厂模式
|
|
15
|
+
* - filter 返回 FilterInfo | null,null 表示「无查询条件」,由 YsFilterPanel 聚合时跳过
|
|
16
|
+
* - 返回的 filter 是 computed,组件把它包成 getter 塞进 FilterItemApi,
|
|
17
|
+
* 面板端的聚合 computed 才能 track 到值变化
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export interface TextFilterItemProps {
|
|
21
|
+
/** 运算符;常传 Operator.Contains,也可传 Equals/StartsWith/EndsWith 等 */
|
|
22
|
+
readonly op: OperatorType;
|
|
23
|
+
/** 实体属性路径(FilterInfo.left),如 "name" / "user.name" */
|
|
24
|
+
readonly propertyPath: string;
|
|
25
|
+
/** 初始值;缺省为空字符串 */
|
|
26
|
+
readonly defaultValue?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function useTextFilterItem(props: TextFilterItemProps) {
|
|
30
|
+
const value = ref<string>(
|
|
31
|
+
typeof props.defaultValue === "string" ? props.defaultValue : "",
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
function isEmpty(v: string): boolean {
|
|
35
|
+
return v == null || v.trim() === "";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const filterInfo = computed<FilterInfo | null>(() => {
|
|
39
|
+
if (isEmpty(value.value)) return null;
|
|
40
|
+
return filter(props.propertyPath, props.op, con(value.value));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
function reset(): void {
|
|
44
|
+
value.value =
|
|
45
|
+
typeof props.defaultValue === "string" ? props.defaultValue : "";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return { value, filter: filterInfo, reset };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface DateFilterItemProps {
|
|
52
|
+
/** 运算符;常传 Operator.Equals,也可传 GreaterThan / LessThan 等 */
|
|
53
|
+
readonly op: OperatorType;
|
|
54
|
+
/** 实体属性路径(FilterInfo.left),如 "birthDate" / "user.createdAt" */
|
|
55
|
+
readonly propertyPath: string;
|
|
56
|
+
/** 初始值;YYYY-MM-DD 格式字符串,缺省为空 */
|
|
57
|
+
readonly defaultValue?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function useDateFilterItem(props: DateFilterItemProps) {
|
|
61
|
+
const value = ref<string>(
|
|
62
|
+
typeof props.defaultValue === "string" ? props.defaultValue : "",
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
function isEmpty(v: string): boolean {
|
|
66
|
+
return v == null || v.trim() === "";
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const filterInfo = computed<FilterInfo | null>(() => {
|
|
70
|
+
if (isEmpty(value.value)) return null;
|
|
71
|
+
return filter(props.propertyPath, props.op, con(value.value));
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
function reset(): void {
|
|
75
|
+
value.value =
|
|
76
|
+
typeof props.defaultValue === "string" ? props.defaultValue : "";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { value, filter: filterInfo, reset };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 日期区间的值:[start, end],YYYY-MM-DD */
|
|
83
|
+
export type DateRangeValue = [string, string] | null;
|
|
84
|
+
|
|
85
|
+
export interface DateRangeFilterItemProps {
|
|
86
|
+
/** 实体属性路径(FilterInfo.left),如 "createdAt" / "user.registeredAt" */
|
|
87
|
+
readonly propertyPath: string;
|
|
88
|
+
/** 初始值;[start, end] 字符串数组,缺省为 null */
|
|
89
|
+
readonly defaultValue?: [string, string];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function useDateRangeFilterItem(props: DateRangeFilterItemProps) {
|
|
93
|
+
function toRange(v: [string, string] | undefined): DateRangeValue {
|
|
94
|
+
if (
|
|
95
|
+
v &&
|
|
96
|
+
v.length === 2 &&
|
|
97
|
+
typeof v[0] === "string" &&
|
|
98
|
+
typeof v[1] === "string"
|
|
99
|
+
) {
|
|
100
|
+
return [v[0], v[1]];
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const initial = toRange(props.defaultValue);
|
|
106
|
+
const value = ref<DateRangeValue>(initial);
|
|
107
|
+
|
|
108
|
+
function isEmpty(v: DateRangeValue): boolean {
|
|
109
|
+
return v == null || v.length !== 2 || !v[0] || !v[1];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const filterInfo = computed<FilterInfo | null>(() => {
|
|
113
|
+
if (isEmpty(value.value)) return null;
|
|
114
|
+
const [start, end] = value.value as [string, string];
|
|
115
|
+
return filter(props.propertyPath, Operator.Between, con([start, end]));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
function reset(): void {
|
|
119
|
+
value.value = initial;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return { value, filter: filterInfo, reset };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** 多选模式下的值类型 */
|
|
126
|
+
export type EnumMultiValue = (string | number)[];
|
|
127
|
+
/** 单选模式下的值类型 */
|
|
128
|
+
export type EnumSingleValue = string | number | null;
|
|
129
|
+
|
|
130
|
+
export interface EnumFilterItemProps {
|
|
131
|
+
/**
|
|
132
|
+
* 运算符;仅在单选模式下使用(常为 Operator.Equals)。
|
|
133
|
+
* 多选模式下被忽略,按「1 项→Equals,2+ 项→In」自动派生。
|
|
134
|
+
*/
|
|
135
|
+
readonly op: OperatorType;
|
|
136
|
+
/** 实体属性路径(FilterInfo.left),如 "status" / "user.roleId" */
|
|
137
|
+
readonly propertyPath: string;
|
|
138
|
+
/** 初始值;单选为 string | number | null,多选为 (string | number)[] */
|
|
139
|
+
readonly defaultValue?: string | number | null | (string | number)[];
|
|
140
|
+
/** 是否多选;默认 false */
|
|
141
|
+
readonly multiple?: boolean;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function toSingleValue(v: unknown): EnumSingleValue {
|
|
145
|
+
if (typeof v === "string" || typeof v === "number") return v;
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function toMultiValue(v: unknown): EnumMultiValue {
|
|
150
|
+
if (Array.isArray(v)) {
|
|
151
|
+
return v.filter(
|
|
152
|
+
(item): item is string | number =>
|
|
153
|
+
typeof item === "string" || typeof item === "number",
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
return [];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function useEnumFilterItem(props: EnumFilterItemProps) {
|
|
160
|
+
const isMultiple = props.multiple === true;
|
|
161
|
+
|
|
162
|
+
const value = ref<EnumSingleValue | EnumMultiValue>(
|
|
163
|
+
isMultiple ? toMultiValue(props.defaultValue) : toSingleValue(props.defaultValue),
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
function isEmpty(): boolean {
|
|
167
|
+
const v = value.value;
|
|
168
|
+
if (isMultiple) {
|
|
169
|
+
return !Array.isArray(v) || v.length === 0;
|
|
170
|
+
}
|
|
171
|
+
if (v == null) return true;
|
|
172
|
+
if (typeof v === "string" && v.trim() === "") return true;
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const filterInfo = computed<FilterInfo | null>(() => {
|
|
177
|
+
if (isEmpty()) return null;
|
|
178
|
+
if (isMultiple) {
|
|
179
|
+
const arr = value.value as EnumMultiValue;
|
|
180
|
+
if (arr.length === 1) {
|
|
181
|
+
const v = arr[0];
|
|
182
|
+
if (v == null) return null;
|
|
183
|
+
return filter(props.propertyPath, Operator.Equals, con(v));
|
|
184
|
+
}
|
|
185
|
+
// con 的类型签名不接受 string 与 number 混合数组,运行时只做包装,
|
|
186
|
+
// 用 unknown 双重断言绕过静态检查
|
|
187
|
+
return filter(
|
|
188
|
+
props.propertyPath,
|
|
189
|
+
Operator.In,
|
|
190
|
+
con(arr as unknown as (string | null)[]),
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
const v = value.value as EnumSingleValue;
|
|
194
|
+
if (v == null) return null;
|
|
195
|
+
return filter(props.propertyPath, props.op, con(v));
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
function reset(): void {
|
|
199
|
+
value.value = isMultiple
|
|
200
|
+
? toMultiValue(props.defaultValue)
|
|
201
|
+
: toSingleValue(props.defaultValue);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return { value, filter: filterInfo, reset };
|
|
205
|
+
}
|