-
-
Notifications
You must be signed in to change notification settings - Fork 641
[6.x] Add Info fieldtype #15368
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
Open
joshuablum
wants to merge
4
commits into
6.x
Choose a base branch
from
info-fieldtype
base: 6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[6.x] Add Info fieldtype #15368
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,29 @@ | ||
| <script setup> | ||
| import Fieldtype from '@/components/fieldtypes/fieldtype'; | ||
| import { Alert } from '@/components/ui'; | ||
| import markdown from '@/util/markdown'; | ||
| import { computed } from 'vue'; | ||
|
|
||
| const props = defineProps(Fieldtype.props); | ||
|
|
||
| const variant = computed(() => { | ||
| return { | ||
| notice: 'default', | ||
| tip: 'tip', | ||
| warning: 'warning', | ||
| important: 'error', | ||
| success: 'success', | ||
| }[props.config.state] ?? 'default'; | ||
| }); | ||
|
|
||
| const html = computed(() => markdown(__(props.config.content), { openLinksInNewTabs: true })); | ||
| </script> | ||
|
|
||
| <template> | ||
| <Alert :variant="variant" :icon="config.alert_icon"> | ||
| <div | ||
| class="st-text-trim-start [&_a]:font-medium [&_a]:underline [&_ol]:list-decimal [&_ol]:ps-5 [&_ul]:list-disc [&_ul]:ps-5" | ||
| v-html="html" | ||
| /> | ||
| </Alert> | ||
| </template> | ||
49 changes: 49 additions & 0 deletions
49
resources/js/tests/components/fieldtypes/InfoFieldtype.test.js
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,49 @@ | ||
| import { mount } from '@vue/test-utils'; | ||
| import { describe, expect, test } from 'vitest'; | ||
| import InfoFieldtype from '@/components/fieldtypes/InfoFieldtype.vue'; | ||
| import { Icon } from '@/components/ui'; | ||
|
|
||
| window.__ = (key) => key; | ||
|
|
||
| function mountField(config = {}) { | ||
| return mount(InfoFieldtype, { | ||
| props: { | ||
| value: null, | ||
| handle: 'info', | ||
| config, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| describe.each([ | ||
| ['notice', 'default'], | ||
| ['tip', 'tip'], | ||
| ['warning', 'warning'], | ||
| ['important', 'error'], | ||
| ['success', 'success'], | ||
| ])('%s state', (state, variant) => { | ||
| test(`uses the ${variant} alert variant`, () => { | ||
| const wrapper = mountField({ state, content: 'Something happened.' }); | ||
|
|
||
| expect(wrapper.get('[data-ui-alert]').attributes('data-variant')).toBe(variant); | ||
| }); | ||
| }); | ||
|
|
||
| test('renders sanitized markdown with links and lists', () => { | ||
| const wrapper = mountField({ | ||
| content: '- First item\n- [Statamic](https://statamic.com)\n\n<script>alert("nope")</script>', | ||
| }); | ||
|
|
||
| expect(wrapper.findAll('li')).toHaveLength(2); | ||
| expect(wrapper.get('a').attributes()).toMatchObject({ | ||
| href: 'https://statamic.com', | ||
| target: '_blank', | ||
| }); | ||
| expect(wrapper.find('script').exists()).toBe(false); | ||
| }); | ||
|
|
||
| test('uses a configured icon', () => { | ||
| const wrapper = mountField({ content: 'Hello', alert_icon: 'lightbulb-idea' }); | ||
|
|
||
| expect(wrapper.findComponent(Icon).props('name')).toBe('lightbulb-idea'); | ||
| }); |
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,60 @@ | ||
| <?php | ||
|
|
||
| namespace Statamic\Fieldtypes; | ||
|
|
||
| use Statamic\Fields\Fieldtype; | ||
|
|
||
| use function Statamic\trans as __; | ||
|
|
||
| class Info extends Fieldtype | ||
| { | ||
| protected $categories = ['special']; | ||
| protected $keywords = ['alert', 'message', 'notice', 'warning']; | ||
| protected $icon = 'info'; | ||
| protected $localizable = false; | ||
| protected $validatable = false; | ||
| protected $defaultable = false; | ||
|
|
||
| protected function configFieldItems(): array | ||
| { | ||
| return [ | ||
| [ | ||
| 'display' => __('Content'), | ||
| 'fields' => [ | ||
| 'content' => [ | ||
| 'display' => __('Content'), | ||
| 'instructions' => __('statamic::fieldtypes.info.config.content'), | ||
| 'type' => 'textarea', | ||
| ], | ||
| ], | ||
| ], | ||
| [ | ||
| 'display' => __('Appearance'), | ||
| 'fields' => [ | ||
| 'state' => [ | ||
| 'display' => __('State'), | ||
| 'instructions' => __('statamic::fieldtypes.info.config.state'), | ||
| 'type' => 'select', | ||
| 'default' => 'notice', | ||
| 'options' => [ | ||
| 'notice' => __('Notice'), | ||
| 'tip' => __('Tip'), | ||
| 'warning' => __('Warning'), | ||
| 'important' => __('Important Warning'), | ||
| 'success' => __('Success'), | ||
| ], | ||
| 'width' => 50, | ||
| ], | ||
| 'alert_icon' => [ | ||
| 'display' => __('Icon'), | ||
| 'instructions' => __('statamic::fieldtypes.info.config.alert_icon'), | ||
| 'type' => 'icon', | ||
| 'set' => 'default', | ||
| 'mode' => 'compact', | ||
| 'width' => 50, | ||
| ], | ||
| ], | ||
| ], | ||
| ]; | ||
| } | ||
| } |
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,59 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Fieldtypes; | ||
|
|
||
| use PHPUnit\Framework\Attributes\Test; | ||
| use Statamic\Fields\FieldTransformer; | ||
| use Statamic\Fieldtypes\Info; | ||
| use Tests\TestCase; | ||
|
|
||
| class InfoTest extends TestCase | ||
| { | ||
| #[Test] | ||
| public function it_preserves_the_custom_icon_when_saving_a_blueprint() | ||
| { | ||
| $iconHandle = (new Info)->configFields()->all()->first(fn ($field) => $field->type() === 'icon')->handle(); | ||
| $field = FieldTransformer::toVue([ | ||
| 'handle' => 'notice', | ||
| 'field' => [ | ||
| 'type' => 'info', | ||
| 'content' => 'Helpful information.', | ||
| $iconHandle => 'lightbulb-idea', | ||
| ], | ||
| ]); | ||
|
|
||
| $this->assertSame('info', $field['icon']); | ||
| $this->assertSame('lightbulb-idea', FieldTransformer::fromVue($field)['field'][$iconHandle] ?? null); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function it_is_a_non_data_fieldtype() | ||
| { | ||
| $fieldtype = new Info; | ||
|
|
||
| $this->assertSame(['special'], $fieldtype->categories()); | ||
| $this->assertFalse($fieldtype->localizable()); | ||
| $this->assertFalse($fieldtype->validatable()); | ||
| $this->assertFalse($fieldtype->defaultable()); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function it_has_configurable_content_state_and_icon() | ||
| { | ||
| $fields = (new Info)->configFields(); | ||
|
|
||
| $this->assertSame('textarea', $fields->get('content')->type()); | ||
| $this->assertSame('select', $fields->get('state')->type()); | ||
| $this->assertSame('notice', $fields->get('state')->get('default')); | ||
| $this->assertSame([ | ||
| 'notice' => 'Notice', | ||
| 'tip' => 'Tip', | ||
| 'warning' => 'Warning', | ||
| 'important' => 'Important Warning', | ||
| 'success' => 'Success', | ||
| ], $fields->get('state')->get('options')); | ||
| $this->assertSame('icon', $fields->get('alert_icon')->type()); | ||
| $this->assertSame('default', $fields->get('alert_icon')->get('set')); | ||
| $this->assertSame('compact', $fields->get('alert_icon')->get('mode')); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Warning — static field content rendered into a live ARIA region
Alertderives its ARIA semantics from the variant (resources/js/components/ui/Alert.vue:18-26):warninganderrorgetrole="alert"+aria-live="assertive", everything elserole="status"+aria-live="polite". Those roles are for messages that appear in response to something the user did.An Info field is static blueprint content. Because the publish form is client-rendered, the Alert is inserted into the DOM after load, which is exactly the case screen readers announce — so every entry-edit page load will announce the field's text, assertively for the
warningandimportantstates. A blueprint with two or three of these gets noisy fast.Alertitself is unchanged by this PR and is fine for its existing dynamic uses; it's the new static use that doesn't fit the semantics. Suggest giving Alert an opt-out (e.g. astatic/live=falseprop that omitsroleandaria-live) and setting it fromInfoFieldtype.vue.