utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { defaultIcon } from '../prompt/props'
  2. export const POPUP_SELECTOR = '#wux-select'
  3. export const getDefaultProps = () => ({
  4. value: {
  5. type: [String, Array],
  6. value: '',
  7. },
  8. options: {
  9. type: Array,
  10. value: [],
  11. },
  12. iconPosition: {
  13. type: String,
  14. value: '',
  15. },
  16. multiple: {
  17. type: Boolean,
  18. value: false,
  19. },
  20. max: {
  21. type: Number,
  22. value: -1,
  23. },
  24. })
  25. export const notFoundContent = {
  26. icon: defaultIcon,
  27. title: '',
  28. text: '暂无数据',
  29. }
  30. export const getNotFoundContent = (newVal) => {
  31. if (newVal !== null && typeof newVal === 'object') {
  32. return Object.assign({}, notFoundContent, newVal)
  33. } else if (typeof newVal === 'string') {
  34. return Object.assign({}, notFoundContent, {
  35. text: newVal,
  36. })
  37. } else if (newVal === null || newVal === false) {
  38. return null
  39. }
  40. return notFoundContent
  41. }
  42. export function convertValue(value) {
  43. return Array.isArray(value) ? [...value] : typeof value === 'string' ? [value] : []
  44. }
  45. export function getSelectIndex(options = [], value = '', multiple = false) {
  46. const newValue = convertValue(value)
  47. const values = options.map((n) => n.value || n).filter((n) => !!n)
  48. if (!multiple) return values.indexOf(newValue[0])
  49. return newValue.map((n) => values.indexOf(n))
  50. }
  51. export function getRealValue(options = [], value = '', multiple = false) {
  52. const newValue = convertValue(value)
  53. const values = options.map((n) => n.value || n).filter((n) => !!n)
  54. if (!multiple) {
  55. if (values.includes(newValue[0])) {
  56. return newValue[0]
  57. }
  58. return ''
  59. }
  60. return newValue.filter((n) => values.includes(n))
  61. }
  62. export const DEFAULT_FIELD_NAMES = {
  63. title: 'title',
  64. value: 'value',
  65. options: 'options',
  66. }
  67. export function fillFieldNames(fieldNames = {}) {
  68. const { title, value, options } = { ...DEFAULT_FIELD_NAMES, ...fieldNames }
  69. return {
  70. title: title || DEFAULT_FIELD_NAMES.title,
  71. value: value || DEFAULT_FIELD_NAMES.value,
  72. options: options || DEFAULT_FIELD_NAMES.options,
  73. }
  74. }
  75. export function flattenOptions(options, { fieldNames = DEFAULT_FIELD_NAMES } = {}) {
  76. const flattenList = []
  77. const {
  78. title: fieldTitle,
  79. value: fieldValue,
  80. options: fieldOptions,
  81. } = fillFieldNames(fieldNames)
  82. function dig(list, isGroupOption) {
  83. list.forEach((data) => {
  84. data = typeof data === 'string' ? ({ [fieldTitle]: data, [fieldValue]: data }) : data
  85. const title = data[fieldTitle]
  86. if (isGroupOption || !(fieldOptions in data)) {
  87. const value = data[fieldValue]
  88. // Option
  89. flattenList.push({
  90. ...data,
  91. isGroupOption,
  92. data,
  93. title,
  94. value,
  95. })
  96. } else {
  97. let grpTitle = title
  98. if (grpTitle === undefined) {
  99. grpTitle = data.title
  100. }
  101. // Option Group
  102. flattenList.push({
  103. ...data,
  104. isGroup: true,
  105. data,
  106. title: grpTitle,
  107. })
  108. dig(data[fieldOptions], true)
  109. }
  110. })
  111. }
  112. dig(options, false)
  113. return flattenList
  114. }