import { AbstractControl, UntypedFormBuilder, FormsModule, ReactiveFormsModule, ValidationErrors, ValidatorFn, Validators, } from "@angular/forms"; import { Meta, StoryObj, moduleMetadata } from "@storybook/angular"; import { I18nService } from "@bitwarden/common/abstractions/i18n.service"; import { AsyncActionsModule } from "../async-actions"; import { ButtonModule } from "../button"; import { CheckboxModule } from "../checkbox"; import { IconButtonModule } from "../icon-button"; import { InputModule } from "../input/input.module"; import { RadioButtonModule } from "../radio-button"; import { SelectModule } from "../select"; import { I18nMockService } from "../utils/i18n-mock.service"; import { BitFormFieldComponent } from "./form-field.component"; import { FormFieldModule } from "./form-field.module"; export default { title: "Component Library/Form/Field", component: BitFormFieldComponent, decorators: [ moduleMetadata({ imports: [ FormsModule, ReactiveFormsModule, FormFieldModule, InputModule, ButtonModule, IconButtonModule, AsyncActionsModule, CheckboxModule, RadioButtonModule, SelectModule, ], providers: [ { provide: I18nService, useFactory: () => { return new I18nMockService({ selectPlaceholder: "-- Select --", required: "required", inputRequired: "Input is required.", inputEmail: "Input is not an email-address.", }); }, }, ], }), ], parameters: { design: { type: "figma", url: "https://www.figma.com/file/Zt3YSeb6E6lebAffrNLa0h/Tailwind-Component-Library?node-id=1881%3A17689", }, }, } as Meta; const fb = new UntypedFormBuilder(); const formObj = fb.group({ test: [""], required: ["", [Validators.required]], }); const defaultFormObj = fb.group({ name: ["", [Validators.required]], email: ["", [Validators.required, Validators.email, forbiddenNameValidator(/bit/i)]], terms: [false, [Validators.requiredTrue]], updates: ["yes"], }); // Custom error message, `message` is shown as the error message function forbiddenNameValidator(nameRe: RegExp): ValidatorFn { return (control: AbstractControl): ValidationErrors | null => { const forbidden = nameRe.test(control.value); return forbidden ? { forbiddenName: { message: "forbiddenName" } } : null; }; } function submit() { defaultFormObj.markAllAsTouched(); } type Story = StoryObj; export const Default: Story = { render: (args) => ({ props: { formObj: defaultFormObj, submit: submit, ...args, }, template: `
Label Optional Hint
`, }), }; export const Required: Story = { render: (args) => ({ props: { formObj: formObj, ...args, }, template: ` Label FormControl `, }), }; export const Hint: Story = { render: (args: BitFormFieldComponent) => ({ props: { formObj: formObj, ...args, }, template: ` FormControl Long hint text `, }), }; export const Disabled: Story = { render: (args) => ({ props: args, template: ` Label `, }), args: {}, }; export const InputGroup: Story = { render: (args) => ({ props: args, template: ` Label $ USD `, }), args: {}, }; export const ButtonInputGroup: Story = { render: (args) => ({ props: args, template: ` `, }), args: {}, }; export const DisabledButtonInputGroup: Story = { render: (args) => ({ props: args, template: ` Label `, }), args: {}, }; export const Select: Story = { render: (args: BitFormFieldComponent) => ({ props: args, template: ` Label `, }), args: {}, }; export const AdvancedSelect: Story = { render: (args: BitFormFieldComponent) => ({ props: args, template: ` Label `, }), }; export const Textarea: Story = { render: (args: BitFormFieldComponent) => ({ props: args, template: ` Textarea `, }), args: {}, };