common.wxs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var util = require("./util.wxs")
  2. var getOptions = function(options, fieldNames) {
  3. var isObject = util.type(fieldNames) === 'object'
  4. var labelName = isObject ? fieldNames.label : 'label'
  5. var valueName = isObject ? fieldNames.value : 'value'
  6. return options.map(function(option, index) {
  7. if (util.type(option) === 'string') {
  8. var 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. }).map(function(option, index) {
  17. // 因为 wx:key 使用动态绑定会有 warn 提示,所以在这里使用内置的字段
  18. option['__comp_unique_key'] =
  19. option[valueName] !== undefined ? option[valueName] : index
  20. return option
  21. })
  22. }
  23. var getValue = function(values, type) {
  24. if (type === 'radio') {
  25. if (util.type(values) === 'array') {
  26. return values[0] || ''
  27. }
  28. return values || ''
  29. }
  30. return values || []
  31. }
  32. var getChecked = function(values, value, type) {
  33. if (type === 'radio') {
  34. return getValue(values, type) === value
  35. }
  36. return getValue(values, type).indexOf(value) !== -1
  37. }
  38. module.exports = {
  39. getOptions: getOptions,
  40. getValue: getValue,
  41. getChecked: getChecked
  42. }