common.js 1020 B

1234567891011121314151617181920212223242526272829303132333435
  1. import * as util from './util.js'
  2. export function getOptions(options, fieldNames) {
  3. const isObject = util.type(fieldNames) === 'object'
  4. const labelName = isObject ? fieldNames.label : 'label'
  5. const valueName = isObject ? fieldNames.value : 'value'
  6. return options.map(function (option, index) {
  7. if (util.type(option) === 'string') {
  8. const newOption = {}
  9. newOption.index = index
  10. newOption[labelName] = option
  11. newOption[valueName] = option
  12. return newOption
  13. }
  14. option.index = index
  15. return option
  16. })
  17. }
  18. export function getValue(values, type) {
  19. if (type === 'radio') {
  20. if (util.type(values) === 'array') {
  21. return values[0] || ''
  22. }
  23. return values || ''
  24. }
  25. return values || []
  26. }
  27. export function getChecked(values, value, type) {
  28. if (type === 'radio') {
  29. return getValue(values, type) === value
  30. }
  31. return getValue(values, type).indexOf(value) !== -1
  32. }