yh-mobile-components 1.0.0

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.
@@ -0,0 +1,176 @@
1
+ <template>
2
+ <van-field
3
+ class="yhm-datetime-container"
4
+ :disabled="disabled"
5
+ v-bind="$attrs"
6
+ :error="error"
7
+ :error-message="errorMessage">
8
+ <template #input>
9
+ <div
10
+ class="yhm-datetime-value"
11
+ :class="valueClass"
12
+ @click="toChoose">
13
+ {{ valueString }}
14
+ </div>
15
+ </template>
16
+ </van-field>
17
+ <van-popup
18
+ v-model:show="show"
19
+ class="yhm-datetime-container"
20
+ position="bottom">
21
+ <van-nav-bar
22
+ left-text="清除"
23
+ @click-left="cancelChoose"
24
+ :title="`请选择${typeName}`"
25
+ right-text="确认"
26
+ @click-right="commitChoose"></van-nav-bar>
27
+ <van-tabs v-model:active="activeTab">
28
+ <van-tab
29
+ title="日期选择"
30
+ v-if="['year', 'month', 'date', 'datetime'].includes(type)">
31
+ <choose-date
32
+ ref="chooseDateRef"
33
+ :type="type"
34
+ v-model="ownValue"></choose-date>
35
+ </van-tab>
36
+ <van-tab
37
+ title="时间选择"
38
+ v-if="['datetime', 'time'].includes(type)">
39
+ <choose-time
40
+ ref="chooseTimeRef"
41
+ :type="type"
42
+ v-model="ownValue"></choose-time>
43
+ </van-tab>
44
+ <van-tab :title="`${typeName}输入`">
45
+ <input-date-time
46
+ ref="inputDateTimeRef"
47
+ :format="format || typeFormat"
48
+ :type="type"
49
+ :type-name="typeName"
50
+ v-model="ownValue"></input-date-time>
51
+ </van-tab>
52
+ </van-tabs>
53
+ </van-popup>
54
+ </template>
55
+ <script setup lang="ts">
56
+ import chooseDate from "./yhmDateTime/chooseDate.vue";
57
+ import chooseTime from "./yhmDateTime/chooseTime.vue";
58
+ import inputDateTime from "./yhmDateTime/inputDateTime.vue";
59
+ import dayjs from "dayjs";
60
+ import { ref, computed, watch, nextTick } from "vue";
61
+
62
+ const props = withDefaults(
63
+ defineProps<{
64
+ error: boolean;
65
+ errorMessage: string;
66
+ type: "year" | "month" | "date" | "datetime" | "time";
67
+ format?: string;
68
+ disabled?: boolean;
69
+ modelValue?: any;
70
+ placeholder?: string;
71
+ }>(),
72
+ {
73
+ type: "datetime",
74
+ disabled: false,
75
+ placeholder: "请选择日期",
76
+ }
77
+ );
78
+
79
+ const emits = defineEmits<{
80
+ (e: "update:modelValue", val: string);
81
+ (e: "change", val: string);
82
+ }>();
83
+
84
+ const chooseDateRef = ref();
85
+ const chooseTimeRef = ref();
86
+ const inputDateTimeRef = ref();
87
+
88
+ const ownValue = ref(dayjs());
89
+
90
+ watch(
91
+ () => props.modelValue,
92
+ (val) => {
93
+ let date = dayjs(val);
94
+ if (date.isValid() && date.valueOf() !== ownValue.value.valueOf()) {
95
+ ownValue.value = date;
96
+ }
97
+ }
98
+ );
99
+
100
+ const valueString = computed(() => {
101
+ return props.modelValue || props.placeholder;
102
+ });
103
+
104
+ const valueClass = computed(() => {
105
+ return {
106
+ placeholder: !props.modelValue,
107
+ };
108
+ });
109
+
110
+ const typeFormat = ref("YYYY-MM-DD HH:mm:ss");
111
+ const typeName = computed(() => {
112
+ switch (props.type) {
113
+ case "year":
114
+ typeFormat.value = "YYYY-01-01 00:00:00";
115
+ return "年";
116
+ case "month":
117
+ typeFormat.value = "YYYY-MM-01 00:00:00";
118
+ return "年月";
119
+ case "date":
120
+ typeFormat.value = "YYYY-MM-DD 00:00:00";
121
+ return "日期";
122
+ case "time":
123
+ typeFormat.value = "HH:mm:ss";
124
+ return "时间";
125
+ case "datetime":
126
+ typeFormat.value = "YYYY-MM-DD HH:mm:ss";
127
+ return "日期时间";
128
+ }
129
+ });
130
+
131
+ const show = ref(false);
132
+
133
+ function toChoose() {
134
+ if (props.disabled) {
135
+ return false;
136
+ }
137
+ show.value = true;
138
+ let date = dayjs(props.modelValue);
139
+ if (date.isValid() && date.valueOf() !== ownValue.value.valueOf()) {
140
+ ownValue.value = date;
141
+ }
142
+ nextTick(() => {
143
+ chooseDateRef.value?.syncModelValue();
144
+ chooseTimeRef.value?.syncModelValue();
145
+ inputDateTimeRef.value?.syncModelValue();
146
+ });
147
+ }
148
+ function cancelChoose() {
149
+ show.value = false;
150
+ emits("update:modelValue", "");
151
+ }
152
+
153
+ function commitChoose() {
154
+ let val = ownValue.value.format(props.format || typeFormat.value);
155
+ emits("update:modelValue", val);
156
+ show.value = false;
157
+ }
158
+
159
+ const activeTab = ref(0);
160
+ </script>
161
+ <style lang="scss">
162
+ .yhm-datetime-container {
163
+ --van-switch-width: calc(3em + 4px);
164
+ .yhm-datetime-value {
165
+ flex: 1;
166
+ width: 100%;
167
+ text-align: right;
168
+ &.placeholder {
169
+ color: var(--van-text-color-3);
170
+ }
171
+ }
172
+ &.van-popup {
173
+ height: 560px;
174
+ }
175
+ }
176
+ </style>
@@ -0,0 +1,36 @@
1
+ <template>
2
+ <van-field
3
+ class="yhm-input-container"
4
+ v-bind="$attrs"
5
+ :model-value="modelValue"
6
+ @update:modelValue="changeHandler"
7
+ :error="error"
8
+ :error-message="errorMessage"></van-field>
9
+ </template>
10
+ <script setup lang="ts">
11
+ const props = withDefaults(
12
+ defineProps<{
13
+ error: boolean;
14
+ errorMessage: string;
15
+ modelValue?: any;
16
+ }>(),
17
+ {}
18
+ );
19
+
20
+ const emits = defineEmits<{
21
+ (e: "update:modelValue", val: any);
22
+ (e: "change", val: any);
23
+ }>();
24
+
25
+ function changeHandler(val) {
26
+ emits("update:modelValue", val);
27
+ emits("change", val);
28
+ }
29
+ </script>
30
+ <style lang="scss">
31
+ .yhm-input-container {
32
+ .van-field__control {
33
+ text-align: right;
34
+ }
35
+ }
36
+ </style>
@@ -0,0 +1,59 @@
1
+ <template>
2
+ <van-field
3
+ class="yhm-radio-container"
4
+ v-bind="$attrs"
5
+ :disabled="disabled"
6
+ :error="error"
7
+ :error-message="errorMessage">
8
+ <template #input>
9
+ <div class="yhm-radio-list">
10
+ <van-radio-group
11
+ :disabled="disabled"
12
+ :model-value="modelValue"
13
+ @update:modelValue="changeHandler">
14
+ <van-radio
15
+ v-for="option in optionData"
16
+ :name="option.value">
17
+ {{ option.text }}
18
+ </van-radio>
19
+ </van-radio-group>
20
+ </div>
21
+ </template>
22
+ </van-field>
23
+ </template>
24
+ <script setup lang="ts">
25
+ import { vanOption } from "../types";
26
+
27
+ const props = withDefaults(
28
+ defineProps<{
29
+ error: boolean;
30
+ errorMessage: string;
31
+ modelValue?: any;
32
+ disabled?: boolean;
33
+ optionData: vanOption[];
34
+ }>(),
35
+ {}
36
+ );
37
+
38
+ const emits = defineEmits<{
39
+ (e: "update:modelValue", val: any);
40
+ (e: "change", val: string);
41
+ }>();
42
+
43
+ function changeHandler(val) {
44
+ emits("update:modelValue", val);
45
+ }
46
+ </script>
47
+ <style lang="scss">
48
+ .yhm-radio-container {
49
+ .yhm-radio-list {
50
+ flex: 1;
51
+ .van-radio {
52
+ padding-bottom: 5px;
53
+ &:first-child {
54
+ padding-top: 5px;
55
+ }
56
+ }
57
+ }
58
+ }
59
+ </style>
@@ -0,0 +1,88 @@
1
+ <template>
2
+ <van-field
3
+ class="yhm-datetime-container"
4
+ :disabled="disabled"
5
+ v-bind="$attrs"
6
+ :error="error"
7
+ :error-message="errorMessage">
8
+ <template #input>
9
+ <div
10
+ class="yhm-datetime-value"
11
+ :class="valueClass"
12
+ @click="toChoose">
13
+ {{ valueString }}
14
+ </div>
15
+ </template>
16
+ </van-field>
17
+ <van-popup
18
+ v-model:show="show"
19
+ class="yhm-datetime-container"
20
+ position="bottom">
21
+ <van-picker
22
+ :title="palceholder"
23
+ :columns="optionData"
24
+ @confirm="onCancel"
25
+ @cancel="onCancel"
26
+ @change="onChange" />
27
+ </van-popup>
28
+ </template>
29
+ <script setup lang="ts">
30
+ import { ref, computed } from "vue";
31
+
32
+ const props = withDefaults(defineProps<{ error: boolean; errorMessage: string; modelValue?: any; disabled?: boolean; palceholder?: string; optionData: any[] }>(), {
33
+ type: "datetime",
34
+ disabled: false,
35
+ palceholder: "请选择",
36
+ });
37
+
38
+ const emits = defineEmits<{
39
+ (e: "update:modelValue", val: string);
40
+ (e: "change", val: string);
41
+ }>();
42
+
43
+ const valueString = computed(() => {
44
+ if (props.modelValue) {
45
+ return props.optionData.filter((item) => item.value === props.modelValue)[0].text;
46
+ } else {
47
+ return props.palceholder || "请选择";
48
+ }
49
+ });
50
+
51
+ const valueClass = computed(() => {
52
+ return {
53
+ placeholder: !props.modelValue,
54
+ };
55
+ });
56
+
57
+ const show = ref(false);
58
+
59
+ function toChoose() {
60
+ if (props.disabled) {
61
+ return false;
62
+ }
63
+ show.value = true;
64
+ }
65
+
66
+ function onChange({ selectedValues }) {
67
+ emits("update:modelValue", selectedValues[0]);
68
+ }
69
+ function onCancel() {
70
+ show.value = false;
71
+ }
72
+ </script>
73
+ <style lang="scss">
74
+ .yhm-datetime-container {
75
+ --van-switch-width: calc(3em + 4px);
76
+ .yhm-datetime-value {
77
+ flex: 1;
78
+ width: 100%;
79
+ text-align: right;
80
+ &.placeholder {
81
+ color: var(--van-text-color-3);
82
+ }
83
+ }
84
+ &.van-popup {
85
+ height: 560px;
86
+ }
87
+ }
88
+ </style>
@@ -0,0 +1,91 @@
1
+ <template>
2
+ <van-field
3
+ class="yhm-switch-container"
4
+ v-bind="$attrs"
5
+ :disabled="disabled"
6
+ :error="error"
7
+ :error-message="errorMessage">
8
+ <template #input>
9
+ <div class="yhm-switch-value">
10
+ <van-switch
11
+ :disabled="disabled"
12
+ :model-value="modelValue"
13
+ :active-value="activeValue"
14
+ :inactive-value="inactiveValue"
15
+ :size="size"
16
+ @update:modelValue="changeHandler">
17
+ <template #background>
18
+ <div class="yhm-switch-value-text">
19
+ {{ modelValue === activeValue ? activeText : inactiveText }}
20
+ </div>
21
+ </template>
22
+ </van-switch>
23
+ </div>
24
+ </template>
25
+ </van-field>
26
+ </template>
27
+ <script setup lang="ts">
28
+ import { computed } from "vue";
29
+
30
+ const props = withDefaults(
31
+ defineProps<{
32
+ error: boolean;
33
+ errorMessage: string;
34
+ modelValue?: any;
35
+ disabled?: boolean;
36
+ size?: number;
37
+ activeValue?: boolean;
38
+ inactiveValue?: boolean;
39
+ activeText?: string;
40
+ inactiveText?: string;
41
+ }>(),
42
+ {
43
+ disabled: false,
44
+ size: 18,
45
+ activeValue: true,
46
+ inactiveValue: false,
47
+ activeText: "",
48
+ inactiveText: "",
49
+ }
50
+ );
51
+
52
+ const emits = defineEmits<{
53
+ (e: "update:modelValue", val: any);
54
+ (e: "change", val: any);
55
+ }>();
56
+
57
+ function changeHandler(val) {
58
+ emits("update:modelValue", val);
59
+ emits("change", val);
60
+ }
61
+ </script>
62
+ <style lang="scss">
63
+ .yhm-switch-container {
64
+ .yhm-switch-value {
65
+ flex: 1;
66
+ display: flex;
67
+ justify-content: flex-end;
68
+ .van-switch {
69
+ width: auto;
70
+ .yhm-switch-value-text {
71
+ transition: padding 0.3s ease 0s;
72
+ padding: 0 0.5em 0 2em;
73
+ font-size: 0.8em;
74
+ color: var(--van-white);
75
+ height: 22px;
76
+ line-height: 22px;
77
+ }
78
+ &.van-switch--on {
79
+ .van-switch__node {
80
+ transform: none;
81
+ left: auto;
82
+ right: 2px;
83
+ }
84
+ .yhm-switch-value-text {
85
+ padding: 0 2em 0 0.5em;
86
+ }
87
+ }
88
+ }
89
+ }
90
+ }
91
+ </style>
package/index.ts ADDED
@@ -0,0 +1,26 @@
1
+ import yhmList from "./info/yhmList.vue";
2
+ import yhmDropdownItem from "./info/yhmDropdownItem.vue";
3
+ import yhmInfo from "./info/yhmInfo.vue";
4
+ import yhmInfoItem from "./info/yhmInfoItem.vue";
5
+ import yhmForm from "./form/yhm-form.vue";
6
+ import yhmDatetime from "./form/yhmDatetime.vue";
7
+ import yhmRadio from "./form/yhmRadio.vue";
8
+ import yhmCheckbox from "./form/yhmCheckbox.vue";
9
+ import yhmSelect from "./form/yhmSelect.vue";
10
+ import yhmInput from "./form/yhmInput.vue";
11
+ import yhmSwitch from "./form/yhmSwitch.vue";
12
+ export default {
13
+ install(app) {
14
+ app.component("yhm-list", yhmList);
15
+ app.component("yhm-dropdown-item", yhmDropdownItem);
16
+ app.component("yhm-info", yhmInfo);
17
+ app.component("yhm-info-item", yhmInfoItem);
18
+ app.component("yhm-form", yhmForm);
19
+ app.component("yhm-datetime", yhmDatetime);
20
+ app.component("yhm-radio", yhmRadio);
21
+ app.component("yhm-checkbox", yhmCheckbox);
22
+ app.component("yhm-input", yhmInput);
23
+ app.component("yhm-select", yhmSelect);
24
+ app.component("yhm-switch", yhmSwitch);
25
+ },
26
+ };
@@ -0,0 +1,35 @@
1
+ <template>
2
+ <van-dropdown-item
3
+ :model-value="modelValue"
4
+ @update:model-value="updateHandler"
5
+ :options="optionData"></van-dropdown-item>
6
+ </template>
7
+ <script setup lang="ts">
8
+ import type { vanOption } from "../types";
9
+ import { computed } from "vue";
10
+
11
+ const props = defineProps<{
12
+ valueObj: any;
13
+ value: string;
14
+ optionData: vanOption[];
15
+ }>();
16
+
17
+ const emits = defineEmits<{
18
+ (e: "change");
19
+ }>();
20
+
21
+ const modelValue = computed({
22
+ get() {
23
+ return props.valueObj[props.value];
24
+ },
25
+ set(val) {
26
+ props.valueObj[props.value] = val;
27
+ },
28
+ });
29
+ function updateHandler(val: string | number) {
30
+ modelValue.value = val;
31
+ emits("change");
32
+ }
33
+ </script>
34
+ <style lang="scss"></style>
35
+ ../types
@@ -0,0 +1,37 @@
1
+ <template>
2
+ <section
3
+ class="yhm-info"
4
+ :class="{
5
+ 'has-top': hasTop,
6
+ }">
7
+ <slot></slot>
8
+ </section>
9
+ </template>
10
+ <script setup lang="ts">
11
+ const props = withDefaults(
12
+ defineProps<{
13
+ hasTop?: boolean;
14
+ }>(),
15
+ {
16
+ hasTop: false,
17
+ }
18
+ );
19
+ </script>
20
+ <style lang="scss">
21
+ .yhm-info {
22
+ display: flex;
23
+ flex-wrap: wrap;
24
+ padding: 4px 8px 4px 16px;
25
+ background-color: var(--van-white);
26
+ box-sizing: border-box;
27
+ &.has-top {
28
+ margin-top: var(--van-padding-xs);
29
+ }
30
+ }
31
+ .yhm-info + .yhm-info {
32
+ border-top: 1px solid var(--van-gray-1);
33
+ &.has-top {
34
+ border-top: 0;
35
+ }
36
+ }
37
+ </style>
@@ -0,0 +1,121 @@
1
+ <template>
2
+ <section :class="`yhm-info-item-container${status ? ' ' + status : ''}`">
3
+ <div :class="`yhm-info-item${regularValue ? ' regular-value' : ''}`">
4
+ <div class="yhm-info-item-label">
5
+ <slot name="label">{{ label }}</slot>
6
+ </div>
7
+ <div class="yhm-info-item-value">
8
+ <slot name="value">{{ value }}</slot>
9
+ </div>
10
+ </div>
11
+ <div
12
+ class="yhm-info-description"
13
+ v-if="description">
14
+ <slot name="description">{{ description }}</slot>
15
+ </div>
16
+ </section>
17
+ </template>
18
+ <script setup lang="ts">
19
+ import { computed, ref } from "vue";
20
+ const props = withDefaults(
21
+ defineProps<{
22
+ span: number;
23
+ label?: string;
24
+ value?: string;
25
+ description?: string;
26
+ regularValue?: boolean;
27
+ status?: "primary" | "success" | "danger" | "warning" | "cyan" | "purple" | "yellow" | "pink";
28
+ }>(),
29
+ {
30
+ span: 24,
31
+ }
32
+ );
33
+
34
+ const widthCount = computed(() => {
35
+ return 24 / props.span;
36
+ });
37
+ </script>
38
+ <style lang="scss">
39
+ .yhm-info-item-container {
40
+ width: calc((100% - (8px * v-bind(widthCount))) / v-bind(widthCount));
41
+ font-size: var(--van-font-size-md);
42
+ padding: 4px 0;
43
+ margin-right: 8px;
44
+ .yhm-info-item {
45
+ display: flex;
46
+ line-height: 24px;
47
+ .yhm-info-item-value {
48
+ flex: 1;
49
+ text-align: right;
50
+ color: var(--van-text-color-2);
51
+ }
52
+ &.regular-value {
53
+ .yhm-info-item-value {
54
+ flex: 1;
55
+ text-align: right;
56
+ color: var(--van-text-color);
57
+ }
58
+ }
59
+ }
60
+ .yhm-info-description {
61
+ color: var(--van-text-color-2);
62
+ line-height: 20px;
63
+ }
64
+ &.primary {
65
+ .yhm-info-item {
66
+ .yhm-info-item-value {
67
+ color: var(--van-primary-color) !important;
68
+ }
69
+ }
70
+ }
71
+ &.success {
72
+ .yhm-info-item {
73
+ .yhm-info-item-value {
74
+ color: var(--van-success-color) !important;
75
+ }
76
+ }
77
+ }
78
+ &.danger {
79
+ .yhm-info-item {
80
+ .yhm-info-item-value {
81
+ color: var(--van-danger-color) !important;
82
+ }
83
+ }
84
+ }
85
+ &.warning {
86
+ .yhm-info-item {
87
+ .yhm-info-item-value {
88
+ color: var(--van-warning-color) !important;
89
+ }
90
+ }
91
+ }
92
+ &.cyan {
93
+ .yhm-info-item {
94
+ .yhm-info-item-value {
95
+ color: var(--van-cyan) !important;
96
+ }
97
+ }
98
+ }
99
+ &.purple {
100
+ .yhm-info-item {
101
+ .yhm-info-item-value {
102
+ color: var(--van-purple) !important;
103
+ }
104
+ }
105
+ }
106
+ &.yellow {
107
+ .yhm-info-item {
108
+ .yhm-info-item-value {
109
+ color: var(--van-yellow) !important;
110
+ }
111
+ }
112
+ }
113
+ &.pink {
114
+ .yhm-info-item {
115
+ .yhm-info-item-value {
116
+ color: var(--van-pink) !important;
117
+ }
118
+ }
119
+ }
120
+ }
121
+ </style>