Skip to content

Commit 3e427b0

Browse files
authored
Merge pull request #12 from RhubarbPHP/feature/AddButtonType
Adding the ability to set button type
2 parents 9e1ef65 + 2c5affe commit 3e427b0

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Changelog
22

3+
### 1.0.22
4+
5+
* Added: The HTML type of a Button can be changed from default "submit"
6+
37
### 1.0.21
48

5-
* Fixed: AJAX checkbox value parsing
9+
* Fixed: AJAX checkbox value parsing
610

711
### 1.0.20
812

src/Buttons/Button.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct($name, $text = "", callable $pressedCallback = null,
3333
$this->model->useXhr = $useXhr;
3434
$this->buttonPressedEvent = new Event();
3535

36-
if ($pressedCallback){
36+
if ($pressedCallback) {
3737
$this->buttonPressedEvent->attachHandler($pressedCallback);
3838
}
3939
}
@@ -57,12 +57,12 @@ public function setUseXhr($useXhr)
5757
{
5858
$this->model->useXhr = $useXhr;
5959
}
60-
60+
6161
protected function createModel()
6262
{
6363
$model = new ButtonModel();
64-
$model->buttonPressedEvent->attachHandler(function(...$arguments){
65-
if ($this->model->useXhr){
64+
$model->buttonPressedEvent->attachHandler(function (...$arguments) {
65+
if ($this->model->useXhr) {
6666
return $this->buttonPressedEvent->raise(...$arguments);
6767
} else {
6868
$this->runBeforeRender(function () use ($arguments) {
@@ -73,4 +73,19 @@ protected function createModel()
7373

7474
return $model;
7575
}
76+
77+
/**
78+
* Allows changing the HTML input type from default "submit"
79+
*
80+
* @param string $type e.g. "submit", "button", "reset"
81+
* @param bool $submitFormOnClick If true, javascript will
82+
*/
83+
public function setButtonType($type, $submitFormOnClick = false)
84+
{
85+
$this->model->type = $type;
86+
87+
if ($submitFormOnClick) {
88+
$this->model->addCssClassNames("submit-on-click");
89+
}
90+
}
7691
}

src/Buttons/ButtonModel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class ButtonModel extends ControlModel
2424
*/
2525
public $buttonPressedEvent;
2626

27+
/**
28+
* @var string
29+
*/
30+
public $type = 'submit';
31+
2732
public function __construct()
2833
{
2934
$this->buttonPressedEvent = new Event();

src/Buttons/ButtonView.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function printViewContent()
2323
$xhrAttribute = ($this->model->useXhr) ? ' xmlrpc="yes"' : '';
2424

2525
?>
26-
<input leaf-name="<?=$this->model->leafName;?>" type="submit" name="<?=$this->model->leafPath;?>" id="<?=$this->model->leafPath;?>" value="<?=htmlentities($this->model->text);?>"<?=$classes.$otherAttributes.$confirmAttribute.$xhrAttribute;?> />
26+
<input leaf-name="<?=$this->model->leafName;?>" type="<?=$this->model->type?>" name="<?=$this->model->leafPath;?>" id="<?=$this->model->leafPath;?>" value="<?=htmlentities($this->model->text);?>"<?=$classes.$otherAttributes.$confirmAttribute.$xhrAttribute;?> />
2727
<?php
2828
}
2929

0 commit comments

Comments
 (0)