index.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import type { Dayjs } from 'dayjs'
  2. import type { StdDesignEdit } from '@/components/StdDesign/types'
  3. import {
  4. DatePicker,
  5. Input,
  6. InputNumber,
  7. RangePicker,
  8. Switch,
  9. } from 'ant-design-vue'
  10. import dayjs from 'dayjs'
  11. import { h } from 'vue'
  12. import { DATE_FORMAT } from '@/constants'
  13. import StdPassword from './components/StdPassword.vue'
  14. import StdSelect from './components/StdSelect.vue'
  15. import StdSelector from './components/StdSelector.vue'
  16. import StdDataEntry from './StdDataEntry.vue'
  17. // eslint-disable-next-line ts/no-explicit-any
  18. export function readonly(edit: StdDesignEdit, dataSource: any, dataIndex: any) {
  19. return h('p', dataSource?.[dataIndex] ?? edit?.config?.defaultValue)
  20. }
  21. export function labelRender(title?: string | (() => string)) {
  22. if (typeof title === 'function')
  23. return title()
  24. return title
  25. }
  26. export function placeholderHelper(edit: StdDesignEdit) {
  27. return typeof edit.config?.placeholder === 'function' ? edit.config?.placeholder() : edit.config?.placeholder
  28. }
  29. // eslint-disable-next-line ts/no-explicit-any
  30. export function input(edit: StdDesignEdit, dataSource: any, dataIndex: any) {
  31. return h(Input, {
  32. 'autocomplete': 'off',
  33. 'placeholder': placeholderHelper(edit),
  34. 'value': dataSource?.[dataIndex] ?? edit?.config?.defaultValue,
  35. 'onUpdate:value': value => {
  36. dataSource[dataIndex] = value
  37. },
  38. })
  39. }
  40. // eslint-disable-next-line ts/no-explicit-any
  41. export function inputNumber(edit: StdDesignEdit, dataSource: any, dataIndex: any) {
  42. if (edit.config?.defaultValue !== undefined)
  43. dataSource[dataIndex] = edit.config.defaultValue
  44. return h(InputNumber, {
  45. 'placeholder': placeholderHelper(edit),
  46. 'min': edit.config?.min,
  47. 'max': edit.config?.max,
  48. 'value': dataSource?.[dataIndex] ?? edit?.config?.defaultValue,
  49. 'onUpdate:value': value => {
  50. dataSource[dataIndex] = value
  51. },
  52. 'addon-before': edit.config?.addonBefore,
  53. 'addon-after': edit.config?.addonAfter,
  54. 'prefix': edit.config?.prefix,
  55. 'suffix': edit.config?.suffix,
  56. })
  57. }
  58. // eslint-disable-next-line ts/no-explicit-any
  59. export function textarea(edit: StdDesignEdit, dataSource: any, dataIndex: any) {
  60. if (!dataSource[dataIndex])
  61. dataSource[dataIndex] = edit.config?.defaultValue
  62. return (
  63. <Input
  64. v-model:value={dataSource[dataIndex]}
  65. placeholder={placeholderHelper(edit)}
  66. />
  67. )
  68. }
  69. // eslint-disable-next-line ts/no-explicit-any
  70. export function password(edit: StdDesignEdit, dataSource: any, dataIndex: any) {
  71. return (
  72. <StdPassword
  73. v-model:value={dataSource[dataIndex]}
  74. value={dataSource[dataIndex] ?? edit?.config?.defaultValue}
  75. generate={edit.config?.generate}
  76. placeholder={placeholderHelper(edit)}
  77. />
  78. )
  79. }
  80. // eslint-disable-next-line ts/no-explicit-any
  81. export function select(edit: StdDesignEdit, dataSource: any, dataIndex: any) {
  82. const actualDataIndex = edit?.actualDataIndex ?? dataIndex
  83. return (
  84. <StdSelect
  85. v-model:value={dataSource[actualDataIndex]}
  86. mask={edit.mask}
  87. placeholder={placeholderHelper(edit)}
  88. multiple={edit.select?.multiple}
  89. defaultValue={edit.config?.defaultValue}
  90. />
  91. )
  92. }
  93. // eslint-disable-next-line ts/no-explicit-any
  94. export function selector(edit: StdDesignEdit, dataSource: any, dataIndex: any) {
  95. return (
  96. <StdSelector
  97. v-model:selectedKey={dataSource[dataIndex]}
  98. selectedKey={dataSource[dataIndex] || edit?.config?.defaultValue}
  99. recordValueIndex={edit.selector?.recordValueIndex}
  100. selectionType={edit.selector?.selectionType ?? 'radio'}
  101. api={edit.selector?.api}
  102. columns={edit.selector?.columns}
  103. disableSearch={edit.selector?.disableSearch}
  104. getParams={edit.selector?.getParams}
  105. description={edit.selector?.description}
  106. getCheckboxProps={edit.selector?.getCheckboxProps}
  107. expandAll={edit.selector?.expandAll}
  108. />
  109. )
  110. }
  111. // eslint-disable-next-line ts/no-explicit-any
  112. export function switcher(edit: StdDesignEdit, dataSource: any, dataIndex: any) {
  113. return h(Switch, {
  114. 'checked': dataSource?.[dataIndex] ?? edit?.config?.defaultValue,
  115. // eslint-disable-next-line ts/no-explicit-any
  116. 'onUpdate:checked': (value: any) => {
  117. dataSource[dataIndex] = value
  118. },
  119. })
  120. }
  121. // eslint-disable-next-line ts/no-explicit-any
  122. export function datePicker(edit: StdDesignEdit, dataSource: any, dataIndex: any) {
  123. const date: Dayjs | undefined = dataSource?.[dataIndex] ? dayjs.unix(dataSource?.[dataIndex]) : undefined
  124. return (
  125. <DatePicker
  126. allowClear
  127. format={edit?.datePicker?.format ?? DATE_FORMAT}
  128. picker={edit?.datePicker?.picker}
  129. value={date}
  130. onChange={(_, dataString) => dataSource[dataIndex] = dayjs(dataString).unix() ?? undefined}
  131. />
  132. )
  133. }
  134. // eslint-disable-next-line ts/no-explicit-any
  135. export function dateRangePicker(edit: StdDesignEdit, dataSource: any, dataIndex: any) {
  136. const dates: [Dayjs, Dayjs] = dataSource
  137. ?.[dataIndex]
  138. ?.filter((item: string) => !!item)
  139. ?.map((item: string) => dayjs(item))
  140. return (
  141. <RangePicker
  142. allowClear
  143. format={edit?.datePicker?.format ?? DATE_FORMAT}
  144. picker={edit?.datePicker?.picker}
  145. value={dates}
  146. onChange={(_, dateStrings: [string, string]) => dataSource[dataIndex] = dateStrings}
  147. />
  148. )
  149. }
  150. export default StdDataEntry