ice.fo.utils
Version: 
112 lines (99 loc) • 2.97 kB
JavaScript
import _isEmpty from 'lodash/isEmpty'
import _startCase from 'lodash/startCase'
import _isPlainObject from 'lodash/isPlainObject'
export function getInitialComponentData (props) {
  return Object.entries(props).reduce((result, [key, item]) => {
    if (item.doc && item.doc.default) {
      result[key] = item.doc.default
    } else if (item.doc && item.doc.options) {
      result[key] = getInitialComponentData(item.doc.options)
    } else if (item.default != null) {
      result[key] = typeof item.default == 'function' ? item.default() : item.default
    }
    return result
  }, {})
}
export function buildPropsRenderTree (props, values, rootProps) {
  return Object.entries(props).reduce((result, [key, item]) => {
    if (item.doc && item.doc.hidden) {
      return result
    }
    const component = getComponentNameForDocProp(item)
    result.push({
      component,
      fieldName: key,
      fieldType: item.type,
      props: {
        ...getComponentPropsForDocProp(component, item, key, values[key], rootProps || props),
        id: key,
        label: _startCase(key),
        value: values[key],
        description: item.description,
      },
    })
    return result
  }, [])
}
export function getComponentNameForDocProp (prop) {
  if (prop.doc && prop.doc.component) {
    return prop.doc.component
  }
  if (Array.isArray(prop.type)) {
    return prop.type.reduce((result, item) => result || getComponentNameForDocProp(item), '')
  }
  switch (prop.type || prop) {
    case Boolean:
      return 'Radio'
    case String:
      return 'InputText'
    case Number:
      return 'InputNumber'
    default:
      return 'ComponentGuideOptionsGroup'
  }
}
export function getComponentPropsForDocProp (component, prop, fieldName, fieldValue, props) {
  switch (component) {
    case 'Radio':
    case 'Select':
      return {
        multiple: prop.multiple,
        list: prop.type == Boolean || typeof prop.default == 'boolean'
          ? [
              { label: 'On', value: true },
              { label: 'Off', value: false },
            ]
          : (prop.doc && (typeof prop.doc.list == 'function' ? prop.doc.list(props) : prop.doc.list)),
      }
    case 'InputText':
    case 'InputNumber':
      return {
        max: prop.doc && prop.doc.max,
        min: prop.doc && prop.doc.min,
      }
    case 'ComponentGuideOptionsGroup':
      return {
        groupProps: prop.doc && prop.doc.options,
        rootProps: props,
      }
    default:
      return null
  }
}
export function cleanEmptyPropValues (target) {
  const result = {}
  for (const k in target) {
    if (typeof target[k] != 'number' && typeof target[k] != 'boolean' && _isEmpty(target[k])) {
      continue
    }
    if (_isPlainObject(target[k])) {
      const value = cleanEmptyPropValues(target[k])
      if (value != null) {
        result[k] = value
      }
    } else {
      result[k] = target[k]
    }
  }
  return _isEmpty(result) ? null : result
}