index.vue 11.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
<script>
fxy060608's avatar
fxy060608 已提交
2 3 4 5 6 7 8 9
import {
  t
} from 'uni-core/helpers/i18n'
import {
  hover,
  emitter,
  listeners
} from 'uni-mixins'
fxy060608's avatar
fxy060608 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
export default {
  name: 'Button',
  mixins: [hover, emitter, listeners],
  props: {
    hoverClass: {
      type: String,
      default: 'button-hover'
    },
    disabled: {
      type: [Boolean, String],
      default: false
    },
    id: {
      type: String,
      default: ''
    },
    hoverStopPropagation: {
      type: Boolean,
      default: false
    },
    hoverStartTime: {
31
      type: [Number, String],
fxy060608's avatar
fxy060608 已提交
32 33 34
      default: 20
    },
    hoverStayTime: {
35
      type: [Number, String],
fxy060608's avatar
fxy060608 已提交
36 37 38 39 40 41 42 43 44
      default: 70
    },
    formType: {
      type: String,
      default: '',
      validator (value) {
        // 只有这几个可取值,其它都是非法的。
        return ~['', 'submit', 'reset'].indexOf(value)
      }
雪洛's avatar
雪洛 已提交
45 46 47 48
    },
    openType: {
      type: String,
      default: ''
fxy060608's avatar
fxy060608 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    }
  },
  data () {
    return {
      clickFunction: null
    }
  },
  methods: {
    _onClick ($event, isLabelClick) {
      if (this.disabled) {
        return
      }
      if (isLabelClick) {
        this.$el.click()
      }
      // TODO 通知父表单执行相应的行为
      if (this.formType) {
雪洛's avatar
雪洛 已提交
66 67
        this.$dispatch(
          'Form',
fxy060608's avatar
fxy060608 已提交
68
          this.formType === 'submit' ? 'uni-form-submit' : 'uni-form-reset', {
雪洛's avatar
雪洛 已提交
69 70 71 72 73
            type: this.formType
          }
        )
        return
      }
74
      if (this.openType === 'feedback' && __PLATFORM__ === 'app-plus') {
雪洛's avatar
雪洛 已提交
75 76
        const feedback = plus.webview.create(
          'https://service.dcloud.net.cn/uniapp/feedback.html',
fxy060608's avatar
fxy060608 已提交
77
          'feedback', {
雪洛's avatar
雪洛 已提交
78
            titleNView: {
fxy060608's avatar
fxy060608 已提交
79
              titleText: t('uni.button.feedback.title'),
雪洛's avatar
雪洛 已提交
80 81
              autoBackButton: true,
              backgroundColor: '#F7F7F7',
雪洛's avatar
雪洛 已提交
82 83
              titleColor: '#007aff',
              buttons: [{
fxy060608's avatar
fxy060608 已提交
84
                text: t('uni.button.feedback.send'),
雪洛's avatar
雪洛 已提交
85 86 87 88 89 90 91 92 93
                color: '#007aff',
                fontSize: '16px',
                fontWeight: 'bold',
                onclick: function (e) {
                  feedback.evalJS(
                    'mui&&mui.trigger(document.getElementById("submit"),"tap")'
                  )
                }
              }]
雪洛's avatar
雪洛 已提交
94 95 96 97
            }
          }
        )
        feedback.show('slide-in-right')
fxy060608's avatar
fxy060608 已提交
98 99 100 101
      }
    },
    _bindObjectListeners (data, value) {
      if (value) {
fxy060608's avatar
fxy060608 已提交
102 103 104
        for (const key in value) {
          const existing = data.on[key]
          const ours = value[key]
fxy060608's avatar
fxy060608 已提交
105 106 107 108 109 110 111
          data.on[key] = existing ? [].concat(existing, ours) : ours
        }
      }
      return data
    }
  },
  render (createElement) {
fxy060608's avatar
fxy060608 已提交
112
    const $listeners = Object.create(null)
fxy060608's avatar
fxy060608 已提交
113 114 115 116 117 118 119 120 121
    if (this.$listeners) {
      Object.keys(this.$listeners).forEach(e => {
        if (this.disabled && (e === 'click' || e === 'tap')) {
          return
        }
        $listeners[e] = this.$listeners[e]
      })
    }
    if (this.hoverClass && this.hoverClass !== 'none') {
雪洛's avatar
雪洛 已提交
122 123
      return createElement(
        'uni-button',
fxy060608's avatar
fxy060608 已提交
124 125 126 127
        this._bindObjectListeners({
          class: [this.hovering ? this.hoverClass : ''],
          attrs: {
            disabled: this.disabled
雪洛's avatar
雪洛 已提交
128
          },
fxy060608's avatar
fxy060608 已提交
129 130 131 132
          on: {
            touchstart: this._hoverTouchStart,
            touchend: this._hoverTouchEnd,
            touchcancel: this._hoverTouchCancel,
133 134
            mousedown: this._hoverMousedown,
            mouseup: this._hoverMouseup,
fxy060608's avatar
fxy060608 已提交
135 136 137 138
            click: this._onClick
          }
        },
        $listeners
雪洛's avatar
雪洛 已提交
139 140 141
        ),
        this.$slots.default
      )
fxy060608's avatar
fxy060608 已提交
142
    } else {
雪洛's avatar
雪洛 已提交
143 144
      return createElement(
        'uni-button',
fxy060608's avatar
fxy060608 已提交
145 146 147 148
        this._bindObjectListeners({
          class: [this.hovering ? this.hoverClass : ''],
          attrs: {
            disabled: this.disabled
雪洛's avatar
雪洛 已提交
149
          },
fxy060608's avatar
fxy060608 已提交
150 151 152 153 154
          on: {
            click: this._onClick
          }
        },
        $listeners
雪洛's avatar
雪洛 已提交
155 156 157
        ),
        this.$slots.default
      )
fxy060608's avatar
fxy060608 已提交
158 159 160 161 162 163 164 165 166
    }
  },
  listeners: {
    'label-click': '_onClick',
    '@label-click': '_onClick'
  }
}
</script>
<style>
fxy060608's avatar
fxy060608 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  uni-button {
    position: relative;
    display: block;
    margin-left: auto;
    margin-right: auto;
    padding-left: 14px;
    padding-right: 14px;
    box-sizing: border-box;
    font-size: 18px;
    text-align: center;
    text-decoration: none;
    line-height: 2.55555556;
    border-radius: 5px;
    -webkit-tap-highlight-color: transparent;
    color: #000000;
    background-color: #f8f8f8;
    cursor: pointer;
  }
