Skip to content

Commit

Permalink
Remove static props
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Feb 3, 2025
1 parent f38635d commit 4f28c2a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 21 deletions.
35 changes: 35 additions & 0 deletions packages/upgrade/src/Rector/SimplePropertyChangesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Filament\Pages\SimplePage;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Resources\Resource;
use Filament\Widgets\ChartWidget;
use Filament\Widgets\StatsOverviewWidget;
use Filament\Widgets\Widget;
use PhpParser\Modifiers;
use PhpParser\Node;
Expand Down Expand Up @@ -92,6 +94,39 @@ public function getChanges(): array
},
],
],
[
'class' => [
ChartWidget::class,
StatsOverviewWidget::class,
],
'changes' => [
'pollingInterval' => function (Property $node) {
$node->flags &= ~Modifiers::STATIC;
},
],
],
[
'class' => [
ChartWidget::class,
],
'changes' => [
'color' => function (Property $node) {
$node->flags &= ~Modifiers::STATIC;
},
'heading' => function (Property $node) {
$node->flags &= ~Modifiers::STATIC;
},
'description' => function (Property $node) {
$node->flags &= ~Modifiers::STATIC;
},
'maxHeight' => function (Property $node) {
$node->flags &= ~Modifiers::STATIC;
},
'options' => function (Property $node) {
$node->flags &= ~Modifiers::STATIC;
},
],
],
];
}

Expand Down
4 changes: 2 additions & 2 deletions packages/widgets/docs/01-stats-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ By default, stats overview widgets refresh their data every 5 seconds.
To customize this, you may override the `$pollingInterval` property on the class to a new interval:

```php
protected static ?string $pollingInterval = '10s';
protected ?string $pollingInterval = '10s';
```

Alternatively, you may disable polling altogether:

```php
protected static ?string $pollingInterval = null;
protected ?string $pollingInterval = null;
```

## Disabling lazy loading
Expand Down
14 changes: 7 additions & 7 deletions packages/widgets/docs/02-charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ php artisan make:filament-widget BlogPostsChart --chart

There is a single `ChartWidget` class that is used for all charts. The type of chart is set by the `getType()` method. In this example, that method returns the string `'line'`.

The `protected static ?string $heading` variable is used to set the heading that describes the chart. If you need to set the heading dynamically, you can override the `getHeading()` method.
The `protected ?string $heading` variable is used to set the heading that describes the chart. If you need to set the heading dynamically, you can override the `getHeading()` method.

The `getData()` method is used to return an array of datasets and labels. Each dataset is a labeled array of points to plot on the chart, and each label is a string. This structure is identical to the [Chart.js](https://www.chartjs.org/docs) library, which Filament uses to render charts. You may use the [Chart.js documentation](https://www.chartjs.org/docs) to fully understand the possibilities to return from `getData()`, based on the chart type.

Expand All @@ -27,7 +27,7 @@ use Filament\Widgets\ChartWidget;

class BlogPostsChart extends ChartWidget
{
protected static ?string $heading = 'Blog Posts';
protected ?string $heading = 'Blog Posts';

protected function getData(): array
{
Expand Down Expand Up @@ -69,7 +69,7 @@ Below is a list of available chart widget classes which you may extend, and thei
You can customize the [color](../styling/colors) of the chart data by setting the `$color` property:

```php
protected static string $color = 'info';
protected string $color = 'info';
```

If you're looking to customize the color further, or use multiple colors across multiple datasets, you can still make use of Chart.js's [color options](https://www.chartjs.org/docs/latest/general/colors.html) in the data:
Expand Down Expand Up @@ -165,29 +165,29 @@ By default, chart widgets refresh their data every 5 seconds.
To customize this, you may override the `$pollingInterval` property on the class to a new interval:

```php
protected static ?string $pollingInterval = '10s';
protected ?string $pollingInterval = '10s';
```

Alternatively, you may disable polling altogether:

```php
protected static ?string $pollingInterval = null;
protected ?string $pollingInterval = null;
```

## Setting a maximum chart height

You may place a maximum height on the chart to ensure that it doesn't get too big, using the `$maxHeight` property:

```php
protected static ?string $maxHeight = '300px';
protected ?string $maxHeight = '300px';
```

## Setting chart configuration options

You may specify an `$options` variable on the chart class to control the many configuration options that the Chart.js library provides. For instance, you could turn off the [legend](https://www.chartjs.org/docs/latest/configuration/legend.html) for a line chart:

```php
protected static ?array $options = [
protected ?array $options = [
'plugins' => [
'legend' => [
'display' => false,
Expand Down
20 changes: 10 additions & 10 deletions packages/widgets/src/ChartWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ abstract class ChartWidget extends Widget implements HasSchemas

public ?string $filter = null;

protected static string $color = 'primary';
protected string $color = 'primary';

protected static ?string $heading = null;
protected ?string $heading = null;

protected static ?string $description = null;
protected ?string $description = null;

protected static ?string $maxHeight = null;
protected ?string $maxHeight = null;

/**
* @var array<string, mixed> | null
*/
protected static ?array $options = null;
protected ?array $options = null;

/**
* @var view-string
Expand Down Expand Up @@ -83,25 +83,25 @@ protected function getFilters(): ?array

public function getHeading(): string | Htmlable | null
{
return static::$heading;
return $this->heading;
}

public function getDescription(): string | Htmlable | null
{
return static::$description;
return $this->description;
}

protected function getMaxHeight(): ?string
{
return static::$maxHeight;
return $this->maxHeight;
}

/**
* @return array<string, mixed> | RawJs | null
*/
protected function getOptions(): array | RawJs | null
{
return static::$options;
return $this->options;
}

public function updateChartData(): void
Expand All @@ -122,6 +122,6 @@ public function rendering(): void

public function getColor(): string
{
return static::$color;
return $this->color;
}
}
4 changes: 2 additions & 2 deletions packages/widgets/src/Concerns/CanPoll.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

trait CanPoll
{
protected static ?string $pollingInterval = '5s';
protected ?string $pollingInterval = '5s';

protected function getPollingInterval(): ?string
{
return static::$pollingInterval;
return $this->pollingInterval;
}
}

0 comments on commit 4f28c2a

Please sign in to comment.