Skip to content

Commit

Permalink
feat: add code examples for help key (#2903)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackbaud-ErikaMcVey authored Nov 18, 2024
1 parent 8518e6b commit a9bacda
Show file tree
Hide file tree
Showing 67 changed files with 2,184 additions and 229 deletions.
2 changes: 1 addition & 1 deletion apps/code-examples/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const routes: Routes = [
{
path: 'angular-tree',
loadChildren: () =>
import('./features/angular-tree.modules').then(
import('./features/angular-tree.module').then(
(m) => m.AngularTreeFeatureModule,
),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<sky-angular-tree-wrapper>
<tree-root #tree [nodes]="nodes">
<ng-template
#treeNodeFullTemplate
let-index="index"
let-node
let-templates="templates"
>
<sky-angular-tree-node
[attr.data-sky-id]="'node-' + node.data.name"
[helpKey]="node.data.helpKey"
[helpPopoverContent]="node.data.helpPopoverContent"
[helpPopoverTitle]="node.data.helpPopoverTitle"
[index]="index"
[node]="node"
[templates]="templates"
/>
</ng-template>
</tree-root>
</sky-angular-tree-wrapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { TreeModule } from '@blackbaud/angular-tree-component';
import { SkyAngularTreeModule } from '@skyux/angular-tree-component';
import { SkyHelpInlineModule } from '@skyux/help-inline';

@Component({
standalone: true,
selector: 'app-demo',
templateUrl: './demo.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [SkyAngularTreeModule, SkyHelpInlineModule, TreeModule],
})
export class DemoComponent {
protected nodes = [
{
name: 'Animals',
isExpanded: true,
helpPopoverContent: 'Example help content for animals.',
children: [
{
name: 'Cats',
isExpanded: true,
helpPopoverContent: 'Example help content for cats.',
helpPopoverTitle: 'What is a cat?',
children: [
{ name: 'Burmese', helpKey: 'cat-burmese' },
{ name: 'Persian', helpKey: 'cat-persian' },
{ name: 'Tabby', helpKey: 'cat-tabby' },
],
},
{
name: 'Dogs',
isExpanded: true,
children: [
{
name: 'Beagle',
helpPopoverContent: 'Example help content for beagles.',
},
{ name: 'German shepherd', helpKey: 'dog-shepherd' },
{ name: 'Labrador retriever', helpKey: 'dog-lab' },
],
},
],
},
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<form [formGroup]="formGroup" (ngSubmit)="submit()">
<sky-colorpicker
#colorPicker
data-sky-id="favorite-color"
labelText="What is your favorite color?"
helpKey="color-help"
hintText="Pick a color with at least 80% opacity."
[stacked]="true"
(selectedColorChanged)="onSelectedColorChanged($event)"
>
<input
formControlName="favoriteColor"
type="text"
[presetColors]="swatches"
[skyColorpickerInput]="colorPicker"
/>
@if (favoriteColor.errors?.['opaque']) {
<sky-form-error
errorName="opaque"
errorText="Color must have at least 80% opacity."
/>
}
</sky-colorpicker>

<button class="sky-btn sky-btn-primary" type="submit">Submit</button>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { SkyColorpickerHarness } from '@skyux/colorpicker/testing';
import {
SkyHelpTestingController,
SkyHelpTestingModule,
} from '@skyux/core/testing';

import { DemoComponent } from './demo.component';

describe('Basic colorpicker demo', () => {
async function setupTest(options: { dataSkyId: string }): Promise<{
harness: SkyColorpickerHarness;
fixture: ComponentFixture<DemoComponent>;
helpController: SkyHelpTestingController;
}> {
const fixture = TestBed.createComponent(DemoComponent);
const loader = TestbedHarnessEnvironment.loader(fixture);
const helpController = TestBed.inject(SkyHelpTestingController);

const harness = await loader.getHarness(
SkyColorpickerHarness.with({ dataSkyId: options.dataSkyId }),
);

fixture.detectChanges();
await fixture.whenStable();

return { harness, fixture, helpController };
}

beforeEach(() => {
TestBed.configureTestingModule({
imports: [DemoComponent, SkyHelpTestingModule],
});
});

it('should have the initial values set', async () => {
const { harness } = await setupTest({ dataSkyId: 'favorite-color' });

await expectAsync(harness.getLabelText()).toBeResolvedTo(
'What is your favorite color?',
);
await expectAsync(harness.getHintText()).toBeResolvedTo(
'Pick a color with at least 80% opacity.',
);
});

it('should throw an error if a low opacity color is selected', async () => {
const { harness, fixture } = await setupTest({
dataSkyId: 'favorite-color',
});

await harness.clickColorpickerButton();
await expectAsync(harness.isColorpickerOpen()).toBeResolvedTo(true);

const dropdown = await harness.getColorpickerDropdown();

await dropdown.setAlphaValue('.2');
await dropdown.clickApplyButton();
fixture.detectChanges();

await expectAsync(harness.hasError('opaque')).toBeResolvedTo(true);
});

it('should have the correct help key', async () => {
const { harness, helpController } = await setupTest({
dataSkyId: 'favorite-color',
});

await harness.clickHelpInline();

helpController.expectCurrentHelpKey('color-help');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Component, inject } from '@angular/core';
import {
FormBuilder,
FormControl,
FormGroup,
ReactiveFormsModule,
ValidationErrors,
} from '@angular/forms';
import { SkyColorpickerModule, SkyColorpickerOutput } from '@skyux/colorpicker';

interface DemoForm {
favoriteColor: FormControl<SkyColorpickerOutput | string>;
}

function isColorpickerOutput(value: unknown): value is SkyColorpickerOutput {
return !!(value && typeof value === 'object' && 'rgba' in value);
}

@Component({
standalone: true,
selector: 'app-demo',
templateUrl: './demo.component.html',
imports: [ReactiveFormsModule, SkyColorpickerModule],
})
export class DemoComponent {
protected favoriteColor: FormControl<SkyColorpickerOutput | string>;
protected formGroup: FormGroup<DemoForm>;

protected swatches: string[] = [
'#BD4040',
'#617FC2',
'#60AC68',
'#3486BA',
'#E87134',
'#DA9C9C',
];

constructor() {
this.favoriteColor = new FormControl('#f00', {
nonNullable: true,
validators: [
(control): ValidationErrors | null => {
return isColorpickerOutput(control.value) &&
control.value.rgba.alpha < 0.8
? { opaque: true }
: null;
},
],
});

this.formGroup = inject(FormBuilder).group<DemoForm>({
favoriteColor: this.favoriteColor,
});
}

protected onSelectedColorChanged(args: SkyColorpickerOutput): void {
console.log('Reactive form color changed:', args);
}

protected submit(): void {
const controlValue = this.favoriteColor.value;
const favoriteColor = isColorpickerOutput(controlValue)
? controlValue.hex
: controlValue;

alert('Your favorite color is: \n' + favoriteColor);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
<div class="sky-padding-even-lg">
<form [formGroup]="formGroup">
<div class="sky-margin-stacked-lg">
<sky-date-range-picker
data-sky-id="last-donation"
formControlName="lastDonation"
[dateFormat]="dateFormat"
[helpPopoverContent]="inlineHelpPopover"
[hintText]="hintText"
[labelText]="labelText"
[required]="required"
>
@if (lastDonation.errors?.['dateWeekend']) {
<sky-form-error
errorName="dateWeekend"
errorText="Do not pick a weekend."
/>
}
</sky-date-range-picker>
</div>
<sky-date-range-picker
data-sky-id="last-donation"
formControlName="lastDonation"
[dateFormat]="dateFormat"
[helpPopoverContent]="inlineHelpPopover"
[hintText]="hintText"
[labelText]="labelText"
[required]="required"
>
@if (lastDonation.errors?.['dateWeekend']) {
<sky-form-error
errorName="dateWeekend"
errorText="Do not pick a weekend."
/>
}
</sky-date-range-picker>
</form>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
<div class="sky-padding-even-lg">
<form [formGroup]="formGroup">
<div class="sky-margin-stacked-lg">
<sky-date-range-picker
formControlName="lastDonation"
[calculatorIds]="customCalculators"
[dateFormat]="dateFormat"
[helpPopoverContent]="inlineHelpPopover"
[hintText]="hintText"
[labelText]="labelText"
[required]="required"
>
@if (
lastDonation.errors?.['skyDateRange']?.errors?.['dateIsAfterToday']
) {
<sky-form-error
errorName="dateIsAfterToday"
errorText="Enter a date before today."
/>
}
</sky-date-range-picker>
</div>
<sky-date-range-picker
formControlName="lastDonation"
[calculatorIds]="customCalculators"
[dateFormat]="dateFormat"
[helpPopoverContent]="inlineHelpPopover"
[hintText]="hintText"
[labelText]="labelText"
[required]="required"
>
@if (
lastDonation.errors?.['skyDateRange']?.errors?.['dateIsAfterToday']
) {
<sky-form-error
errorName="dateIsAfterToday"
errorText="Enter a date before today."
/>
}
</sky-date-range-picker>
</form>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div class="sky-padding-even-lg">
<form [formGroup]="formGroup">
<sky-date-range-picker
data-sky-id="last-donation"
formControlName="lastDonation"
helpKey="dates-help"
[dateFormat]="dateFormat"
[hintText]="hintText"
[labelText]="labelText"
[required]="required"
>
@if (lastDonation.errors?.['dateWeekend']) {
<sky-form-error
errorName="dateWeekend"
errorText="Do not pick a weekend."
/>
}
</sky-date-range-picker>
</form>
</div>
Loading

0 comments on commit a9bacda

Please sign in to comment.