Skip to content

Commit f7e5c0d

Browse files
feat: New stats field class
docs: Fix inline docs
1 parent 3ca4c71 commit f7e5c0d

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/Cms/Core.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Kirby\Form\Field\BlocksField;
1313
use Kirby\Form\Field\EntriesField;
1414
use Kirby\Form\Field\LayoutField;
15+
use Kirby\Form\Field\StatsField;
1516
use Kirby\Panel\Ui\FilePreviews\AudioFilePreview;
1617
use Kirby\Panel\Ui\FilePreviews\ImageFilePreview;
1718
use Kirby\Panel\Ui\FilePreviews\PdfFilePreview;
@@ -270,6 +271,7 @@ public function fields(): array
270271
'range' => $this->root . '/fields/range.php',
271272
'select' => $this->root . '/fields/select.php',
272273
'slug' => $this->root . '/fields/slug.php',
274+
'stats' => StatsField::class,
273275
'structure' => $this->root . '/fields/structure.php',
274276
'tags' => $this->root . '/fields/tags.php',
275277
'tel' => $this->root . '/fields/tel.php',

src/Form/Field/StatsField.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Kirby\Form\Field;
4+
5+
use Kirby\Form\FieldClass;
6+
use Kirby\Panel\Ui\Stats;
7+
8+
/**
9+
* Stats field
10+
*
11+
* @package Kirby Field
12+
* @author Bastian Allgeier <[email protected]>
13+
* @link https://getkirby.com
14+
* @copyright Bastian Allgeier
15+
* @license https://getkirby.com/license
16+
* @since 5.1.0
17+
*/
18+
class StatsField extends FieldClass
19+
{
20+
/**
21+
* Array or query string for reports. Each report needs a `label` and `value` and can have additional `info`, `link`, `icon` and `theme` settings.
22+
*/
23+
protected array|string $reports;
24+
25+
/**
26+
* The size of the report cards. Available sizes: `tiny`, `small`, `medium`, `large`
27+
*/
28+
protected string $size;
29+
30+
/**
31+
* Cache for the Stats UI component
32+
*/
33+
protected Stats $stats;
34+
35+
public function __construct(array $params)
36+
{
37+
parent::__construct($params);
38+
39+
$this->reports = $params['reports'] ?? [];
40+
$this->size = $params['size'] ?? 'large';
41+
}
42+
43+
public function hasValue(): bool
44+
{
45+
return false;
46+
}
47+
48+
public function reports(): array
49+
{
50+
return $this->stats()->reports();
51+
}
52+
53+
public function size(): string
54+
{
55+
return $this->stats()->size();
56+
}
57+
58+
public function stats(): Stats
59+
{
60+
return $this->stats ??= Stats::from(
61+
model: $this->model,
62+
reports: $this->reports,
63+
size: $this->size
64+
);
65+
}
66+
67+
public function props(): array
68+
{
69+
return [
70+
...parent::props(),
71+
...$this->stats()->props()
72+
];
73+
}
74+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Kirby\Form\Field;
4+
5+
use Kirby\Panel\Ui\Stats;
6+
7+
class StatsFieldTest extends TestCase
8+
{
9+
public function testDefaultProps(): void
10+
{
11+
$field = $this->field('stats');
12+
13+
$this->assertSame('stats', $field->type());
14+
$this->assertSame('stats', $field->name());
15+
$this->assertSame('Stats', $field->label());
16+
$this->assertSame([], $field->reports());
17+
$this->assertSame('large', $field->size());
18+
$this->assertFalse($field->hasValue());
19+
$this->assertInstanceOf(Stats::class, $field->stats());
20+
21+
$props = $field->props();
22+
23+
$this->assertSame('Stats', $props['label']);
24+
$this->assertSame('stats', $props['name']);
25+
$this->assertSame('stats', $props['type']);
26+
$this->assertSame([], $props['reports']);
27+
$this->assertSame('large', $props['size']);
28+
}
29+
}

0 commit comments

Comments
 (0)