Skip to content

feat(textarea): support notch with label slot #27660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion core/src/components/textarea/test/fill/textarea.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ configs({ modes: ['md'] }).forEach(({ title, screenshot, config }) => {
helper-text="Enter your email"
maxlength="20"
counter="true"
></ion-input>
></ion-textarea>
`,
config
);
Expand Down Expand Up @@ -196,3 +196,58 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
});
});
});

configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, screenshot, config }) => {
test.describe(title('textarea: label slot'), () => {
test('should render the notch correctly with a slotted label', async ({ page }) => {
await page.setContent(
`
<style>
.custom-label {
font-size: 30px;
}
</style>
<ion-textarea
fill="outline"
label-placement="stacked"
value="apple"
>
<div slot="label" class="custom-label">My Label Content</div>
</ion-textarea>
`,
config
);

const textarea = page.locator('ion-textarea');
expect(await textarea.screenshot()).toMatchSnapshot(screenshot(`textarea-fill-outline-slotted-label`));
});
test('should render the notch correctly with a slotted label after the textarea was originally hidden', async ({
page,
}) => {
await page.setContent(
`
<style>
.custom-label {
font-size: 30px;
}
</style>
<ion-textarea
fill="outline"
label-placement="stacked"
value="apple"
style="display: none"
>
<div slot="label" class="custom-label">My Label Content</div>
</ion-textarea>
`,
config
);

const textarea = page.locator('ion-textarea');

await textarea.evaluate((el: HTMLIonSelectElement) => el.style.removeProperty('display'));

expect(await textarea.screenshot()).toMatchSnapshot(screenshot(`textarea-fill-outline-hidden-slotted-label`));
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions core/src/components/textarea/textarea.md.outline.scss
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@

opacity: 0;
pointer-events: none;

/**
* The spacer currently inherits
* border-box sizing from the Ionic reset styles.
* However, we do not want to include padding in
* the calculation of the element dimensions.
* This code can be removed if textarea is updated
* to use the Shadow DOM.
*/
box-sizing: content-box;
}

:host(.textarea-fill-outline) .textarea-outline-start {
Expand Down
23 changes: 20 additions & 3 deletions core/src/components/textarea/textarea.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ComponentInterface, EventEmitter } from '@stencil/core';
import { Build, Component, Element, Event, Host, Method, Prop, State, Watch, h, writeTask } from '@stencil/core';
import type { LegacyFormController } from '@utils/forms';
import { createLegacyFormController } from '@utils/forms';
import type { LegacyFormController, NotchController } from '@utils/forms';
import { createLegacyFormController, createNotchController } from '@utils/forms';
import type { Attributes } from '@utils/helpers';
import { inheritAriaAttributes, debounceEvent, findItemLabel, inheritAttributes } from '@utils/helpers';
import { printIonWarning } from '@utils/logging';
Expand Down Expand Up @@ -40,6 +40,9 @@ export class Textarea implements ComponentInterface {
private inheritedAttributes: Attributes = {};
private originalIonInput?: EventEmitter<TextareaInputEventDetail>;
private legacyFormController!: LegacyFormController;
private notchSpacerEl: HTMLElement | undefined;

private notchController?: NotchController;

// This flag ensures we log the deprecation warning at most once.
private hasLoggedDeprecationWarning = false;
Expand Down Expand Up @@ -292,6 +295,11 @@ export class Textarea implements ComponentInterface {
connectedCallback() {
const { el } = this;
this.legacyFormController = createLegacyFormController(el);
this.notchController = createNotchController(
el,
() => this.notchSpacerEl,
() => this.labelSlot
);
this.emitStyle();
this.debounceChanged();
if (Build.isBrowser) {
Expand All @@ -311,6 +319,11 @@ export class Textarea implements ComponentInterface {
})
);
}

if (this.notchController) {
this.notchController.destroy();
this.notchController = undefined;
}
}

componentWillLoad() {
Expand All @@ -325,6 +338,10 @@ export class Textarea implements ComponentInterface {
this.runAutoGrow();
}

componentDidRender() {
this.notchController?.calculateNotchWidth();
}

/**
* Sets focus on the native `textarea` in `ion-textarea`. Use this method instead of the global
* `textarea.focus()`.
Expand Down Expand Up @@ -591,7 +608,7 @@ Developers can use the "legacy" property to continue using the legacy form marku
'textarea-outline-notch-hidden': !this.hasLabel,
}}
>
<div class="notch-spacer" aria-hidden="true">
<div class="notch-spacer" aria-hidden="true" ref={(el) => (this.notchSpacerEl = el)}>
{this.label}
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion core/src/utils/forms/notch-controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { win } from '@utils/browser';
import { raf } from '@utils/helpers';

type NotchElement = HTMLIonInputElement | HTMLIonSelectElement;
type NotchElement = HTMLIonInputElement | HTMLIonSelectElement | HTMLIonTextareaElement;

/**
* A utility to calculate the size of an outline notch
Expand Down