8

I'm trying to make a Story that uses a component that takes a TemplateRef as an @Input. But I can't figure out how.

TestComponent

@Component({
  selector: 'test',
  templateUrl: './test.component.html',
})
export class TestComponent {
  @Input() public set value(value: TemplateRef<void>) {
    // ...
  }
}

Story

export default {
  title: 'Components/Test',
  component: TestComponent,
} as Meta;

const Template: Story<TestComponent> = (args) => ({
  props: args,
});

export const TemplateRefStory = Template.bind({});
TemplateRefStory.args = {
  value: ???, // <-- what do I put here?
};

I've tried a variety of things, they're similar to the below. Which doesn't really make sense.

export const TemplateRef: Story<TestComponent> = (args) => ({
  template: `<ng-template #hello>Hello</ng-template>`,
  props: args,
});

TemplateRef.args = {
  value: '#hello',
};
Snæbjørn
  • 10,322
  • 14
  • 65
  • 124

1 Answers1

0

I followed a approach like this and it worked

   export default {
    title: 'StepperComponent',
    component: StepperComponent,
    decorators: [
      moduleMetadata({
        imports: [
          CommonModule,
          BrowserAnimationsModule,
          MaterialModule
        ],
      })
    ],
  } as Meta<StepperComponent>;
  
  const actionsData = {
    checked: action('checked change: '),
  
  };



 export const Template: Story = (args) => ({
    props: {
      ...args
    },
    template: `

      <app-stepper 
        [steps]="[{stepName: 'Connectivity Type', stepTemplate: step1Template}, {stepName: 'Database Authentication', stepTemplate: step2Template}]">
      </app-stepper>

      <ng-template #step1Template>
        <div>Step 1</div>
      </ng-template>
      <ng-template #step2Template>
        <div>Step 2</div>
      </ng-template>
    `
  });

Here I have defined the the elements I need, within the story template. I assume you can also include components within the ng-template tags too.

  <ng-template #step2Template>
    <cmp-test></cmp-test>
  </ng-template>

Don't forget to add BrowserAnimationsModule

Kelum Bandara
  • 443
  • 4
  • 9