Number.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. class Number extends Text
  4. {
  5. protected $view = 'admin::form.number';
  6. protected $options = [
  7. 'upClass' => 'primary shadow-0',
  8. 'downClass' => 'light shadow-0',
  9. 'center' => true,
  10. 'disabled' => false,
  11. ];
  12. /**
  13. * Set min value of number field.
  14. *
  15. * @param int $value
  16. * @return $this
  17. */
  18. public function min($value)
  19. {
  20. $this->attribute('min', $value);
  21. return $this;
  22. }
  23. /**
  24. * Set max value of number field.
  25. *
  26. * @param int $value
  27. * @return $this
  28. */
  29. public function max($value)
  30. {
  31. $this->attribute('max', $value);
  32. return $this;
  33. }
  34. /**
  35. * Set increment and decrement button to disabled.
  36. *
  37. * @param bool $value
  38. * @return $this
  39. */
  40. public function disable(bool $value = true)
  41. {
  42. parent::disable($value);
  43. $this->options['disabled'] = $value;
  44. return $this;
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. protected function prepareInputValue($value)
  50. {
  51. return empty($value) ? 0 : $value;
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function value($value = null)
  57. {
  58. if (is_null($value)) {
  59. return (int) parent::value();
  60. }
  61. return parent::value($value);
  62. }
  63. public function render()
  64. {
  65. $this->defaultAttribute('style', 'width: 140px;flex:none');
  66. $this->prepend('');
  67. return parent::render();
  68. }
  69. }