form-value-store.js 891 B

1234567891011121314151617181920212223242526272829303132
  1. import dayjs from 'dayjs'
  2. import { getFieldValue, isNotEmpty } from '@cip/utils/util'
  3. const state = {}
  4. export const settingValueTransformState = (key, value) => {
  5. state[key] = value
  6. }
  7. // basic-input 组件默认值模版
  8. export const valueOptions = [
  9. /* eslint-disable no-template-curly-in-string */
  10. '${user.displayName}',
  11. '${user.email}',
  12. '${user.group.name}',
  13. '${new Date()}'
  14. /* eslint-enable */
  15. ]
  16. export const getValueByTemplate = (template, config) => {
  17. // 不能完全匹配template的数据 直接返回原始值
  18. if (typeof template !== 'string') {
  19. return template
  20. }
  21. return template?.replace(/\${([^}]+)}/g, (_, key) => {
  22. if (key === 'new Date()') {
  23. return dayjs(Date.now()).format(config?.formatter || 'YYYY-MM-DD HH:mm:ss')
  24. } else {
  25. const val = getFieldValue(state, key)
  26. return isNotEmpty(val) ? val : `\${${key}}`
  27. }
  28. })
  29. }