雪洛's avatar
雪洛 已提交
185

fxy060608's avatar
fxy060608 已提交
186 187 188
  uni-button[hidden] {
    display: none !important;
  }
雪洛's avatar
雪洛 已提交
189

fxy060608's avatar
fxy060608 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  uni-button:after {
    content: " ";
    width: 200%;
    height: 200%;
    position: absolute;
    top: 0;
    left: 0;
    border: 1px solid rgba(0, 0, 0, 0.2);
    -webkit-transform: scale(0.5);
    transform: scale(0.5);
    -webkit-transform-origin: 0 0;
    transform-origin: 0 0;
    box-sizing: border-box;
    border-radius: 10px;
  }
雪洛's avatar
雪洛 已提交
205

fxy060608's avatar
fxy060608 已提交
206 207 208 209
  uni-button[native] {
    padding-left: 0;
    padding-right: 0;
  }
雪洛's avatar
雪洛 已提交
210

fxy060608's avatar
fxy060608 已提交
211 212 213 214 215 216
  uni-button[native] .uni-button-cover-view-wrapper {
    border: inherit;
    border-color: inherit;
    border-radius: inherit;
    background-color: inherit;
  }
雪洛's avatar
雪洛 已提交
217

fxy060608's avatar
fxy060608 已提交
218 219 220 221
  uni-button[native] .uni-button-cover-view-inner {
    padding-left: 14px;
    padding-right: 14px;
  }
