Skip to content
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

1960 ic text field autofocus prop not working unless text field label is hidden #2901

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
44 changes: 22 additions & 22 deletions packages/docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -15696,28 +15696,6 @@
],
"usage": {},
"props": [
{
"name": "autoFocus",
"type": "boolean",
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"mutable": false,
"attr": "auto-focus",
"reflectToAttr": false,
"docs": "If `true`, the form control will have input focus when the page loads.",
"docsTags": [],
"default": "false",
"values": [
{
"type": "boolean"
}
],
"optional": false,
"required": false
},
{
"name": "autocapitalize",
"type": "string",
Expand Down Expand Up @@ -16014,6 +15992,28 @@
"optional": false,
"required": false
},
{
"name": "autofocus",
"type": "boolean",
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"mutable": false,
"attr": "autofocus",
"reflectToAttr": false,
"docs": "If `true`, the form control will have input focus when the page loads.",
"docsTags": [],
"default": "false",
"values": [
{
"type": "boolean"
}
],
"optional": false,
"required": false
},
{
"name": "debounce",
"type": "number",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from "../utils/constants";
import {
CustomSizeTextField,
AutofocusTextField,
DisabledTextField,
FullWidthTextField,
HiddenLabelTextField,
Expand Down Expand Up @@ -333,6 +334,14 @@ describe("IcTextField end-to-end tests", () => {

input.should(HAVE_ATTR, "title", "new-input-title");
});

it("should autofocus when autofocus attribute is set", () => {
mount(<AutofocusTextField />);

const input = cy.findShadowEl(IC_TEXTFIELD, TEXTFIELD_INPUT);

input.should(HAVE_FOCUS);
});
});

describe("IcTextField visual regression tests", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ export const HiddenLabelTextField = (): ReactElement => (
</div>
);

export const AutofocusTextField = (): ReactElement => (
<div style={style}>
<IcTextField
maxCharacters={25}
label="What is your favourite coffee?"
placeholder="Please enter…"
helperText="Such as Arabica, Robusta or Liberica"
autofocus
>
<ReusableSlottedIcon />
</IcTextField>
</div>
);

export const TextFieldWithMinMaxCharacters = (): ReactElement => (
<>
<div style={style}>
Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/stories/ic-text-field.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export const ControlledExample = () => {
</Story>
</Canvas>

### Autofocus

<Canvas>
<Story name="Autofocus">
<IcTextField label="What is your favourite coffee?" autofocus/>
</Story>
</Canvas>

### Placeholder and required

<Canvas>
Expand Down
20 changes: 10 additions & 10 deletions packages/web-components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ export namespace Components {
* The reference to an anchor element the menu will position itself from when rendered.
*/
"anchorEl": HTMLElement;
"autoFocusOnSelected": boolean;
"autofocusOnSelected": boolean;
/**
* If `true`, the menu will close when an option is selected.
*/
Expand Down Expand Up @@ -2031,10 +2031,6 @@ export namespace Components {
"ariaAutocomplete": IcAriaAutocompleteTypes;
"ariaExpanded": string;
"ariaOwns": string;
/**
* If `true`, the form control will have input focus when the page loads.
*/
"autoFocus": boolean;
/**
* The automatic capitalisation of the text value as it is entered/edited by the user. Available options: "off", "none", "on", "sentences", "words", "characters".
*/
Expand All @@ -2047,6 +2043,10 @@ export namespace Components {
* The state of autocorrection the browser can apply when the user is entering/editing the text value.
*/
"autocorrect": IcAutocorrectStates;
/**
* If `true`, the form control will have input focus when the page loads.
*/
"autofocus": boolean;
/**
* The amount of time, in milliseconds, to wait to trigger the `icChange` event after each keystroke.
*/
Expand Down Expand Up @@ -4390,7 +4390,7 @@ declare namespace LocalJSX {
* The reference to an anchor element the menu will position itself from when rendered.
*/
"anchorEl": HTMLElement;
"autoFocusOnSelected"?: boolean;
"autofocusOnSelected"?: boolean;
/**
* If `true`, the menu will close when an option is selected.
*/
Expand Down Expand Up @@ -5464,10 +5464,6 @@ declare namespace LocalJSX {
"ariaAutocomplete"?: IcAriaAutocompleteTypes;
"ariaExpanded"?: string;
"ariaOwns"?: string;
/**
* If `true`, the form control will have input focus when the page loads.
*/
"autoFocus"?: boolean;
/**
* The automatic capitalisation of the text value as it is entered/edited by the user. Available options: "off", "none", "on", "sentences", "words", "characters".
*/
Expand All @@ -5480,6 +5476,10 @@ declare namespace LocalJSX {
* The state of autocorrection the browser can apply when the user is entering/editing the text value.
*/
"autocorrect"?: IcAutocorrectStates;
/**
* If `true`, the form control will have input focus when the page loads.
*/
"autofocus"?: boolean;
/**
* The amount of time, in milliseconds, to wait to trigger the `icChange` event after each keystroke.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/web-components/src/components/ic-menu/ic-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class Menu {
/**
* @internal If `true`, autofocus will be applied on selected item when menu is open.
*/
@Prop() autoFocusOnSelected: boolean = true;
@Prop() autofocusOnSelected: boolean = true;

/**
* If `true`, the menu will close when an option is selected.
Expand Down Expand Up @@ -267,7 +267,7 @@ export class Menu {
this.value &&
this.keyboardNav &&
inputValueInOptions &&
this.autoFocusOnSelected &&
this.autofocusOnSelected &&
!this.isSearchableSelect
) {
this.scrollToSelected(this.menu);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ export class SearchBar {
role={options.length > 0 ? "combobox" : undefined}
autocomplete={autocomplete}
autocapitalize={autocapitalize}
autoFocus={autofocus}
autofocus={autofocus}
spellcheck={spellcheck}
inputmode="search"
debounce={this.debounce}
Expand Down Expand Up @@ -944,7 +944,7 @@ export class SearchBar {
}}
activationType="manual"
anchorEl={this.anchorEl}
autoFocusOnSelected={false}
autofocusOnSelected={false}
searchMode={this.searchMode}
inputEl={this.inputEl}
inputLabel={label}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe("ic-search-bar search", () => {
page.win.addEventListener("menuOptionId", eventSpy);

const menu = page.root.shadowRoot.querySelector("ic-menu");
menu.autoFocusOnSelected = true;
menu.autofocusOnSelected = true;
await page.waitForChanges();

await page.rootInstance.handleKeyDown({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ import readme from "./readme.md";
html`<ic-text-field label="What is your favourite coffee?">
</ic-text-field>
<ic-text-field rows="6" label="What is your favourite coffee?">
</ic-text-field> `
</ic-text-field>`
}
</Story>
</Canvas>

### Autofocus

<Canvas>
<Story name="Autofocus" parameters={{ loki: { skip: true } }}>
{(args) =>
html`<ic-text-field autofocus label="What is your favourite coffee?">
</ic-text-field>`
}
</Story>
</Canvas>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class TextField {
private inheritedAttributes: { [k: string]: string } = {};
private inputEl: HTMLInputElement | HTMLTextAreaElement;
private hostMutationObserver: MutationObserver = null;
private interval: ReturnType<typeof setInterval>;

@Element() el: HTMLIcTextFieldElement;

Expand Down Expand Up @@ -111,7 +112,7 @@ export class TextField {
/**
* If `true`, the form control will have input focus when the page loads.
*/
@Prop() autoFocus = false;
@Prop() autofocus = false;

/**
* If `true`, the disabled state will be set.
Expand Down Expand Up @@ -374,8 +375,20 @@ export class TextField {
attributes: true,
childList: true,
});

if (this.autofocus && !this.disabled && !this.readonly) {
this.interval = setInterval(this.checkChildHydration, 50);
}
}

private checkChildHydration = () => {
const el = this.el.shadowRoot?.querySelector("ic-typography");
if (el && el.classList.contains("hydrated")) {
this.setFocus();
clearInterval(this.interval);
}
};

@Listen("keydown", {})
handleKeyDown(ev: KeyboardEvent): void {
this.icKeydown.emit({ event: ev });
Expand All @@ -387,7 +400,7 @@ export class TextField {
*/

@Method()
async setFocus(): Promise<void> {
setFocus() {
this.inputEl?.focus();
}

Expand Down Expand Up @@ -622,7 +635,6 @@ export class TextField {
aria-owns={this.ariaOwns}
autocomplete={this.autocomplete}
autocapitalize={this.autocapitalize}
autoFocus={this.autoFocus}
spellcheck={spellcheck}
inputmode={inputmode}
role={this.role}
Expand Down Expand Up @@ -653,7 +665,6 @@ export class TextField {
aria-describedby={describedBy}
aria-invalid={invalid}
autocapitalize={this.autocapitalize}
autoFocus={this.autoFocus}
spellcheck={spellcheck}
inputmode={inputmode}
maxlength={maxCharactersReached ? maxCharacters : null}
Expand Down
Loading
Loading