|
7 | 7 | use Code16\Sharp\Form\Fields\SharpFormAutocompleteLocalField; |
8 | 8 | use Code16\Sharp\Form\Fields\SharpFormCheckField; |
9 | 9 | use Code16\Sharp\Form\Fields\SharpFormEditorField; |
| 10 | +use Code16\Sharp\Form\Fields\SharpFormHtmlField; |
| 11 | +use Code16\Sharp\Form\Fields\SharpFormListField; |
10 | 12 | use Code16\Sharp\Form\Fields\SharpFormTextField; |
11 | 13 | use Code16\Sharp\Form\Layout\FormLayout; |
12 | 14 | use Code16\Sharp\Form\Layout\FormLayoutColumn; |
| 15 | +use Code16\Sharp\Form\SharpForm; |
13 | 16 | use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity; |
14 | 17 | use Code16\Sharp\Tests\Fixtures\Entities\SinglePersonEntity; |
15 | 18 | use Code16\Sharp\Tests\Fixtures\Sharp\PersonForm; |
@@ -606,3 +609,77 @@ public function buildFormConfig(): void |
606 | 609 | ->where('form.title', 'My custom create title') |
607 | 610 | ); |
608 | 611 | }); |
| 612 | + |
| 613 | +it('handles html fields', function () { |
| 614 | + fakeFormFor('person', new class() extends SharpForm |
| 615 | + { |
| 616 | + public function buildFormFields(FieldsContainer $formFields): void |
| 617 | + { |
| 618 | + $formFields |
| 619 | + ->addField( |
| 620 | + SharpFormTextField::make('name') |
| 621 | + ) |
| 622 | + ->addField( |
| 623 | + SharpFormHtmlField::make('html_string') |
| 624 | + ->setTemplate('<h1>{{ $name }}</h1><p>{{ $text }}</p>') |
| 625 | + ) |
| 626 | + ->addField( |
| 627 | + SharpFormHtmlField::make('html_view') |
| 628 | + ->setTemplate(view('fixtures::form-html-field')) |
| 629 | + ) |
| 630 | + ->addField( |
| 631 | + SharpFormHtmlField::make('html_closure') |
| 632 | + ->setTemplate(fn ($data) => sprintf('<h1>%s</h1><p>%s</p>', $data['name'], $data['text']) |
| 633 | + ) |
| 634 | + ) |
| 635 | + ->addField( |
| 636 | + SharpFormListField::make('list') |
| 637 | + ->addItemField( |
| 638 | + SharpFormTextField::make('list_name') |
| 639 | + ) |
| 640 | + ->addItemField( |
| 641 | + SharpFormHtmlField::make('list_html') |
| 642 | + ->setTemplate(fn ($data) => sprintf('<h1>%s</h1><p>%s</p>', $data['list_name'], $data['text']) |
| 643 | + ) |
| 644 | + ) |
| 645 | + ); |
| 646 | + } |
| 647 | + |
| 648 | + public function buildFormLayout(FormLayout $formLayout): void |
| 649 | + { |
| 650 | + $formLayout->addColumn(12, function (FormLayoutColumn $column) { |
| 651 | + $column->withField('html_string') |
| 652 | + ->withField('html_view') |
| 653 | + ->withField('html_closure') |
| 654 | + ->withListField('list', function (FormLayoutColumn $column) { |
| 655 | + $column->withField('list_name') |
| 656 | + ->withField('list_html'); |
| 657 | + }); |
| 658 | + }); |
| 659 | + } |
| 660 | + |
| 661 | + public function find($id): array |
| 662 | + { |
| 663 | + return $this->setCustomTransformer('html_string', fn ($value) => ['text' => 'example']) |
| 664 | + ->setCustomTransformer('html_view', fn ($value) => ['text' => 'example']) |
| 665 | + ->setCustomTransformer('html_closure', fn ($value) => ['text' => 'example']) |
| 666 | + ->transform([ |
| 667 | + 'name' => 'Albert Einstein', |
| 668 | + 'list' => [ |
| 669 | + ['id' => 1, 'list_name' => 'Marie Curie', 'list_html' => ['text' => 'example']], |
| 670 | + ], |
| 671 | + ]); |
| 672 | + } |
| 673 | + |
| 674 | + public function update(mixed $id, array $data) {} |
| 675 | + }); |
| 676 | + |
| 677 | + $this->get('/sharp/s-list/person/s-form/person/1') |
| 678 | + ->assertOk() |
| 679 | + ->assertInertia(fn (Assert $page) => $page |
| 680 | + ->where('form.data.html_string', '<h1>Albert Einstein</h1><p>example</p>') |
| 681 | + ->where('form.data.html_view', "<h1>Albert Einstein</h1><p>example</p>\n") |
| 682 | + ->where('form.data.html_closure', '<h1>Albert Einstein</h1><p>example</p>') |
| 683 | + ->where('form.data.list.0.list_html', '<h1>Marie Curie</h1><p>example</p>') |
| 684 | + ); |
| 685 | +}); |
0 commit comments