以下为完整代码,可在控件商城导入

    1. /* eslint-disable react/react-in-jsx-scope */
    2. /* eslint-disable no-undef */
    3. // eslint-disable-next-line @typescript-eslint/no-var-requires
    4. const { Stepper } = require('antd-mobile');
    5. const types = {
    6. type: 'SAMPLE_STEPPER_WIDGET',
    7. icon: 'https://static.codemao.cn/appcraft/extension-widgets/production/stepper-ui2.svg',
    8. title: '步进器',
    9. platforms: ['web', 'android', 'ios'],
    10. isInvisibleWidget: false,
    11. isGlobalWidget: false,
    12. properties: [
    13. {
    14. key: '__width',
    15. label: '宽度',
    16. valueType: 'number',
    17. defaultValue: 90,
    18. },
    19. {
    20. key: '__height',
    21. label: '高度',
    22. valueType: 'number',
    23. defaultValue: 36,
    24. },
    25. {
    26. key: 'count',
    27. label: '当前值',
    28. valueType: 'number',
    29. defaultValue: 0,
    30. },
    31. {
    32. key: 'min',
    33. label: '最小值',
    34. valueType: 'number',
    35. defaultValue: 0,
    36. },
    37. {
    38. key: 'max',
    39. label: '最大值',
    40. valueType: 'number',
    41. defaultValue: 100,
    42. },
    43. {
    44. key: 'step',
    45. label: '步幅',
    46. valueType: 'number',
    47. defaultValue: 1,
    48. },
    49. {
    50. key: 'disabled',
    51. label: '禁用',
    52. valueType: 'boolean',
    53. defaultValue: false,
    54. },
    55. {
    56. key: 'inputFontSize',
    57. label: '输入框字体大小',
    58. valueType: 'number',
    59. defaultValue: 14,
    60. },
    61. {
    62. key: 'inputFontColor',
    63. label: '输入框字体颜色',
    64. valueType: 'color',
    65. defaultValue: '#000000',
    66. },
    67. {
    68. key: 'borderRadius',
    69. label: '圆角',
    70. valueType: 'number',
    71. defaultValue: 2,
    72. },
    73. {
    74. key: 'borderWidth',
    75. label: '边框粗细',
    76. valueType: 'number',
    77. defaultValue: 1,
    78. },
    79. {
    80. key: 'borderColor',
    81. label: '边框颜色',
    82. valueType: 'color',
    83. defaultValue: '#cccccc',
    84. },
    85. {
    86. key: 'buttonWidth',
    87. label: '按钮宽度',
    88. valueType: 'number',
    89. defaultValue: 22,
    90. },
    91. {
    92. key: 'buttonBgColor',
    93. label: '按钮背景颜色',
    94. valueType: 'color',
    95. defaultValue: '#ffffff',
    96. },
    97. {
    98. key: 'buttonTextColor',
    99. label: '按钮文本颜色',
    100. valueType: 'color',
    101. defaultValue: '#1677ff',
    102. },
    103. {
    104. key: 'buttonFontSize',
    105. label: '按钮字体大小',
    106. valueType: 'number',
    107. defaultValue: 14,
    108. },
    109. ],
    110. methods: [
    111. {
    112. key: 'decrease',
    113. label: '减小',
    114. params: [],
    115. },
    116. {
    117. key: 'increase',
    118. label: '增加',
    119. params: [],
    120. },
    121. ],
    122. events: [
    123. {
    124. key: 'onChange',
    125. label: '变化',
    126. params: [
    127. {
    128. key: 'count',
    129. label: '当前值',
    130. valueType: 'number',
    131. },
    132. ],
    133. },
    134. {
    135. key: 'onFocus',
    136. label: '获得焦点',
    137. params: [],
    138. },
    139. {
    140. key: 'onBlur',
    141. label: '失去焦点',
    142. params: [],
    143. },
    144. ],
    145. };
    146. class StepperWidget extends VisibleWidget {
    147. constructor(props) {
    148. super(props);
    149. this.count = props.count;
    150. this.max = props.max;
    151. this.min = props.min;
    152. this.step = props.step;
    153. this.disabled = props.disabled;
    154. this.inputFontSize = props.inputFontSize;
    155. this.inputFontColor = props.inputFontColor;
    156. this.borderRadius = props.borderRadius;
    157. this.borderWidth = props.borderWidth;
    158. this.borderColor = props.borderColor;
    159. this.buttonWidth = props.buttonWidth;
    160. this.buttonBgColor = props.buttonBgColor;
    161. this.buttonTextColor = props.buttonTextColor;
    162. this.buttonFontSize = props.buttonFontSize;
    163. }
    164. decrease = () => {
    165. this.setProps({
    166. count: this.count - 1,
    167. });
    168. this.emit('onChange', this.count);
    169. };
    170. increase = () => {
    171. this.setProps({
    172. count: this.count + 1,
    173. });
    174. this.emit('onChange', this.count);
    175. };
    176. onChange = (value) => {
    177. this.setProps({
    178. count: value,
    179. });
    180. };
    181. onFocus = () => {
    182. this.emit('onFocus');
    183. };
    184. onBlur = () => {
    185. this.emit('onBlur');
    186. };
    187. render() {
    188. const height = this.__height;
    189. return (
    190. <Stepper
    191. value={this.count}
    192. onChange={this.onChange}
    193. onFocus={this.onFocus}
    194. onBlur={this.onBlur}
    195. disabled={this.disabled}
    196. max={this.max}
    197. min={this.min}
    198. step={this.step}
    199. style={{
    200. // height: '100%',
    201. width: '100%',
    202. '--height': `${height}px`,
    203. '--input-font-size': `${this.inputFontSize}px`,
    204. '--input-font-color': this.inputFontColor,
    205. '--border-radius': `${this.borderRadius}px`,
    206. '--border': `${this.borderWidth}px solid ${this.borderColor}`,
    207. '--button-width': `${this.buttonWidth}px`,
    208. '--button-background-color': this.buttonBgColor,
    209. '--button-text-color': this.buttonTextColor,
    210. '--button-font-size': `${this.buttonFontSize}px`,
    211. }}
    212. />
    213. );
    214. }
    215. }
    216. exports.types = types;
    217. exports.widget = StepperWidget;