Skip to content

Commit

Permalink
Mj.6884 input max min length (#1353)
Browse files Browse the repository at this point in the history
* feat(input): add min and max length attributes

* chore: build

* docs: changeset

* docs(input): update story
  • Loading branch information
micahjones13 authored Oct 18, 2024
1 parent 0944b9e commit 44237a2
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 12 deletions.
11 changes: 11 additions & 0 deletions .changeset/strong-ravens-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@astrouxds/angular": minor
"@astrouxds/astro-web-components": minor
"angular-workspace": minor
"astro-angular": minor
"astro-react": minor
"astro-vue": minor
"@astrouxds/react": minor
---

Added minlength and maxlength to rux-input
16 changes: 16 additions & 0 deletions packages/web-components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19414,10 +19414,18 @@ export namespace Components {
* The input max attribute
*/
"max"?: string;
/**
* The input maxlength attribute
*/
"maxlength"?: string;
/**
* The input min attribute
*/
"min"?: string;
/**
* The input minlength attribute
*/
"minlength"?: string;
/**
* The input name
*/
Expand Down Expand Up @@ -54952,10 +54960,18 @@ declare namespace LocalJSX {
* The input max attribute
*/
"max"?: string;
/**
* The input maxlength attribute
*/
"maxlength"?: string;
/**
* The input min attribute
*/
"min"?: string;
/**
* The input minlength attribute
*/
"minlength"?: string;
/**
* The input name
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/web-components/src/components/rux-input/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
| `invalid` | `invalid` | Presentational only. Renders the Input Field as invalid. | `boolean` | `false` |
| `label` | `label` | The input label text. For HTML content, use the `label` slot instead. | `string \| undefined` | `undefined` |
| `max` | `max` | The input max attribute | `string \| undefined` | `undefined` |
| `maxlength` | `maxlength` | The input maxlength attribute | `string \| undefined` | `undefined` |
| `min` | `min` | The input min attribute | `string \| undefined` | `undefined` |
| `minlength` | `minlength` | The input minlength attribute | `string \| undefined` | `undefined` |
| `name` | `name` | The input name | `string` | `''` |
| `placeholder` | `placeholder` | The input placeholder text | `string \| undefined` | `undefined` |
| `readonly` | `readonly` | The inputs readonly attribute | `boolean` | `false` |
Expand Down
27 changes: 21 additions & 6 deletions packages/web-components/src/components/rux-input/rux-input.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {
Prop,
Host,
Component,
Element,
Event,
EventEmitter,
h,
Element,
Host,
Method,
Prop,
State,
Watch,
Method,
h,
} from '@stencil/core'
import { FormFieldInterface } from '../../common/interfaces.module'
import { hasSlot, renderHiddenInput } from '../../utils/utils'

import { FormFieldInterface } from '../../common/interfaces.module'

let id = 0

/**
Expand Down Expand Up @@ -113,6 +114,16 @@ export class RuxInput implements FormFieldInterface {
*/
@Prop() max?: string

/**
* The input maxlength attribute
*/
@Prop() maxlength?: string

/**
* The input minlength attribute
*/
@Prop() minlength?: string

/**
* Disables the button via HTML disabled attribute. Button takes on a distinct visual state. Cursor uses the not-allowed system replacement and all keyboard and mouse events are ignored.
*/
Expand Down Expand Up @@ -288,6 +299,8 @@ export class RuxInput implements FormFieldInterface {
togglePassword,
isPasswordVisible,
autocomplete,
minlength,
maxlength,
} = this

renderHiddenInput(true, el, name, value, disabled)
Expand Down Expand Up @@ -357,6 +370,8 @@ export class RuxInput implements FormFieldInterface {
step={step}
min={min}
max={max}
minlength={minlength}
maxlength={maxlength}
value={value}
class="native-input"
id={inputId}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, expect } from '../../../../tests/utils/_astro-fixtures'
import { expect, test } from '../../../../tests/utils/_astro-fixtures'

test.describe('Input with form', () => {
const testString = 'Hello World'
Expand Down Expand Up @@ -145,6 +145,7 @@ test.describe('Input with form', () => {
</form>
<ul id="log"></ul>
</div>
</body>
`

Expand Down Expand Up @@ -427,3 +428,47 @@ test.describe('Input', () => {
expect(isFocused).toBeTruthy()
})
})

test.describe('Min and max length', () => {
test('minlength works', async ({ page }) => {
const template = `
<div style="margin: auto; width: 300px;">
<rux-input id="minmaxlength" minlength="1" maxlength="3"></rux-input>
</div>`
await page.setContent(template)

// Access the shadow DOM input inside the <rux-input> component
const input = page.locator('#minmaxlength').locator('#rux-input-1')

// Type into the input
await input.type('12')

// Check validity
await input.evaluate((el: HTMLInputElement) => el.reportValidity())
const validityState = await input.evaluate(
(el: HTMLInputElement) => el.validity.tooShort
)

// Assert the validity
expect(validityState).toBe(false)
})
test('maxlength works', async ({ page }) => {
const template = `
<div style="margin: auto; width: 300px;">
<rux-input id="minmaxlength" minlength="1" maxlength="3"></rux-input>
</div>`
await page.setContent(template)

// Access the shadow DOM input inside the <rux-input> component
const input = page.locator('#minmaxlength').locator('#rux-input-1')

// Type into the input
await input.type('1234')

//test that only 123 were typed, since the maxlength is 3
const inputValue = await input.evaluate(
(el: HTMLInputElement) => el.value
)
expect(inputValue.length).toBe(3) // Input should only contain '123'
})
})
88 changes: 83 additions & 5 deletions packages/web-components/src/stories/input.stories.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { extractArgTypes } from '@astrouxds/storybook-addon-docs-stencil';
import { html } from 'lit-html';
import { extractArgTypes } from '@astrouxds/storybook-addon-docs-stencil'
import { html } from 'lit-html'

const Base = (args) => {
return html`
return html`
<rux-input
label="${args.label}"
.disabled="${args.disabled}"
Expand All @@ -21,6 +21,8 @@ return html`
autocomplete="${args.autocomplete}"
?spellcheck="${args.spellcheck}"
size="${args.size}"
.minlength="${args.minlength}"
.maxlength="${args.maxlength}"
></rux-input>
`
}
Expand All @@ -45,6 +47,8 @@ const Icons = (args) => {
?readonly="${args.readonly}"
?autocomplete="${args.autocomplete}"
?spellcheck="${args.spellcheck}"
.minlength="${args.minlength}"
.maxlength="${args.maxlength}"
>
<rux-icon
slot="prefix"
Expand All @@ -71,6 +75,8 @@ const Icons = (args) => {
?readonly="${args.readonly}"
?autocomplete="${args.autocomplete}"
?spellcheck="${args.spellcheck}"
.minlength="${args.minlength}"
.maxlength="${args.maxlength}"
>
<rux-icon
slot="suffix"
Expand All @@ -97,6 +103,8 @@ const Icons = (args) => {
?readonly="${args.readonly}"
?autocomplete="${args.autocomplete}"
?spellcheck="${args.spellcheck}"
.minlength="${args.minlength}"
.maxlength="${args.maxlength}"
>
<rux-icon
slot="prefix"
Expand Down Expand Up @@ -132,6 +140,8 @@ const Sizes = (args) => {
?readonly="${args.readonly}"
?autocomplete="${args.autocomplete}"
?spellcheck="${args.spellcheck}"
.minlength="${args.minlength}"
.maxlength="${args.maxlength}"
></rux-input>
<br />
<rux-input
Expand All @@ -152,6 +162,8 @@ const Sizes = (args) => {
?readonly="${args.readonly}"
?autocomplete="${args.autocomplete}"
?spellcheck="${args.spellcheck}"
.minlength="${args.minlength}"
.maxlength="${args.maxlength}"
></rux-input>
<br />
<rux-input
Expand All @@ -172,6 +184,8 @@ const Sizes = (args) => {
?readonly="${args.readonly}"
?autocomplete="${args.autocomplete}"
?spellcheck="${args.spellcheck}"
.minlength="${args.minlength}"
.maxlength="${args.maxlength}"
></rux-input>
`
}
Expand Down Expand Up @@ -269,6 +283,8 @@ const HorizontalLabel = (args) => {
?autocomplete="${args.autocomplete}"
?spellcheck="${args.spellcheck}"
size="${args.size}"
.minlength="${args.minlength}"
.maxlength="${args.maxlength}"
></rux-input>
`
}
Expand Down Expand Up @@ -312,6 +328,8 @@ export const Default = {
spellcheck: false,
step: '',
value: '',
minlength: '',
maxlength: '',
},

argTypes: {
Expand Down Expand Up @@ -343,6 +361,8 @@ export const Disabled = {
spellcheck: false,
step: '',
value: '',
minlength: '',
maxlength: '',
},

argTypes: {
Expand All @@ -357,10 +377,25 @@ export const Required = {
name: 'Required',

args: {
label: 'Required input',
required: true,
type: 'text',
label: 'Required Input',
autocomplete: '',
disabled: false,
errorText: '',
helpText: '',
invalid: false,
max: '',
min: '',
name: '',
placeholder: '',
readonly: false,
required: true,
size: 'medium',
spellcheck: false,
step: '',
value: '',
minlength: '',
maxlength: '',
},
}

Expand All @@ -370,7 +405,24 @@ export const WithIcons = {

args: {
type: 'text',
label: '',
autocomplete: '',
disabled: false,
errorText: '',
helpText: '',
invalid: false,
max: '',
min: '',
name: '',
placeholder: '',
readonly: false,
required: false,
size: 'medium',
spellcheck: false,
step: '',
value: '',
minlength: '',
maxlength: '',
},
}

Expand Down Expand Up @@ -484,6 +536,16 @@ export const WithSizes = {
disable: true,
},
},
minlength: {
table: {
disable: true,
},
},
maxlength: {
table: {
disable: true,
},
},
},
}

Expand All @@ -509,6 +571,8 @@ export const HelpText = {
spellcheck: false,
step: '',
value: '',
minlength: '',
maxlength: '',
},

argTypes: {
Expand Down Expand Up @@ -540,6 +604,8 @@ export const Invalid = {
spellcheck: false,
step: '',
value: '',
minlength: '',
maxlength: '',
},

argTypes: {
Expand Down Expand Up @@ -661,6 +727,16 @@ export const WithTypes = {
disable: true,
},
},
minlength: {
table: {
disable: true,
},
},
maxlength: {
table: {
disable: true,
},
},
},
}

Expand Down Expand Up @@ -691,6 +767,8 @@ export const WithHorizontalLabel = {
spellcheck: false,
step: '',
value: '',
minlength: '',
maxlength: '',
},

argTypes: {
Expand Down

0 comments on commit 44237a2

Please sign in to comment.