雪洛's avatar
雪洛 已提交
222

fxy060608's avatar
fxy060608 已提交
223 224 225 226
  uni-button uni-cover-view {
    line-height: inherit;
    white-space: inherit;
  }
雪洛's avatar
雪洛 已提交
227

fxy060608's avatar
fxy060608 已提交
228 229 230 231
  uni-button[type="default"] {
    color: #000000;
    background-color: #f8f8f8;
  }
雪洛's avatar
雪洛 已提交
232

fxy060608's avatar
fxy060608 已提交
233 234 235 236
  uni-button[type="primary"] {
    color: #ffffff;
    background-color: #007aff;
  }
雪洛's avatar
雪洛 已提交
237

fxy060608's avatar
fxy060608 已提交
238 239 240 241
  uni-button[type="warn"] {
    color: #ffffff;
    background-color: #e64340;
  }
雪洛's avatar
雪洛 已提交
242

fxy060608's avatar
fxy060608 已提交
243 244 245 246
  uni-button[disabled] {
    color: rgba(255, 255, 255, 0.6);
    cursor: not-allowed;
  }
雪洛's avatar
雪洛 已提交
247

fxy060608's avatar
fxy060608 已提交
248 249 250 251 252
  uni-button[disabled][type="default"],
  uni-button[disabled]:not([type]) {
    color: rgba(0, 0, 0, 0.3);
    background-color: #f7f7f7;
  }
雪洛's avatar
雪洛 已提交
253

fxy060608's avatar
fxy060608 已提交
254 255 256
  uni-button[disabled][type="primary"] {
    background-color: rgba(0, 122, 255, 0.6);
  }
雪洛's avatar
雪洛 已提交
257

fxy060608's avatar
fxy060608 已提交
258 259 260
  uni-button[disabled][type="warn"] {
    background-color: #ec8b89;
  }
雪洛's avatar
雪洛 已提交
261

fxy060608's avatar
fxy060608 已提交
262 263 264 265
  uni-button[type="primary"][plain] {
    color: #007aff;
    background-color: transparent;
  }
雪洛's avatar
雪洛 已提交
266

fxy060608's avatar
fxy060608 已提交
267 268 269
  uni-button[type="primary"][plain][disabled] {
    color: rgba(0, 0, 0, 0.2);
  }
雪洛's avatar
雪洛 已提交
270

fxy060608's avatar
fxy060608 已提交
271
  uni-button[type="primary"][plain]:after {
272
    border-color:#007aff;
fxy060608's avatar
fxy060608 已提交
273
  }
雪洛's avatar
雪洛 已提交
274

fxy060608's avatar
fxy060608 已提交
275 276 277 278
  uni-button[type="default"][plain] {
    color: #353535;
    background-color: transparent;
  }
雪洛's avatar
雪洛 已提交
279

fxy060608's avatar
fxy060608 已提交
280 281 282
  uni-button[type="default"][plain][disabled] {
    color: rgba(0, 0, 0, 0.2);
  }
雪洛's avatar
雪洛 已提交
283

fxy060608's avatar
fxy060608 已提交
284
  uni-button[type="default"][plain]:after {
285
    border-color: #353535;
fxy060608's avatar
fxy060608 已提交
286
  }
雪洛's avatar
雪洛 已提交
287

fxy060608's avatar
fxy060608 已提交
288 289 290
  uni-button[plain][native] .uni-button-cover-view-inner {
    padding: 0;
  }
雪洛's avatar
雪洛 已提交
291

fxy060608's avatar
fxy060608 已提交
292 293 294 295
  uni-button[type="warn"][plain] {
    color: #e64340;
    background-color: transparent;
  }
雪洛's avatar
雪洛 已提交
296

fxy060608's avatar
fxy060608 已提交
297 298 299
  uni-button[type="warn"][plain][disabled] {
    color: rgba(0, 0, 0, 0.2);
  }
