-
-
Notifications
You must be signed in to change notification settings - Fork 0
[#106] Added choice field options that resolve from the collected answers. #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
49f6e5d
[#106] Added choice field options resolved from the collected answers.
AlexSkrypnyk efc636f
[#106] Folded answer-driven options into 'options()' and documented b…
AlexSkrypnyk b270c2b
[#106] Carried the run context through option resolution and validation.
AlexSkrypnyk 0b1ad9d
[#106] Narrowed the schema test helpers with type guards rather than …
AlexSkrypnyk cb6cdad
[#106] Satisfied the coding standards for the option guard and test n…
AlexSkrypnyk 3185c38
[#106] Formatted the documentation pages to the project style.
AlexSkrypnyk 460dc87
Addressed code review: stated the headless loader timing and the supp…
AlexSkrypnyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @file | ||
| * Options resolved from the answers collected so far. | ||
| * | ||
| * A callback handed to ->options() that asks for the run context is called | ||
| * again whenever the answers change, so one field's choices can narrow by | ||
| * another's answer. It runs as part of the form settling, before anything is | ||
| * drawn or validated, so the narrowed list is what the panel offers, what a | ||
| * headless payload is checked against and what the schema advertises - and a | ||
| * choice the narrowed list no longer holds is dropped from the answers, unless | ||
| * it was supplied headlessly, which collection reports instead. | ||
| * | ||
| * Usage: | ||
| * php playground/19-dynamic-options.php | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use DrevOps\Tui\Builder\Form; | ||
| use DrevOps\Tui\Builder\PanelBuilder; | ||
| use DrevOps\Tui\Handler\Context; | ||
| use DrevOps\Tui\InterruptException; | ||
| use DrevOps\Tui\Tui; | ||
|
|
||
| require __DIR__ . '/../vendor/autoload.php'; | ||
|
|
||
| $catalog = [ | ||
| 'fruit' => ['apple' => 'Apple', 'banana' => 'Banana', 'cherry' => 'Cherry'], | ||
| 'vegetable' => ['carrot' => 'Carrot', 'potato' => 'Potato', 'tomato' => 'Tomato'], | ||
| ]; | ||
|
|
||
| $form = Form::create('Quick start') | ||
| ->panel('order', 'New order', function (PanelBuilder $p) use ($catalog): void { | ||
| $p->text('name', 'Order name')->required(); | ||
|
|
||
| $p->select('category', 'Category')->options(['fruit' => 'Fruit', 'vegetable' => 'Vegetable'])->default('fruit'); | ||
|
|
||
| // An answer is whatever was supplied until it is validated, so the category | ||
| // is read defensively before it indexes anything. | ||
| $stock = static function (Context $context) use ($catalog): array { | ||
| $category = $context->answers['category'] ?? ''; | ||
|
|
||
| return is_string($category) ? ($catalog[$category] ?? []) : []; | ||
| }; | ||
|
|
||
| // Called again whenever the answers change: pick another category and the | ||
| // item list follows, dropping an item the new category does not stock. | ||
| $p->select('item', 'Item')->options($stock); | ||
|
|
||
| // The same narrowing over several picks - the basket keeps only what the | ||
| // chosen category still offers. | ||
| $p->select('basket', 'Basket')->multiple()->options($stock); | ||
| }); | ||
|
|
||
| try { | ||
| echo (new Tui($form))->run()->toJson() . PHP_EOL; | ||
| } | ||
| catch (InterruptException) { | ||
| exit(130); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.