Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
some clean up, add confirmation dialogue #7
Browse files Browse the repository at this point in the history
  • Loading branch information
trin4ik committed Nov 1, 2023
1 parent 01eff2d commit 06b7dc8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,39 @@ composer require trin4ik/nova-switcher
## Usage
```php
use Trin4ik\NovaSwitcher\NovaSwitcher;
...
NovaSwitcher::make('Active');
```
### Labels
```php
use Trin4ik\NovaSwitcher\NovaSwitcher;
...
NovaSwitcher::make('Active')
->trueLabel('On')
->falseLabel('Off');
...
NovaSwitcher::make('Active')
->withLabels(true: 'On', false: 'Off');
```
or
### Confirmation
```php
use Trin4ik\NovaSwitcher\NovaSwitcher;
...
NovaSwitcher::make('Active')
->withLabels(true: 'On', false: 'Off');
->confirmToTrue('enable?')
->confirmToFalse('disable?');
...
NovaSwitcher::make('Active')
->confirm(toTrue: 'enable?', toFalse: 'disable?');
```
of couse, you can only use confirmation dialogue to enable, or disable:
```php
use Trin4ik\NovaSwitcher\NovaSwitcher;
...
NovaSwitcher::make('Active')
->confirmToFalse('Turn it off?');
```
### Reverse
Also, you can use reverse value (if switcher checked, value is false and vice versa)
```php
NovaSwitcher::make('Active')
Expand Down
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

23 changes: 16 additions & 7 deletions resources/js/components/IndexField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<input
class="input"
type="checkbox"
:checked="value"
v-model="value"
@change="toggle"
/>
<span class="switch"></span>
Expand All @@ -28,17 +28,19 @@ export default {
methods: {
toggle() {
this.value = !this.value
this.$emit('change', this.reverse ? !this.value : this.value)
if (this.confirm[!this.value] && !confirm(this.confirm[!this.value])) {
this.$nextTick(() => {
this.value = !this.value
})
return;
}
if (this.withoutRequest) return
Nova.request().post('/nova-vendor/nova-switcher/toggle/' + this.resourceName, {
value: this.reverse ? !this.value : this.value,
fieldName: this.field.attribute,
resourceId: this.id,
}).then((res) => {
if (res.data.success) {
this.field.value = this.reverse ? !this.value : this.value;
} else {
if (!res.data.success) {
setTimeout(() => {
this.value = !this.value
}, 250)
Expand All @@ -62,7 +64,14 @@ export default {
reverse() {
return this.field.reverse ?? false
},
},
confirm() {
return {
'true': this.field.confirm_to_false ?? false,
'false': this.field.confirm_to_true ?? false,
}
}
}
}
</script>
<style>
Expand Down
23 changes: 23 additions & 0 deletions src/NovaSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class NovaSwitcher extends Boolean

public $textAlign = 'center';

public $defaultConfirm = 'Are you sure?';

public function trueLabel (string $label): self
{
$this->textAlign = 'left';
Expand Down Expand Up @@ -45,4 +47,25 @@ public function reverse (): self
'reverse' => true,
]);
}

public function confirmToTrue (string $confirm): self
{
return $this->withMeta([
'confirm_to_true' => $confirm,
]);
}

public function confirmToFalse (string $confirm): self
{
return $this->withMeta([
'confirm_to_false' => $confirm,
]);
}

public function confirm (null|string $toTrue = null, null|string $toFalse = null): self
{
$this->confirmToTrue($toTrue ?? $this->defaultConfirm);
$this->confirmToFalse($toFalse ?? $toTrue ?? $this->defaultConfirm);
return $this;
}
}

0 comments on commit 06b7dc8

Please sign in to comment.