HrPickerRow.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <view class="hr-picker-row">
  3. <!-- 枚举单选:原生 selector(与「个人档案」页完全一致的外观与交互) -->
  4. <picker
  5. v-if="mode === 'selector'"
  6. mode="selector"
  7. :range="range"
  8. range-key="label"
  9. :value="currentIndex"
  10. :disabled="disabled"
  11. @change="onPickerChange"
  12. >
  13. <view class="hr-field" :class="[{ press: !disabled }, { 'is-disabled': disabled }]" :style="rowStyle">
  14. <text class="hr-label" :class="{ required: required }" :style="labelStyle">{{ label }}</text>
  15. <text :class="shownText ? 'hr-value' : 'hr-placeholder'">{{ shownText || emptyTip }}</text>
  16. <text v-if="!disabled" class="hr-arrow">›</text>
  17. </view>
  18. </picker>
  19. <!-- 层级选择:树形选择器(部门等,层级不限) -->
  20. <view v-else-if="mode === 'tree'" class="hr-field" :class="[{ press: !disabled }, { 'is-disabled': disabled }]" :style="rowStyle" @click="openTree">
  21. <text class="hr-label" :class="{ required: required }" :style="labelStyle">{{ label }}</text>
  22. <text :class="shownText ? 'hr-value' : 'hr-placeholder'">{{ shownText || emptyTip }}</text>
  23. <text v-if="!disabled" class="hr-arrow">›</text>
  24. </view>
  25. <!-- 自定义流程(如「先输关键字再选人」):只渲染行,点击事件交给页面 -->
  26. <view v-else class="hr-field" :class="[{ press: !disabled }, { 'is-disabled': disabled }]" :style="rowStyle" @click="onCustomTap">
  27. <text class="hr-label" :class="{ required: required }" :style="labelStyle">{{ label }}</text>
  28. <text :class="shownText ? 'hr-value' : 'hr-placeholder'">{{ shownText || emptyTip }}</text>
  29. <text v-if="!disabled" class="hr-arrow">›</text>
  30. </view>
  31. <ba-tree-picker
  32. v-if="mode === 'tree'"
  33. ref="tree"
  34. :multiple="false"
  35. :localdata="tree"
  36. valueKey="id"
  37. textKey="name"
  38. childrenKey="children"
  39. :title="treeTitle || label"
  40. :border="true"
  41. confirm-color="#157a2c"
  42. switch-color="#157a2c"
  43. @select-change="onTreeSelect"
  44. />
  45. </view>
  46. </template>
  47. <script>
  48. /**
  49. * HR 统一选择行(口径对齐「个人档案」页):
  50. * mode="selector"(默认):原生 <picker mode="selector"> 底部滚轮,选项 [{ label, value }]
  51. * mode="tree":ba-tree-picker 树形选择(部门等不定层级),树节点 { id, name, children }
  52. * mode="custom":只渲染行并把点击抛给页面(用于「先输关键字再选人」这类自定义流程)
  53. * 只负责「选什么」,级联副作用由使用方在 @change 里处理。
  54. */
  55. import baTreePicker from "@/components/ba-tree-picker/ba-tree-picker.vue";
  56. export default {
  57. name: "HrPickerRow",
  58. components: { baTreePicker },
  59. props: {
  60. label: { type: String, default: "" },
  61. required: { type: Boolean, default: false },
  62. /** 当前值(枚举为主键/编码;树为节点 id) */
  63. value: { type: [String, Number], default: "" },
  64. /** 显示文本:不传则用 value */
  65. display: { type: String, default: "" },
  66. options: { type: Array, default: () => [] },
  67. placeholder: { type: String, default: "请选择" },
  68. /** 前置条件未满足时的提示(如「请先选择所属机构」);**只在 disabled 时显示** */
  69. hint: { type: String, default: "" },
  70. disabled: { type: Boolean, default: false },
  71. /**
  72. * 行度量(内联样式下发,与宿主页面的 .field 保持一致):
  73. * 各页 .field 的内边距 / 行高 / 标签宽并不相同,组件默认值只对个人档案页成立;
  74. * 用 prop 而不是 /deep/ 覆盖,规避 scoped CSS 作用不到子组件内部的问题。
  75. */
  76. rowMinHeight: { type: String, default: "96rpx" },
  77. rowPadding: { type: String, default: "" },
  78. labelWidth: { type: String, default: "190rpx" },
  79. /** mode="tree" 专用 */
  80. mode: { type: String, default: "selector" },
  81. tree: { type: Array, default: () => [] },
  82. treeTitle: { type: String, default: "" },
  83. },
  84. emits: ["change", "tap"],
  85. computed: {
  86. /** 兼容 [{label,value}]、[{name,id}]、["a","b"] 三种写法 */
  87. range() {
  88. return (this.options || []).map((item) => {
  89. if (item && typeof item === "object") {
  90. return {
  91. label: String(item.label != null ? item.label : item.name != null ? item.name : item.value),
  92. value: item.value != null ? item.value : item.id,
  93. };
  94. }
  95. return { label: String(item), value: item };
  96. });
  97. },
  98. /** 行内度量:只下发显式设置过的属性 */
  99. rowStyle() {
  100. const style = { "min-height": this.rowMinHeight };
  101. if (this.rowPadding) style.padding = this.rowPadding;
  102. return style;
  103. },
  104. labelStyle() {
  105. return { width: this.labelWidth };
  106. },
  107. shownText() {
  108. if (this.display) return this.display;
  109. return this.value === undefined || this.value === null || this.value === "" ? "" : String(this.value);
  110. },
  111. /**
  112. * 无值时的提示文案。
  113. * 注意:hint 是「前置条件未满足」的说明(如「请先选择所属机构」),
  114. * **只在禁用状态下显示**;前置条件已满足、只是还没选时,应显示通用占位「请选择」,
  115. * 否则会出现「已选好所属机构,目标部门却仍提示请先选择所属机构」的误导。
  116. */
  117. emptyTip() {
  118. if (this.disabled && this.hint) return this.hint;
  119. return this.placeholder;
  120. },
  121. currentIndex() {
  122. const idx = this.range.findIndex((item) => String(item.value) === String(this.value));
  123. return idx < 0 ? 0 : idx;
  124. },
  125. },
  126. methods: {
  127. onPickerChange(e) {
  128. const picked = this.range[(e && e.detail && e.detail.value) || 0];
  129. if (!picked) return;
  130. this.$emit("change", picked.value, picked);
  131. },
  132. openTree() {
  133. if (this.disabled) return;
  134. // 没有可选项时不打开空弹层,给个提示
  135. if (!this.tree || !this.tree.length) {
  136. if (typeof uni !== "undefined" && uni.showToast) {
  137. uni.showToast({ title: this.hint || "暂无可选项", icon: "none" });
  138. }
  139. return;
  140. }
  141. const picker = this.$refs.tree;
  142. if (picker && typeof picker._show === "function") picker._show();
  143. },
  144. /** mode="custom":自定义流程,只把点击抛给页面处理 */
  145. onCustomTap() {
  146. if (this.disabled) return;
  147. this.$emit("tap");
  148. },
  149. onTreeSelect(ids, names) {
  150. const id = Array.isArray(ids) ? ids[0] : ids;
  151. if (id === undefined || id === null || id === "") return;
  152. this.$emit("change", id, { value: id, label: names || "" });
  153. },
  154. },
  155. };
  156. </script>
  157. <style lang="scss" scoped>
  158. /* 行样式与 pages/hr/profile/edit.vue 的 .field 保持一致 */
  159. .hr-field {
  160. display: flex;
  161. align-items: center;
  162. min-height: 96rpx;
  163. font-size: 28rpx;
  164. .hr-label {
  165. width: 190rpx;
  166. flex: none;
  167. /* 标签不换行:宽度由页面 labelWidth 决定,换行会把行撑成两行高 */
  168. white-space: nowrap;
  169. color: #4b5563;
  170. }
  171. .hr-label.required::before {
  172. content: "*";
  173. color: #ee4d4d;
  174. margin-right: 4rpx;
  175. }
  176. .hr-value,
  177. .hr-placeholder {
  178. flex: 1;
  179. min-width: 0;
  180. text-align: right;
  181. color: #1f2329;
  182. font-size: 28rpx;
  183. }
  184. .hr-placeholder {
  185. color: #b5bac3;
  186. }
  187. .hr-arrow {
  188. flex: none;
  189. margin-left: 12rpx;
  190. color: #c4c8ce;
  191. font-size: 32rpx;
  192. line-height: 1;
  193. }
  194. }
  195. .hr-field.is-disabled {
  196. opacity: 0.6;
  197. }
  198. </style>