雪洛's avatar
雪洛 已提交
300

fxy060608's avatar
fxy060608 已提交
301
  uni-button[type="warn"][plain]:after {
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    border-color: #e64340;
  }

  uni-button[plain] {
    color: #353535;
    background-color: transparent;
  }

  uni-button[plain][disabled] {
    color: rgba(0, 0, 0, 0.2);
  }

  uni-button[plain]:after {
    border-color: #353535;
  }

  uni-button[plain][disabled]:after {
    border-color: rgba(0, 0, 0, 0.2);
fxy060608's avatar
fxy060608 已提交
320
  }
雪洛's avatar
雪洛 已提交
321

fxy060608's avatar
fxy060608 已提交
322 323 324 325 326 327
  uni-button[size="mini"] {
    display: inline-block;
    line-height: 2.3;
    font-size: 13px;
    padding: 0 1.34em;
  }
雪洛's avatar
雪洛 已提交
328

fxy060608's avatar
fxy060608 已提交
329 330 331
  uni-button[size="mini"][native] {
    padding: 0;
  }
雪洛's avatar
雪洛 已提交
332

fxy060608's avatar
fxy060608 已提交
333 334 335
  uni-button[size="mini"][native] .uni-button-cover-view-inner {
    padding: 0 1.34em;
  }
雪洛's avatar
雪洛 已提交
336

fxy060608's avatar
fxy060608 已提交
337 338 339
  uni-button[loading]:not([disabled]) {
    cursor: progress;
  }
雪洛's avatar
雪洛 已提交
340

fxy060608's avatar
fxy060608 已提交
341 342 343 344 345 346 347 348 349 350
  uni-button[loading]:before {
    content: " ";
    display: inline-block;
    width: 18px;
    height: 18px;
    vertical-align: middle;
    -webkit-animation: uni-loading 1s steps(12, end) infinite;
    animation: uni-loading 1s steps(12, end) infinite;
    background-size: 100%;
  }
雪洛's avatar
雪洛 已提交
351

fxy060608's avatar
fxy060608 已提交
352 353 354 355
  uni-button[loading][type="primary"] {
    color: rgba(255, 255, 255, 0.6);
    background-color: #0062cc;
  }
雪洛's avatar
雪洛 已提交
356

fxy060608's avatar
fxy060608 已提交
357 358 359 360
  uni-button[loading][type="primary"][plain] {
    color: #007aff;
    background-color: transparent;
  }
雪洛's avatar
雪洛 已提交
361

fxy060608's avatar
fxy060608 已提交
362 363 364 365
  uni-button[loading][type="default"] {
    color: rgba(0, 0, 0, 0.6);
    background-color: #dedede;
  }
雪洛's avatar
雪洛 已提交
366

fxy060608's avatar
fxy060608 已提交
367 368 369 370
  uni-button[loading][type="default"][plain] {
    color: #353535;
    background-color: transparent;
  }
雪洛's avatar
雪洛 已提交
371

fxy060608's avatar
fxy060608 已提交
372 373 374 375
  uni-button[loading][type="warn"] {
    color: rgba(255, 255, 255, 0.6);
    background-color: #ce3c39;
  }
雪洛's avatar
雪洛 已提交
376

fxy060608's avatar
fxy060608 已提交
377 378 379 380
  uni-button[loading][type="warn"][plain] {
    color: #e64340;
    background-color: transparent;
  }
雪洛's avatar
雪洛 已提交
381

fxy060608's avatar
fxy060608 已提交
382 383 384
  uni-button[loading][native]:before {
    content: none;
  }
雪洛's avatar
雪洛 已提交
385

fxy060608's avatar
fxy060608 已提交
386 387 388 389
  .button-hover {
    color: rgba(0, 0, 0, 0.6);
    background-color: #dedede;
  }
雪洛's avatar
雪洛 已提交
390

