72 lines
2.0 KiB
PHP
72 lines
2.0 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
|
|
|
use PHPNative\Framework\Application;
|
|
use PHPNative\Ui\Widget\Button;
|
|
use PHPNative\Ui\Widget\Container;
|
|
use PHPNative\Ui\Widget\Label;
|
|
|
|
// Check PHP version
|
|
if (PHP_VERSION_ID < 80100) {
|
|
die("This demo requires PHP 8.1+ for Fiber support.\nYour version: " . PHP_VERSION . "\n");
|
|
}
|
|
|
|
$app = new Application('Button Example', 800, 600);
|
|
|
|
$container = new Container('p-4');
|
|
// Button with different padding
|
|
$button2 = new Button(
|
|
text: 'Another Button',
|
|
style: 'm-5 p-15 bg-lime-500 hover:bg-green-200',
|
|
);
|
|
|
|
$button2->setOnClickAsync(
|
|
onClickAsync: function () {
|
|
// Fetch data from API (example: JSON placeholder)
|
|
$url = 'https://jsonplaceholder.typicode.com/todos/1';
|
|
|
|
$context = stream_context_create([
|
|
'http' => [
|
|
'timeout' => 10,
|
|
'method' => 'GET',
|
|
],
|
|
]);
|
|
|
|
$response = file_get_contents($url, false, $context);
|
|
|
|
if ($response === false) {
|
|
throw new \Exception('Failed to fetch data');
|
|
}
|
|
|
|
return json_decode($response, true);
|
|
},
|
|
|
|
onComplete: function ($data) use ($container) {
|
|
$statusLabel = new Label(
|
|
text: 'Klicken Sie den Button, um Daten zu laden...',
|
|
style: 'text-base text-gray-700',
|
|
);
|
|
|
|
$resultLabel = new Label(
|
|
text: '',
|
|
style: 'text-sm text-blue-600',
|
|
);
|
|
|
|
$statusLabel->setText('✓ Daten erfolgreich geladen!');
|
|
|
|
$formatted = 'ID: ' . ($data['id'] ?? 'N/A') . "\n";
|
|
$formatted .= 'Titel: ' . ($data['title'] ?? 'N/A') . "\n";
|
|
$formatted .= 'Erledigt: ' . (($data['completed'] ?? false) ? 'Ja' : 'Nein');
|
|
|
|
$resultLabel->setText($formatted);
|
|
$container->addComponent($statusLabel);
|
|
$container->addComponent($resultLabel);
|
|
},
|
|
|
|
onError: function ($error) {},
|
|
);
|
|
$container->addComponent($button2);
|
|
$app->setRoot($container);
|
|
$app->run();
|