diff --git a/core/api.txt b/core/api.txt index 67d49417556..c589bdeaca1 100644 --- a/core/api.txt +++ b/core/api.txt @@ -1619,8 +1619,10 @@ ion-select,prop,cancelText,string,'Cancel',false,false ion-select,prop,color,"danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string & Record | undefined,undefined,false,true ion-select,prop,compareWith,((currentValue: any, compareValue: any) => boolean) | null | string | undefined,undefined,false,false ion-select,prop,disabled,boolean,false,false,false +ion-select,prop,errorText,string | undefined,undefined,false,false ion-select,prop,expandedIcon,string | undefined,undefined,false,false ion-select,prop,fill,"outline" | "solid" | undefined,undefined,false,false +ion-select,prop,helperText,string | undefined,undefined,false,false ion-select,prop,interface,"action-sheet" | "alert" | "modal" | "popover",'alert',false,false ion-select,prop,interfaceOptions,any,{},false,false ion-select,prop,justify,"end" | "space-between" | "start" | undefined,undefined,false,false diff --git a/core/src/components.d.ts b/core/src/components.d.ts index 1bdfaa88545..a77c651c8ad 100644 --- a/core/src/components.d.ts +++ b/core/src/components.d.ts @@ -2755,6 +2755,10 @@ export namespace Components { * If `true`, the user cannot interact with the select. */ "disabled": boolean; + /** + * Text that is placed under the select and displayed when an error is detected. + */ + "errorText"?: string; /** * The toggle icon to show when the select is open. If defined, the icon rotation behavior in `md` mode will be disabled. If undefined, `toggleIcon` will be used for when the select is both open and closed. */ @@ -2763,6 +2767,10 @@ export namespace Components { * The fill for the item. If `"solid"` the item will have a background. If `"outline"` the item will be transparent with a border. Only available in `md` mode. */ "fill"?: 'outline' | 'solid'; + /** + * Text that is placed under the select and displayed when no error is detected. + */ + "helperText"?: string; /** * The interface the select should use: `action-sheet`, `popover`, `alert`, or `modal`. */ @@ -7568,6 +7576,10 @@ declare namespace LocalJSX { * If `true`, the user cannot interact with the select. */ "disabled"?: boolean; + /** + * Text that is placed under the select and displayed when an error is detected. + */ + "errorText"?: string; /** * The toggle icon to show when the select is open. If defined, the icon rotation behavior in `md` mode will be disabled. If undefined, `toggleIcon` will be used for when the select is both open and closed. */ @@ -7576,6 +7588,10 @@ declare namespace LocalJSX { * The fill for the item. If `"solid"` the item will have a background. If `"outline"` the item will be transparent with a border. Only available in `md` mode. */ "fill"?: 'outline' | 'solid'; + /** + * Text that is placed under the select and displayed when no error is detected. + */ + "helperText"?: string; /** * The interface the select should use: `action-sheet`, `popover`, `alert`, or `modal`. */ diff --git a/core/src/components/select/select.scss b/core/src/components/select/select.scss index 8b12f01ec1c..a9f9758869a 100644 --- a/core/src/components/select/select.scss +++ b/core/src/components/select/select.scss @@ -340,6 +340,34 @@ button { display: none; } +// Select Hint Text +// ---------------------------------------------------------------- + +/** + * Error text should only be shown when .ion-invalid is + * present on the input. Otherwise the helper text should + * be shown. + */ + .input-bottom .error-text { + display: none; + + color: var(--highlight-color-invalid); +} + +.input-bottom .helper-text { + display: block; + + color: #{$text-color-step-450}; +} + +:host(.ion-touched.ion-invalid) .input-bottom .error-text { + display: block; +} + +:host(.ion-touched.ion-invalid) .input-bottom .helper-text { + display: none; +} + // Select Native Wrapper // ---------------------------------------------------------------- diff --git a/core/src/components/select/select.tsx b/core/src/components/select/select.tsx index 3b4ef84f26f..818735f990d 100644 --- a/core/src/components/select/select.tsx +++ b/core/src/components/select/select.tsx @@ -52,6 +52,8 @@ import type { SelectChangeEventDetail, SelectInterface, SelectCompareFn } from ' }) export class Select implements ComponentInterface { private inputId = `ion-sel-${selectIds++}`; + private helperTextId = `${this.inputId}-helper-text`; + private errorTextId = `${this.inputId}-error-text`; private overlay?: OverlaySelect; private focusEl?: HTMLButtonElement; private mutationO?: MutationObserver; @@ -98,6 +100,16 @@ export class Select implements ComponentInterface { */ @Prop() fill?: 'outline' | 'solid'; + /** + * Text that is placed under the select and displayed when no error is detected. + */ + @Prop() helperText?: string; + + /** + * Text that is placed under the select and displayed when an error is detected. + */ + @Prop() errorText?: string; + /** * The interface the select should use: `action-sheet`, `popover`, `alert`, or `modal`. */ @@ -714,6 +726,21 @@ export class Select implements ComponentInterface { return this.getText() !== ''; } + /** + * Renders the helper text or error text values + */ + private renderHintText() { + const { helperText, errorText, helperTextId, errorTextId } = this; + + return [ +
+ {helperText} +
, +
+ {errorText} +
, + ]; + } private get childOpts() { return Array.from(this.el.querySelectorAll('ion-select-option')); } @@ -812,6 +839,26 @@ export class Select implements ComponentInterface { this.ionBlur.emit(); }; + /** + * Responsible for rendering helper text and + * error text. This element should only + * be rendered if hint text is set. + */ + private renderBottomContent() { + const { helperText, errorText } = this; + + /** + * undefined and empty string values should + * be treated as not having helper/error text. + */ + const hasHintText = !!helperText || !!errorText; + if (!hasHintText) { + return; + } + + return
{this.renderHintText()}
; + } + private renderLabel() { const { label } = this; @@ -1069,6 +1116,7 @@ export class Select implements ComponentInterface { {hasFloatingOrStackedLabel && this.renderSelectIcon()} {shouldRenderHighlight &&
} + {this.renderBottomContent()} ); } diff --git a/core/src/components/select/test/bottom-content/index.html b/core/src/components/select/test/bottom-content/index.html new file mode 100644 index 00000000000..7c694f501a2 --- /dev/null +++ b/core/src/components/select/test/bottom-content/index.html @@ -0,0 +1,99 @@ + + + + + Input - Bottom Content + + + + + + + + + + + + + + select - Bottom Content + + + + +
+
+

