1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui;
6:
7: use Atk4\Ui\Js\JsExpressionable;
8:
9: /**
10: * $bar = ProgressBar::addTo($app, [10, 'label' => 'Processing files']);.
11: */
12: class ProgressBar extends View
13: {
14: /** @var string|false|null Contains a text label to display under the bar. Null/false will disable the label. */
15: public $label;
16:
17: public $ui = 'progress';
18:
19: public $defaultTemplate = 'progress.html';
20:
21: /**
22: * Value that appears on a progress bar. Set it through constructor, e.g.
23: * ProgressBar::addTo($app, [20]);.
24: *
25: * @var int
26: */
27: public $value = 0;
28:
29: /** @var int Indicates a maximum value of a progress bar. */
30: public $max = 100;
31:
32: /**
33: * @param array<0|string, mixed>|string $label
34: */
35: public function __construct(int $value = 0, $label = [])
36: {
37: $this->value = $value;
38:
39: parent::__construct($label);
40: }
41:
42: #[\Override]
43: protected function renderView(): void
44: {
45: $this->js(true)->progress(['percent' => $this->value]);
46:
47: parent::renderView();
48: }
49:
50: /**
51: * Return JS action for incrementing progress by one.
52: */
53: public function jsIncrement(): JsExpressionable
54: {
55: return $this->js()->progress('increment');
56: }
57:
58: /**
59: * Return JS action for setting value.
60: */
61: public function jsValue(int $value): JsExpressionable
62: {
63: return $this->js()->progress(['percent' => $value]);
64: }
65: }
66: