utils.mjs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { ref, computed } from 'vue';
  2. import '../../../utils/index.mjs';
  3. import { debugWarn } from '../../../utils/error.mjs';
  4. import { castArray } from 'lodash-unified';
  5. const SCOPE = "ElForm";
  6. function useFormLabelWidth() {
  7. const potentialLabelWidthArr = ref([]);
  8. const autoLabelWidth = computed(() => {
  9. if (!potentialLabelWidthArr.value.length)
  10. return "0";
  11. const max = Math.max(...potentialLabelWidthArr.value);
  12. return max ? `${max}px` : "";
  13. });
  14. function getLabelWidthIndex(width) {
  15. const index = potentialLabelWidthArr.value.indexOf(width);
  16. if (index === -1 && autoLabelWidth.value === "0") {
  17. debugWarn(SCOPE, `unexpected width ${width}`);
  18. }
  19. return index;
  20. }
  21. function registerLabelWidth(val, oldVal) {
  22. if (val && oldVal) {
  23. const index = getLabelWidthIndex(oldVal);
  24. potentialLabelWidthArr.value.splice(index, 1, val);
  25. } else if (val) {
  26. potentialLabelWidthArr.value.push(val);
  27. }
  28. }
  29. function deregisterLabelWidth(val) {
  30. const index = getLabelWidthIndex(val);
  31. if (index > -1) {
  32. potentialLabelWidthArr.value.splice(index, 1);
  33. }
  34. }
  35. return {
  36. autoLabelWidth,
  37. registerLabelWidth,
  38. deregisterLabelWidth
  39. };
  40. }
  41. const filterFields = (fields, props) => {
  42. const normalized = castArray(props);
  43. return normalized.length > 0 ? fields.filter((field) => field.prop && normalized.includes(field.prop)) : fields;
  44. };
  45. export { filterFields, useFormLabelWidth };
  46. //# sourceMappingURL=utils.mjs.map