Skip to content

Commit

Permalink
Feat: Add methods to change icon
Browse files Browse the repository at this point in the history
  • Loading branch information
vkazakevich committed Jul 8, 2021
1 parent 3e12328 commit 0003406
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 12 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,45 @@ use Elbytes\NovaTooltipField\Tooltip;

....

Tooltip::make('Field')
Tooltip::make('Type', 'type'),

// OR

Tooltip::make('Type', 'type')
->setDefaultIcon('<svg>...</svg>') // Optional
->setDependIcons([
'fieldValue' => '<svg>...</svg>',
'fieldValue2' => '<svg>...</svg>',
]), // Optional
```

### Using with your own icon

To change the default icon, you need to use the setDefaultIcon method.

```php
use Elbytes\NovaTooltipField\Tooltip;

....

Tooltip::make('Type', 'type')
->setDefaultIcon('<svg>...</svg>')
```

### Value-dependent icons

Sometimes the icon has to change depending on the value of the field, you can use the setDependIcons method for this.

```php
use Elbytes\NovaTooltipField\Tooltip;

....

Tooltip::make('Type', 'type')
->setDependIcons([
'fieldValue' => '<svg>...</svg>',
'fieldValue2' => '<svg>...</svg>',
]),
```

## Contribution
Expand Down
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

46 changes: 36 additions & 10 deletions resources/js/components/IndexField.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
<template>
<span class="text-70 hover:text-primary">
<icon
type="info"
width="18"
height="18"
view-box="0 0 20 20"
v-tooltip="field.value ? field.value : '—'"
/>
</span>
<div class="text-70 hover:text-primary flex justify-center mb-1 cursor-pointer">
<span
v-if="icon"
class="h-6"
v-tooltip="tooltipValue"
v-html="icon"
/>
<icon
v-else
type="info"
width="18"
height="18"
view-box="0 0 20 20"
v-tooltip="tooltipValue"
/>
</div>
</template>

<script>
export default {
props: ['resourceName', 'field'],
props: ['resourceName', 'field'],
computed: {
icon () {
if (typeof this.field.dependIcons === 'object' && this.field.dependIcons) {
if (this.field.dependIcons.hasOwnProperty(this.field.value)) {
return this.field.dependIcons[this.field.value]
}
}
if (this.field.icon) {
return this.field.icon
}
return null
},
tooltipValue () {
return this.field.value ? this.field.value : ''
}
}
}
</script>
26 changes: 26 additions & 0 deletions src/Tooltip.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,30 @@ class Tooltip extends Field
* @var string
*/
public $component = 'nova-tooltip-field';

/**
* Depending on the value, the icon changes.
*
* @param array $icons ['expected value' => '<svg>...</svg>']
* @return mixed
*/
public function setDependIcons(array $icons)
{
return $this->withMeta([
'dependIcons' => $icons
]);
}

/**
* Replace the default icon.
*
* @param string $inlineSvg
* @return mixed
*/
public function setDefaultIcon(string $inlineSvg)
{
return $this->withMeta([
'icon' => $inlineSvg
]);
}
}

0 comments on commit 0003406

Please sign in to comment.