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

fix: add autocomplete attribute to input and textarea #321

Merged
merged 4 commits into from
Nov 21, 2024
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
16 changes: 16 additions & 0 deletions libs/core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ export namespace Components {
"width"?: number;
}
interface PdsInput {
/**
* Specifies if and how the browser provides `autocomplete` assistance for the field.
*/
"autocomplete": string;
/**
* A unique identifier used for the underlying component `id` attribute.
*/
Expand Down Expand Up @@ -752,6 +756,10 @@ export namespace Components {
"variant": 'primary' | 'availability' | 'filter';
}
interface PdsTextarea {
/**
* Specifies if and how the browser provides `autocomplete` assistance for the field.
*/
"autocomplete": string;
/**
* A unique identifier used for the underlying component `id` attribute.
*/
Expand Down Expand Up @@ -1641,6 +1649,10 @@ declare namespace LocalJSX {
"width"?: number;
}
interface PdsInput {
/**
* Specifies if and how the browser provides `autocomplete` assistance for the field.
*/
"autocomplete"?: string;
/**
* A unique identifier used for the underlying component `id` attribute.
*/
Expand Down Expand Up @@ -2055,6 +2067,10 @@ declare namespace LocalJSX {
"variant": 'primary' | 'availability' | 'filter';
}
interface PdsTextarea {
/**
* Specifies if and how the browser provides `autocomplete` assistance for the field.
*/
"autocomplete"?: string;
/**
* A unique identifier used for the underlying component `id` attribute.
*/
Expand Down
1 change: 1 addition & 0 deletions libs/core/src/components/pds-input/docs/pds-input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@ import { components } from '../../../../dist/docs.json';
## Additional Resources

[MDN Web Docs: Input](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input)
[MDN Web Docs: HTML autocomplete attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete)
7 changes: 7 additions & 0 deletions libs/core/src/components/pds-input/pds-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import { PdsLabel } from '../_internal/pds-label/pds-label';
shadow: true,
})
export class PdsInput {

/**
* Specifies if and how the browser provides `autocomplete` assistance for the field.
*/
@Prop() autocomplete: string;

/**
* A unique identifier used for the underlying component `id` attribute.
*/
Expand Down Expand Up @@ -93,6 +99,7 @@ export class PdsInput {
<input class="pds-input__field"
aria-describedby={assignDescription(this.componentId, this.invalid, this.helperMessage)}
aria-invalid={this.invalid ? "true" : undefined}
autocomplete={this.autocomplete}
disabled={this.disabled}
id={this.componentId}
name={this.name}
Expand Down
1 change: 1 addition & 0 deletions libs/core/src/components/pds-input/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

| Property | Attribute | Description | Type | Default |
| -------------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------ | --------- | ----------- |
| `autocomplete` | `autocomplete` | Specifies if and how the browser provides `autocomplete` assistance for the field. | `string` | `undefined` |
| `componentId` _(required)_ | `component-id` | A unique identifier used for the underlying component `id` attribute. | `string` | `undefined` |
| `disabled` | `disabled` | Indicates whether or not the input field is disabled. | `boolean` | `undefined` |
| `errorMessage` | `error-message` | Specifies the error message and provides an error-themed treatment to the field. | `string` | `undefined` |
Expand Down
10 changes: 10 additions & 0 deletions libs/core/src/components/pds-input/stories/pds-input.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { withActions } from '@storybook/addon-actions/decorator';
export default {
argTypes: extractArgTypes('pds-input'),
args: {
autocomplete: null,
disabled: false,
errorMessage: null,
helperMessage: null,
Expand All @@ -26,6 +27,7 @@ export default {
}

const BaseTemplate = (args) => html`<pds-input
autocomplete="${args.autocomplete}"
disabled="${args.disabled}"
error-message="${args.errorMessage}"
helper-message="${args.helperMessage}"
Expand Down Expand Up @@ -108,3 +110,11 @@ Invalid.args = {
type: 'email',
value: 'Frank Dux'
};

export const Autocomplete = BaseTemplate.bind({});
Autocomplete.args = {
componentId: 'pds-input-autocomplete',
label: 'First name',
type: 'text',
autocomplete: 'given-name',
};
19 changes: 19 additions & 0 deletions libs/core/src/components/pds-input/test/pds-input.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ describe('pds-input', () => {
`);
});

it('renders autocomplete attribute', async () => {
const { root } = await newSpecPage({
components: [PdsInput],
html: `<pds-input component-id="field-1" value="Frank Dux" autocomplete="given-name"></pds-input>`
});

expect(root).toEqualHtml(`
<pds-input component-id="field-1" value="Frank Dux" autocomplete="given-name">
<mock:shadow-root>
<div class="pds-input">
<label htmlFor="field-1">
</label>
<input id="field-1" class="pds-input__field" type="text" value="Frank Dux" autocomplete="given-name" />
</div>
</mock:shadow-root>
</pds-input>
`);
});

it('renders a helper message', async () => {
const { root } = await newSpecPage({
components: [PdsInput],
Expand Down
4 changes: 4 additions & 0 deletions libs/core/src/components/pds-textarea/docs/pds-textarea.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,7 @@ The rows property is used to pass a number of expected rows visible within the t
}}>
<pds-textarea name="Rows" label="Name" rows="4" component-id="textarea-rows"></pds-textarea>
</DocCanvas>

## Additional Resources

[MDN Web Docs: HTML autocomplete attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete)
6 changes: 6 additions & 0 deletions libs/core/src/components/pds-textarea/pds-textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { PdsLabel } from '../_internal/pds-label/pds-label';
export class PdsTextarea {
@Element() el: HTMLPdsTextareaElement;

/**
* Specifies if and how the browser provides `autocomplete` assistance for the field.
*/
@Prop() autocomplete: string;

/**
* A unique identifier used for the underlying component `id` attribute.
*/
Expand Down Expand Up @@ -113,6 +118,7 @@ export class PdsTextarea {
<textarea
aria-describedby={assignDescription(this.componentId, this.invalid, this.helperMessage)}
aria-invalid={this.invalid ? "true" : undefined}
autocomplete={this.autocomplete}
class={this.textareaClassNames()}
disabled={this.disabled}
id={this.componentId}
Expand Down
1 change: 1 addition & 0 deletions libs/core/src/components/pds-textarea/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

| Property | Attribute | Description | Type | Default |
| -------------------------- | ---------------- | --------------------------------------------------------------------------------------------------- | --------- | ------------------ |
| `autocomplete` | `autocomplete` | Specifies if and how the browser provides `autocomplete` assistance for the field. | `string` | `undefined` |
| `componentId` _(required)_ | `component-id` | A unique identifier used for the underlying component `id` attribute. | `string` | `undefined` |
| `disabled` | `disabled` | Indicates whether or not the textarea is disabled | `boolean` | `false` |
| `errorMessage` | `error-message` | Specifies the error text and provides an error-themed treatment to the field | `string` | `undefined` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { withActions } from '@storybook/addon-actions/decorator';

export default {
args: {
autocomplete: null,
componentId: null,
disabled: false,
errorMessage: null,
Expand All @@ -28,6 +29,7 @@ export default {
}

const BaseTemplate = (args) => html`<pds-textarea
autocomplete="${args.autocomplete}"
component-id="${args.componentId}"
disabled="${args.disabled}"
error-message="${args.errorMessage}"
Expand All @@ -47,46 +49,46 @@ const BaseTemplate = (args) => html`<pds-textarea
export const Default = BaseTemplate.bind({});
Default.args = {
componentId: 'pds-textarea-default-example',
label: 'Name',
label: 'Message',
name: 'Default',
};

export const Rows = BaseTemplate.bind({});
Rows.args = {
componentId: 'pds-textarea-rows-example',
label: 'Name',
label: 'Message',
name: 'Rows',
rows: 4,
};

export const Required = BaseTemplate.bind({});
Required.args = {
componentId: 'pds-textarea-required-example',
label: 'Name',
label: 'Message',
name: 'Required',
required: true,
};

export const Placeholder = BaseTemplate.bind({});
Placeholder.args = {
componentId: 'pds-textarea-placeholder-example',
label: 'Name',
label: 'Message',
name: 'Placeholder',
placeholder: 'Placeholder...'
placeholder: 'Enter a message...'
};

export const Disabled = BaseTemplate.bind({});
Disabled.args = {
componentId: 'pds-textarea-disabled-example',
disabled: true,
label: 'Name',
label: 'Message',
name: 'Disabled',
};

export const Readonly = BaseTemplate.bind({});
Readonly.args = {
componentId: 'pds-textarea-readonly-example',
label: 'Name',
label: 'Message',
name: 'Readonly',
readonly: true,
value: 'Readonly Value'
Expand All @@ -96,7 +98,7 @@ export const Message = BaseTemplate.bind({});
Message.args = {
componentId: 'pds-textarea-helper-example',
helperMessage: 'Helper message text',
label: 'Name',
label: 'Message',
name: 'Message',
};

Expand All @@ -105,7 +107,16 @@ Invalid.args = {
componentId: 'pds-textarea-error-example',
errorMessage: 'Error',
invalid: true,
label: 'Name',
label: 'Message',
name: 'Error',
required: true,
};

export const Autocomplete = BaseTemplate.bind({});
Autocomplete.args = {
componentId: 'pds-textarea-autocomplete',
label: 'Message',
name: 'message',
autocomplete: 'on',
placeholder: 'Enter a message...',
};
17 changes: 17 additions & 0 deletions libs/core/src/components/pds-textarea/test/pds-textarea.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ describe('pds-textarea', () => {
`);
});

it('renders autocomplete attribute when property is passed', async () => {
const {root} = await newSpecPage({
components: [PdsTextarea],
html: `<pds-textarea autocomplete="off"></pds-textarea>`,
});

expect(root).toEqualHtml(`
<pds-textarea autocomplete="off">
<mock:shadow-root>
<div class="pds-textarea">
<textarea class="pds-textarea__field" autocomplete="off"></textarea>
</div>
</mock:shadow-root>
</pds-textarea>
`);
});

it('renders a helper and error message and assigns aria-description to the input', async () => {
const {root} = await newSpecPage({
components: [PdsTextarea],
Expand Down
Loading