diff --git a/.storybook/stories/forms/forms-input-group.stories.ts b/.storybook/stories/forms/forms-input-group.stories.ts new file mode 100644 index 0000000000..ed864abcb2 --- /dev/null +++ b/.storybook/stories/forms/forms-input-group.stories.ts @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2016-2024 Broadcom. All Rights Reserved. + * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. + * This software is released under MIT license. + * The full license information can be found in LICENSE in the root directory of this project. + */ + +import { ChangeDetectorRef, Component, Input } from '@angular/core'; +import { FormControl, FormGroup } from '@angular/forms'; +import { ClrFormLayout, ClrFormsModule, ClrLayoutModule } from '@clr/angular'; +import { argsToTemplate, moduleMetadata, StoryObj } from '@storybook/angular'; + +import { CommonModules } from '../../helpers/common'; + +@Component({ + selector: 'forms-input-group', + template: ` +
+ `, +}) +class FormsStoryComponent { + _isDisabled = false; + _isSuccess = false; + _isError = false; + + form = new FormGroup({ + text: new FormControl(), + }); + + @Input() clrLayout = ClrFormLayout.HORIZONTAL; + + constructor(private changeDetectorRef: ChangeDetectorRef) {} + + @Input() + get isDisabled() { + return this._isDisabled; + } + set isDisabled(value: boolean) { + this._isDisabled = value; + this.setControlsState(); + } + + @Input() + get isError() { + return this._isError; + } + set isError(value: boolean) { + this._isError = value; + this.setControlsState(); + } + + @Input() + get isSuccess() { + return this._isSuccess; + } + set isSuccess(value: boolean) { + this._isSuccess = value; + this.setControlsState(); + } + + setControlsState() { + this.form.enable(); + Object.keys(this.form.controls).forEach(control => { + if (this._isDisabled) { + this.form.get(control)?.disable(); + } else { + if (this._isError && !this._isSuccess) { + this.form.get(control).setErrors({ required: true }); + this.form.get(control).markAsTouched(); + this.form.get(control).markAsDirty(); + } else if (this._isSuccess) { + this.form.get(control).setErrors(null); + this.form.get(control).markAsTouched(); + } + } + }); + this.form.updateValueAndValidity(); + this.changeDetectorRef.detectChanges(); + } +} + +export default { + title: 'Forms/Input Group', + component: FormsStoryComponent, + decorators: [ + moduleMetadata({ + declarations: [FormsStoryComponent], + imports: [...CommonModules, ClrLayoutModule, ClrFormsModule], + }), + ], + argTypes: { + getProviderFromContainer: { control: { disable: true }, table: { disable: true } }, + triggerValidation: { control: { disable: true }, table: { disable: true } }, + clrLayout: { control: 'radio', options: Object.values(ClrFormLayout).filter(value => typeof value === 'string') }, + }, + args: { + clrLayout: ClrFormLayout.HORIZONTAL, + isDisabled: false, + isError: false, + isSuccess: false, + }, + render: (args: FormsStoryComponent) => ({ + props: { + ...args, + }, + template: ` +