fxy060608's avatar
fxy060608 已提交
391 392 393 394
  .button-hover[plain] {
    color: rgba(53, 53, 53, 0.6);
    background-color: transparent;
  }
雪洛's avatar
雪洛 已提交
395

396 397 398 399
  .button-hover[plain]:after {
    border-color: rgba(53, 53, 53, 0.6);
  }

fxy060608's avatar
fxy060608 已提交
400 401 402 403
  .button-hover[type="primary"] {
    color: rgba(255, 255, 255, 0.6);
    background-color: #0062cc;
  }
雪洛's avatar
雪洛 已提交
404

fxy060608's avatar
fxy060608 已提交
405
  .button-hover[type="primary"][plain] {
D
DCloud_LXH 已提交
406
    color: rgba(0, 122, 255, 0.6);
fxy060608's avatar
fxy060608 已提交
407 408
    background-color: transparent;
  }
雪洛's avatar
雪洛 已提交
409

410 411 412 413
  .button-hover[type="primary"][plain]:after {
    border-color: rgba(0, 122, 255, 0.6);
  }

fxy060608's avatar
fxy060608 已提交
414 415 416 417
  .button-hover[type="default"] {
    color: rgba(0, 0, 0, 0.6);
    background-color: #dedede;
  }
雪洛's avatar
雪洛 已提交
418

fxy060608's avatar
fxy060608 已提交
419 420 421 422
  .button-hover[type="default"][plain] {
    color: rgba(53, 53, 53, 0.6);
    background-color: transparent;
  }
雪洛's avatar
雪洛 已提交
423

424 425 426 427
  .button-hover[type="default"][plain]:after {
    border-color: rgba(53, 53, 53, 0.6);
  }

fxy060608's avatar
fxy060608 已提交
428 429 430 431
  .button-hover[type="warn"] {
    color: rgba(255, 255, 255, 0.6);
    background-color: #ce3c39;
  }
雪洛's avatar
雪洛 已提交
432

fxy060608's avatar
fxy060608 已提交
433 434 435 436
  .button-hover[type="warn"][plain] {
    color: rgba(230, 67, 64, 0.6);
    background-color: transparent;
  }
D
DCloud_LXH 已提交
437

438 439 440 441
  .button-hover[type="warn"][plain]:after {
    border-color: rgba(230, 67, 64, 0.6);
  }

D
DCloud_LXH 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
  @media (prefers-color-scheme: dark) {
    uni-button,
    uni-button[type='default'] {
      color: #d6d6d6;
      background-color: #343434;
    }

    .button-hover,
    .button-hover[type='default'] {
      color: #d6d6d6;
      background-color: rgba(255, 255, 255, 0.1);
    }

    uni-button[disabled][type='default'],
    uni-button[disabled]:not([type]) {
      color: rgba(255, 255, 255, 0.2);
      background-color: rgba(255, 255, 255, 0.08);
    }

    uni-button[type='primary'][plain][disabled] {
      color: rgba(255, 255, 255, 0.2);
      border-color: rgba(255, 255, 255, 0.2);
    }

    uni-button[type='default'][plain] {
      color: #d6d6d6;
468 469 470 471
    }

    uni-button[type='default'][plain]:after {
      border-color: #d6d6d6;
D
DCloud_LXH 已提交
472 473 474 475 476 477 478 479
    }

    .button-hover[type='default'][plain] {
      color: rgba(150, 150, 150, 0.6);
      border-color: rgba(150, 150, 150, 0.6);
      background-color: rgba(50, 50, 50, 0.2);
    }

480 481 482 483
    .button-hover[type='default'][plain]:after {
      border-color: rgba(150, 150, 150, 0.6);
    }

D
DCloud_LXH 已提交
484 485 486
    uni-button[type='default'][plain][disabled] {
      color: hsla(0, 0%, 100%, 0.2);
    }
487 488 489 490

    uni-button[type='default'][plain][disabled]:after {
      border-color: hsla(0, 0%, 100%, 0.2);
    }
D
DCloud_LXH 已提交
491
  }
492
</style>