Skip to content

Commit

Permalink
Merge pull request #16 from Praznik/add-clear-button
Browse files Browse the repository at this point in the history
Added clear button
  • Loading branch information
nathancox authored Feb 19, 2020
2 parents 72e864e + d47b1ac commit 4fce857
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
1 change: 1 addition & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ name: hasoneautocompletefield
---

NathanCox\HasOneAutocompleteField\Forms\HasOneAutocompleteField:
clearButtonEnabled: false
autocompleteDelay: 300
3 changes: 2 additions & 1 deletion client/dist/css/hasoneautocompletefield.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
display: inline-block;
}
.hasoneautocomplete.showsearch .hasoneautocomplete-currenttext,
.hasoneautocomplete.showsearch .hasoneautocomplete-editbutton {
.hasoneautocomplete.showsearch .hasoneautocomplete-editbutton,
.hasoneautocomplete.showsearch .hasoneautocomplete-clearbutton {
display: none;
}

Expand Down
32 changes: 32 additions & 0 deletions client/dist/js/hasoneautocompletefield.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,38 @@
}
});

$(".hasoneautocomplete-clearbutton").entwine({
onclick: function() {
var defaultText = String(this.closest('.hasoneautocomplete').data('default-text')).trim();
var $currentText = this.closest('.hasoneautocomplete').find('.hasoneautocomplete-currenttext').first();
var $idField = this.closest('.hasoneautocomplete').find('.hasoneautocomplete-id').first();
if ($currentText.length && $idField.length) {
if (defaultText === '') {
defaultText = '(none)';
}

$currentText.html(defaultText);
$idField.val(0).change();
}
},
onmatch: function() {
$idField = this.closest('.hasoneautocomplete').find('.hasoneautocomplete-id').first();
if ($idField.length) {
$idField.change(function(event) {
var $clearButton = $(this).closest('.hasoneautocomplete').find('.hasoneautocomplete-clearbutton').first();
if ($clearButton.length) {
var newValue = String($(this).val()).trim();
if (newValue === '' || newValue === '0') {
$clearButton.hide();
} else {
$clearButton.show();
}
}
});
}
}
});

$(".hasoneautocomplete-search").entwine({
onmatch: function (event) {
var t = this;
Expand Down
39 changes: 38 additions & 1 deletion src/forms/HasOneAutocompleteField.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class HasOneAutocompleteField extends FormField
*/
protected $searchFields = false;

protected $clearButtonEnabled = false;

/**
* Variable that sets the autocomplete delay
*
Expand All @@ -79,12 +81,13 @@ public function __construct($name, $title = null, $sourceObject, $labelField = '
{
$this->sourceObject = $sourceObject;
$this->labelField = $labelField;
$this->clearButtonEnabled = Config::inst()->get(HasOneAutocompleteField::class, 'clearButtonEnabled');

$configAutocompleteDelay = intval(Config::inst()->get(HasOneAutocompleteField::class, 'autocompleteDelay'));
if ($configAutocompleteDelay > 0) {
$this->autocompleteDelay = $configAutocompleteDelay;
}

parent::__construct($name, $title);
}

Expand Down Expand Up @@ -273,6 +276,29 @@ public function setResultsLimit($limit)
return $this;
}

public function enableClearButton()
{
$this->setClearButtonEnabled(true);
return $this;
}

public function disableClearButton()
{
$this->setClearButtonEnabled(false);
return $this;
}

private function getClearButtonEnabled()
{
return $this->clearButtonEnabled;
}

private function setClearButtonEnabled(bool $enabled = true)
{
$this->clearButtonEnabled = $enabled;
return $this;
}

public function getAutocompleteDelay()
{
return $this->autocompleteDelay;
Expand Down Expand Up @@ -317,6 +343,17 @@ public function Field($properties = array())
$cancelField->setButtonContent('Cancel');
$cancelField->addExtraClass('edit hasoneautocomplete-cancelbutton btn-outline-secondary');

if ($this->getClearButtonEnabled() === true) {
$fields->push($clearField = FormAction::create($this->name.'Clear', ''));
$clearField->setUseButtonTag(true);
$clearField->setButtonContent('Clear');
$clearField->addExtraClass('clear hasoneautocomplete-clearbutton btn-outline-danger btn-hide-outline action--delete btn-sm');

if (intval($this->value) === 0) {
$clearField->setAttribute('style', 'display:none;');
}
}

return $fields;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="$HolderID" class="form-group field<% if $extraClass %> $extraClass<% end_if %>" data-autocomplete-delay="$getAutocompleteDelay">
<div id="$HolderID" class="form-group field<% if $extraClass %> $extraClass<% end_if %>" data-default-text="$defaultText" data-autocomplete-delay="$getAutocompleteDelay">
<% if $Title %>
<label for="$ID" id="title-$ID" class="form__field-label">$Title</label>
<% end_if %>
Expand Down

0 comments on commit 4fce857

Please sign in to comment.