Skip to content

Commit

Permalink
fix(ui): add eventListeners to textarea (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
angsherpa456 committed Feb 6, 2024
1 parent bfedd01 commit fe32971
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 53 deletions.
55 changes: 10 additions & 45 deletions packages/ui-library/src/components/forms/textarea/index.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { html } from 'lit-html';
import { BlrTextareaType } from './index';
import { BlrTextareaRenderFunction } from './renderFunction';
import { FormSizes, Resizes } from '../../../globals/constants';
import { action } from '@storybook/addon-actions';
import { Themes } from '../../../foundation/_tokens-generated/index.themes';
import { PureIconKeys } from '@boiler/icons';

Expand All @@ -25,31 +24,30 @@ const sharedStyles = html`
// Default parameters for Textarea component
const defaultParams: BlrTextareaType = {
size: 'md',
isResizeable: 'none',
isResizeable: 'both',
cols: 40,
rows: 4,
placeholder: 'Placeholder-text',
value: '',
minLength: 10,
maxLength: 140,
hasLabel: true,
label: 'Label-text',
labelAppendix: '',
labelAppendix: '(Appendix)',
hasHint: false,
hintMessage: 'This is a small hint',
hintMessage: 'This is a small hint message',
hintIcon: 'blrInfo',
arialabel: '',
name: '',
showCounter: false,
arialabel: 'Text Area',
name: 'Text Area',
showCounter: true,
theme: 'Light',
textareaId: '',
textareaId: '#textAreaId',
warningLimitType: 'warningLimitInt',
warningLimitInt: 105,
warningLimitPer: 75,

required: false,
disabled: false,
readonly: false,

hasError: false,
errorMessage: '',
errorIcon: 'blr360',
Expand Down Expand Up @@ -414,41 +412,8 @@ export const TextArea = (params: BlrTextareaType) =>
...params,
})}
</div> `;
const args: BlrTextareaType = {
theme: 'Light',
size: 'md',
isResizeable: 'both',
cols: 40,
rows: 4,
placeholder: 'Placeholder-text',
value: '',
minLength: 0,
maxLength: 140,
hasLabel: true,
label: 'Label-text',
labelAppendix: '(Appendix)',
hasHint: false,
hintMessage: 'This is a small hint message',
hintIcon: 'blrInfo',
showCounter: true,
warningLimitType: 'warningLimitInt',
warningLimitInt: 105,
warningLimitPer: 75,
disabled: false,
readonly: false,
required: false,
hasError: false,
errorMessage: '',
errorIcon: undefined,
arialabel: 'Text Area',
textareaId: '#textAreaId',
name: 'Text Area',
onChange: () => action('onChange'),
onFocus: () => action('onFocus'),
onSelect: () => action('onSelect'),
onBlur: () => action('onBlur'),
};
TextArea.args = args;

TextArea.args = defaultParams;

//disabledArgTypesTable to deactivate the controls-Panel for a story in storybook
const argTypesToDisable = [
Expand Down
54 changes: 46 additions & 8 deletions packages/ui-library/src/components/forms/textarea/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ export class BlrTextarea extends LitElement {
@property() hasLabel?: boolean;
@property() size?: FormSizesType = 'md';
@property() required?: boolean;
@property() onChange?: HTMLElement['oninput'];
@property() onBlur?: HTMLElement['blur'];
@property() onFocus?: HTMLElement['focus'];
@property() maxLength?: number;
@property() minLength?: number;
@property() warningLimitType: WarningLimits = 'warningLimitInt';
Expand All @@ -49,10 +46,15 @@ export class BlrTextarea extends LitElement {
@property() isResizeable: ResizeType = 'none';
@property() rows?: number;
@property() cols?: number;
@property() onSelect?: HTMLElement['onselect'];
@property() name?: string;
@property() theme: ThemeType = 'Light';

// these are not triggered directly but allows us to map it internally and bve typesafe
@property() blrFocus?: () => void;
@property() blrBlur?: () => void;
@property() blrChange?: () => void;
@property() blrSelect?: () => void;

@state() protected count = 0;
@query('textarea') protected textareaElement: HTMLTextAreaElement | undefined;

Expand Down Expand Up @@ -105,6 +107,42 @@ export class BlrTextarea extends LitElement {
return counterVariant;
}

protected handleChange = (event: Event) => {
if (!this.disabled) {
this.dispatchEvent(
new CustomEvent('blrChange', { bubbles: true, composed: true, detail: { originalEvent: event } })
);
}
};

protected handleFocus = (event: Event) => {
if (!this.disabled) {
this.dispatchEvent(
new CustomEvent('blrFocus', { bubbles: true, composed: true, detail: { originalEvent: event } })
);
}
};

protected handleBlur = (event: Event) => {
if (!this.disabled) {
this.dispatchEvent(
new CustomEvent('blrBlur', { bubbles: true, composed: true, detail: { originalEvent: event } })
);
}
};

protected handleSelect = (event: Event) => {
if (!this.disabled) {
this.dispatchEvent(
new CustomEvent('blrSelect', {
bubbles: true,
composed: true,
detail: { originalEvent: event, value: this.textareaElement?.value },
})
);
}
};

protected render() {
if (this.size) {
const dynamicStyles = this.theme === 'Light' ? [formLight, textAreaLight] : [formDark, textAreaDark];
Expand Down Expand Up @@ -184,10 +222,10 @@ export class BlrTextarea extends LitElement {
?required="${this.required}"
?disabled="${this.disabled}"
?readonly="${this.readonly}"
@input="${this.onChange}"
@focus=${this.focus}
@blur=${this.blur}
@select="${this.onSelect}"
@input=${this.handleChange}
@focus=${this.handleFocus}
@blur=${this.handleBlur}
@select=${this.handleSelect}
@keyup=${this.updateCounter}
>
${this.value}
Expand Down

0 comments on commit fe32971

Please sign in to comment.