-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an Api type to Freestyle::Usage for when you expect a component a…
…s argument
- Loading branch information
Showing
13 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
addon/components/freestyle/usage/component-like/control/index.hbs
This file contains 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,12 @@ | ||
{{! template-lint-disable require-input-label }} | ||
|
||
{{#if @options}} | ||
<select {{on 'change' this.callOnChange}}> | ||
{{#each @options as |componentLike|}} | ||
<option | ||
value={{componentLike.name}} | ||
selected={{eq componentLike.name @value.name}} | ||
>{{componentLike.name}}</option> | ||
{{/each}} | ||
</select> | ||
{{/if}} |
21 changes: 21 additions & 0 deletions
21
addon/components/freestyle/usage/component-like/control/index.ts
This file contains 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,21 @@ | ||
import Component from '@glimmer/component'; | ||
import { action } from '@ember/object'; | ||
import type { ComponentLike } from '@glint/template'; | ||
|
||
interface Signature { | ||
Args: { | ||
value?: ComponentLike; | ||
onChange: (c: ComponentLike | undefined) => void; | ||
options?: ComponentLike[]; | ||
}; | ||
} | ||
export default class FreestyleUsageComponentLikeControlComponent extends Component<Signature> { | ||
@action | ||
callOnChange(event: InputEvent): void { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const eventTarget = event.target as any; | ||
const componentName = eventTarget?.value; | ||
const value = this.args.options?.find((c) => c.name === componentName); | ||
this.args.onChange(value); | ||
} | ||
} |
This file contains 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,14 @@ | ||
<Freestyle::Usage::Argument | ||
@type="ComponentLike" | ||
@name={{@name}} | ||
@description={{@description}} | ||
@required={{@required}} | ||
@defaultValue={{@defaultValue}} | ||
@hideControls={{@hideControls}} | ||
> | ||
<Freestyle::Usage::ComponentLike::Control | ||
@options={{@options}} | ||
@value={{@value}} | ||
@onChange={{@onChange}} | ||
/> | ||
</Freestyle::Usage::Argument> |
This file contains 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,17 @@ | ||
import templateOnlyComponent from '@ember/component/template-only'; | ||
import type { ComponentLike } from '@glint/template'; | ||
|
||
interface Signature { | ||
Args: { | ||
name?: string; | ||
description?: string; | ||
required?: boolean; | ||
defaultValue?: ComponentLike; | ||
hideControls?: boolean; | ||
value?: ComponentLike; | ||
options?: ComponentLike[]; | ||
onChange: (val: ComponentLike | null | undefined) => void; | ||
}; | ||
} | ||
const FreestyleUsageComponentLikeComponent = templateOnlyComponent<Signature>(); | ||
export default FreestyleUsageComponentLikeComponent; |
This file contains 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 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 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 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 @@ | ||
export { default } from 'ember-freestyle/components/freestyle/usage/component-like'; |
This file contains 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 @@ | ||
export { default } from 'ember-freestyle/components/freestyle/usage/component-like/control'; |
This file contains 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 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
55 changes: 55 additions & 0 deletions
55
tests/integration/components/freestyle/usage/component-like-test.ts
This file contains 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,55 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { render, select } from '@ember/test-helpers'; | ||
import { hbs } from 'ember-cli-htmlbars'; | ||
import { TestContext } from '@ember/test-helpers'; | ||
import ProgressIcon from 'dummy/components/progress-icon'; | ||
import MarkdownContent from 'dummy/components/markdown-content'; | ||
import type { ComponentLike } from '@glint/template'; | ||
|
||
interface Context extends TestContext { | ||
value: ComponentLike; | ||
options?: ComponentLike[]; | ||
onChange: (val?: ComponentLike) => void; | ||
} | ||
|
||
const ARGUMENT = '.FreestyleUsageArgument'; | ||
const NAME = `${ARGUMENT}-name`; | ||
const CONTROL = `${ARGUMENT}-controls--ComponentLike input`; | ||
const SELECT_CONTROL = `${ARGUMENT}-controls--ComponentLike select`; | ||
|
||
module( | ||
'Integration | Component | freestyle/usage/component-like', | ||
function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('renders a select list', async function (this: Context, assert) { | ||
assert.expect(4); | ||
this.set('value', ProgressIcon); | ||
this.set('options', [ProgressIcon, MarkdownContent]); | ||
this.set('onChange', function (value: ComponentLike) { | ||
assert.strictEqual(value, MarkdownContent); | ||
}); | ||
await render<Context>(hbs`<Freestyle::Usage::ComponentLike | ||
@name="Name" | ||
@value={{this.value}} | ||
@options={{this.options}} | ||
@onChange={{this.onChange}} | ||
/>`); | ||
|
||
assert.dom(NAME).hasText('Name'); | ||
assert.dom(SELECT_CONTROL).exists(); | ||
assert.dom(SELECT_CONTROL).hasValue('ProgressIcon'); | ||
await select(SELECT_CONTROL, 'MarkdownContent'); | ||
}); | ||
|
||
test('Does not render the controls', async function (this: Context, assert) { | ||
await render<Context>(hbs`<Freestyle::Usage::ComponentLike | ||
@hideControls={{true}} | ||
@onChange={{this.onChange}} | ||
/>`); | ||
|
||
assert.dom(CONTROL).doesNotExist(); | ||
}); | ||
} | ||
); |
This file contains 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