Select with Helper text

+ +
Favorite Fruit
+ Apple + Bananna +
+
+ +
+

Select with Error text

+ +
Favorite Fruit
+ Apple + Bananna +
+
+ +
+

Select with Helper and error text| Valid

+ +
Favorite Fruit
+ Apple + Bananna +
+
+ +
+

Select with no helper or error text

+ +
Favorite Fruit
+ Apple + Bananna +
+
+
+ + +
+
+ + diff --git a/core/src/components/select/test/bottom-content/select.e2e.ts b/core/src/components/select/test/bottom-content/select.e2e.ts new file mode 100644 index 00000000000..61e7d9528b9 --- /dev/null +++ b/core/src/components/select/test/bottom-content/select.e2e.ts @@ -0,0 +1,41 @@ +import { expect } from '@playwright/test'; +import { configs, test } from '@utils/test/playwright'; + +configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, screenshot, config }) => { + test.describe(title('Select: Helper and Error Text'), () => { + test.describe('Select with helper text', () => { + test('should set label and show helper text', async ({ page }) => { + await page.setContent( + ` + +
Favorite Fruit
+ Apple + Bananna +
+ `, + config + ); + + const select = page.locator('ion-select'); + await expect(select).toHaveScreenshot(screenshot(`select-helper-text`)); + }); + }); + test.describe('Select with Error text', () => { + test('should set label and show error text', async ({ page }) => { + await page.setContent( + ` + +
Favorite Fruit
+ Apple + Bananna +
+ `, + config + ); + + const select = page.locator('ion-select'); + await expect(select).toHaveScreenshot(screenshot(`select-error-text`)); + }); + }); + }); +}); diff --git a/packages/angular/src/directives/proxies.ts b/packages/angular/src/directives/proxies.ts index 675c37bd1c1..09e05390a90 100644 --- a/packages/angular/src/directives/proxies.ts +++ b/packages/angular/src/directives/proxies.ts @@ -2060,7 +2060,7 @@ export declare interface IonSegmentView extends Components.IonSegmentView { @ProxyCmp({ - inputs: ['cancelText', 'color', 'compareWith', 'disabled', 'expandedIcon', 'fill', 'interface', 'interfaceOptions', 'justify', 'label', 'labelPlacement', 'mode', 'multiple', 'name', 'okText', 'placeholder', 'selectedText', 'shape', 'toggleIcon', 'value'], + inputs: ['cancelText', 'color', 'compareWith', 'disabled', 'errorText', 'expandedIcon', 'fill', 'helperText', 'interface', 'interfaceOptions', 'justify', 'label', 'labelPlacement', 'mode', 'multiple', 'name', 'okText', 'placeholder', 'selectedText', 'shape', 'toggleIcon', 'value'], methods: ['open'] }) @Component({ @@ -2068,7 +2068,7 @@ export declare interface IonSegmentView extends Components.IonSegmentView { changeDetection: ChangeDetectionStrategy.OnPush, template: '', // eslint-disable-next-line @angular-eslint/no-inputs-metadata-property - inputs: ['cancelText', 'color', 'compareWith', 'disabled', 'expandedIcon', 'fill', 'interface', 'interfaceOptions', 'justify', 'label', 'labelPlacement', 'mode', 'multiple', 'name', 'okText', 'placeholder', 'selectedText', 'shape', 'toggleIcon', 'value'], + inputs: ['cancelText', 'color', 'compareWith', 'disabled', 'errorText', 'expandedIcon', 'fill', 'helperText', 'interface', 'interfaceOptions', 'justify', 'label', 'labelPlacement', 'mode', 'multiple', 'name', 'okText', 'placeholder', 'selectedText', 'shape', 'toggleIcon', 'value'], }) export class IonSelect { protected el: HTMLElement; diff --git a/packages/vue/src/proxies.ts b/packages/vue/src/proxies.ts index 7dd5812ebbc..079a64fd710 100644 --- a/packages/vue/src/proxies.ts +++ b/packages/vue/src/proxies.ts @@ -771,6 +771,8 @@ export const IonSelect = /*@__PURE__*/ defineContainer