v1.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace zin;
  3. class checkbox extends wg
  4. {
  5. /**
  6. * @var mixed[]
  7. */
  8. protected static $defineProps = array(
  9. 'text?: string',
  10. 'checked?: bool',
  11. 'name?: string',
  12. 'primary: bool=true',
  13. 'id?: string',
  14. 'disabled?: bool',
  15. 'type: string="checkbox"',
  16. 'value?: string',
  17. 'typeClass?: string',
  18. 'rootClass?: string',
  19. 'labelClass?: string',
  20. 'title?: string'
  21. );
  22. public function onAddChild($child)
  23. {
  24. if(is_string($child) && !$this->props->has('text'))
  25. {
  26. $this->props->set('text', $child);
  27. return false;
  28. }
  29. }
  30. protected function buildPrimary()
  31. {
  32. list($id, $text, $name, $checked, $disabled, $type, $typeClass, $rootClass, $labelClass, $value, $title) = $this->prop(array('id', 'text', 'name', 'checked', 'disabled', 'type', 'typeClass', 'rootClass', 'labelClass', 'value', 'title'));
  33. if(empty($typeClass)) $typeClass = $type;
  34. if(empty($id)) $id = empty($name) ? $this->gid : ($name . '_' . $value);
  35. return div
  36. (
  37. setClass("$typeClass-primary", $rootClass, array('disabled' => $disabled)),
  38. h::input
  39. (
  40. set::type($type),
  41. set::id($id),
  42. set::name($name),
  43. set::checked($checked),
  44. set($this->props->skip('text,primary,typeClass,rootClass,id,labelClass'))
  45. ),
  46. h::label
  47. (
  48. set('for', $id),
  49. setClass($labelClass),
  50. !empty($title) ? set('title', $title) : null,
  51. html($text)
  52. ),
  53. $this->children()
  54. );
  55. }
  56. protected function build()
  57. {
  58. if($this->prop('primary')) return $this->buildPrimary();
  59. list($text, $type, $typeClass) = $this->prop(array('text', 'type', 'typeClass'));
  60. return h::label
  61. (
  62. setClass(empty($typeClass) ? $type : $typeClass),
  63. h::input
  64. (
  65. set::type($type),
  66. set($this->props->skip('text,primary,typeClass'))
  67. ),
  68. is_string($text) ? span($text, set::className('text')) : $text,
  69. $this->children()
  70. );
  71. }
  72. /**
  73. * @param string|mixed[] $checkedOrProps
  74. * @param mixed ...$children
  75. * @return $this
  76. * @param mixed[]|null $props
  77. */
  78. public static function create($checkedOrProps, $props = null, ...$children)
  79. {
  80. $props = $props ? $props : array();
  81. if(is_array($checkedOrProps))
  82. {
  83. $props = array_merge($checkedOrProps, $props);
  84. }
  85. else
  86. {
  87. $props['checked'] = $checkedOrProps;
  88. }
  89. return new static(set($props), ...$children);
  90. }
  91. }