index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/libs/classNames'
  3. import styleToCssString from '../helpers/libs/styleToCssString'
  4. import { isPresetColor } from '../helpers/colors'
  5. import eventsMixin from '../helpers/mixins/eventsMixin'
  6. baseComponent({
  7. useField: true,
  8. externalClasses: ['wux-input-class'],
  9. behaviors: [eventsMixin()],
  10. relations: {
  11. '../field/index': {
  12. type: 'ancestor',
  13. },
  14. },
  15. properties: {
  16. prefixCls: {
  17. type: String,
  18. value: 'wux-selectable',
  19. },
  20. type: {
  21. type: String,
  22. value: 'checkbox',
  23. },
  24. value: {
  25. type: String,
  26. value: '',
  27. },
  28. defaultChecked: {
  29. type: Boolean,
  30. value: false,
  31. },
  32. checked: {
  33. type: Boolean,
  34. value: false,
  35. observer(newVal) {
  36. if (this.data.controlled) {
  37. this.updated(newVal)
  38. }
  39. },
  40. },
  41. disabled: {
  42. type: Boolean,
  43. value: false,
  44. },
  45. readOnly: {
  46. type: Boolean,
  47. value: false,
  48. },
  49. color: {
  50. type: String,
  51. value: 'balanced',
  52. observer(newVal) {
  53. this.setData({
  54. inputColor: isPresetColor(newVal),
  55. })
  56. },
  57. },
  58. controlled: {
  59. type: Boolean,
  60. value: false,
  61. },
  62. wrapStyle: {
  63. type: [String, Object],
  64. value: '',
  65. observer(newVal) {
  66. this.setData({
  67. extStyle: styleToCssString(newVal),
  68. })
  69. },
  70. },
  71. iconSize: {
  72. type: String,
  73. value: '',
  74. },
  75. iconOn: {
  76. type: String,
  77. value: '',
  78. },
  79. iconOff: {
  80. type: String,
  81. value: '',
  82. },
  83. },
  84. data: {
  85. inputChecked: false,
  86. inputColor: '',
  87. extStyle: '',
  88. innerIconSize: '23',
  89. innerIconOn: 'success',
  90. innerIconOff: 'circle',
  91. },
  92. observers: {
  93. ['type, iconSize, iconOn, iconOff'](type, iconSize, iconOn, iconOff) {
  94. const useDefaultSize = iconSize === ''
  95. const useDefaultIcon = iconOn === '' && iconOff === ''
  96. if (type === 'checkbox') {
  97. this.setData({
  98. innerIconSize: useDefaultSize ? 23 : parseInt(iconSize),
  99. innerIconOn: useDefaultIcon ? 'success' : iconOn,
  100. innerIconOff: useDefaultIcon ? 'circle' : iconOff,
  101. })
  102. } else if (type === 'radio') {
  103. this.setData({
  104. innerIconSize: useDefaultSize ? 16 : parseInt(iconSize),
  105. innerIconOn: useDefaultIcon ? 'success_no_circle' : iconOn,
  106. innerIconOff: useDefaultIcon ? '' : iconOff,
  107. })
  108. }
  109. },
  110. },
  111. computed: {
  112. classes: ['prefixCls, inputChecked, disabled, readOnly', function(prefixCls, inputChecked, disabled, readOnly) {
  113. const wrap = classNames(prefixCls, {
  114. [`${prefixCls}--checked`]: inputChecked,
  115. [`${prefixCls}--disabled`]: disabled,
  116. [`${prefixCls}--readonly`]: readOnly,
  117. })
  118. const input = `${prefixCls}__input`
  119. const icon = `${prefixCls}__icon`
  120. return {
  121. wrap,
  122. input,
  123. icon,
  124. }
  125. }],
  126. },
  127. methods: {
  128. updated(inputChecked) {
  129. if (this.hasFieldDecorator) return
  130. if (this.data.inputChecked !== inputChecked) {
  131. this.setData({
  132. inputChecked,
  133. })
  134. }
  135. },
  136. onChange() {
  137. const { value, inputChecked, disabled, readOnly, controlled } = this.data
  138. const item = {
  139. checked: !inputChecked,
  140. value,
  141. type: 'checkbox',
  142. }
  143. if (disabled || readOnly) return
  144. if (!controlled) {
  145. this.updated(!inputChecked)
  146. }
  147. this.triggerEvent('change', item)
  148. },
  149. },
  150. attached() {
  151. const { defaultChecked, checked, controlled } = this.data
  152. const inputChecked = controlled ? checked : defaultChecked
  153. const inputColor = isPresetColor(this.data.color)
  154. this.setData({
  155. inputChecked,
  156. inputColor,
  157. })
  158. },
  159. })