Skip to content

Commit

Permalink
feat(input): Add component
Browse files Browse the repository at this point in the history
  • Loading branch information
adamledwards committed Dec 7, 2022
1 parent a063e11 commit c615759
Show file tree
Hide file tree
Showing 63 changed files with 386 additions and 1 deletion.
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.
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.
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.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions e2e/tests/components/input/default.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, test } from '../../../fixtures'

test.describe('input, default', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/components/input/default')
})

test('displays the heading', async ({ page }) => {
await expect(
page.getByRole('heading'),
).toHaveText('What is the name of the event?')
})

test('displays the input', async ({ page }) => {
await expect(page.getByRole('textbox')).toBeVisible()
})
})
17 changes: 17 additions & 0 deletions e2e/tests/components/input/with-error.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, test } from '../../../fixtures'

test.describe('input, with error', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/components/input/with-error')
})

test('displays the heading', async ({ page }) => {
await expect(
page.getByRole('heading'),
).toHaveText('What is the name of the event?')
})

test('displays the error message', async ({ page }) => {
await expect(page.getByRole('textbox')).toHaveDescription('Error: Enter an event name')
})
})
13 changes: 13 additions & 0 deletions e2e/tests/components/input/with-hint.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect, test } from '../../../fixtures'

test.describe('input, with hint', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/components/input/with-hint')
})

test('displays the hint text', async ({ page }) => {
await expect(
page.getByText('The name you’ll use on promotional material'),
).toBeVisible()
})
})
17 changes: 17 additions & 0 deletions e2e/tests/components/input/with-prefix-and-suffix.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, test } from '../../../fixtures'

test.describe('input, with prefix and suffix', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/components/input/with-prefix-and-suffix')
})

test('displays prefix and suffix', async ({ page }) => {
await expect(
page.getByText('£'),
).toBeVisible()

await expect(
page.getByText('per item', { exact: true }),
).toBeVisible()
})
})
1 change: 1 addition & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare global {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<R, T> {
toHaveNoViolations(disabledRules?: string[]): Promise<void>
toHaveDescription(text: string): Promise<void>
}
}
}
40 changes: 39 additions & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AxeBuilder from '@axe-core/playwright'
import { devices, expect, Page, PlaywrightTestConfig } from '@playwright/test'
import { devices, Expect, expect, Locator, Page, PlaywrightTestConfig } from '@playwright/test'

expect.extend({
toHaveNoViolations: async (
Expand Down Expand Up @@ -30,6 +30,44 @@ expect.extend({
pass: false,
}
},
async toHaveDescription(this: ReturnType<Expect['getState']>, locator: Locator, expected: string) {
const page = locator.page()
const describedBy = await locator.getAttribute('aria-describedby')
const describedByIds = `#${describedBy?.split(' ').join(', #')}`

const result = page.locator(describedByIds, {
hasText: expected,
})

if (await result.isVisible()) {
return {
message: () => `Expected aria-describedby not to have text content ${this.utils.printReceived(expected)}`,
pass: true,
}
}

const receivedResult = page.locator(describedByIds)
const receivedTextPromises: Promise<string | null>[] = []

const count = await receivedResult.count()
for (let i = 0; i < count; i += 1) {
receivedTextPromises.push(receivedResult.nth(i).textContent())
}

const receivedText = await Promise.all(receivedTextPromises)

return {
message: () => `
${
this.utils.matcherHint('toHaveDescription', 'HTMLElement', expected, {
isNot: this.isNot,
promise: this.promise,
})
}\n
Received: ${this.utils.printReceived(receivedText.map((text) => text?.trim()).join('\n'))}`,
pass: false,
}
},
})

const config: PlaywrightTestConfig = {
Expand Down
11 changes: 11 additions & 0 deletions src/nunjucks/moduk/components/input/__examples__/default.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% from "moduk/components/input/macro.njk" import modukInput %}

{{ modukInput({
label: {
text: "What is the name of the event?",
classes: "govuk-label--l",
isPageHeading: true
},
id: "default",
name: "event-name"
}) }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% from "moduk/components/input/macro.njk" import modukInput %}

{{ modukInput({
label: {
text: "What is the name of the event?"
},
id: "with-not-as-page-heading",
name: "event-name"
}) }}
17 changes: 17 additions & 0 deletions src/nunjucks/moduk/components/input/__examples__/with-error.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% from "moduk/components/input/macro.njk" import modukInput %}

{{ modukInput({
label: {
text: "What is the name of the event?",
classes: "govuk-label--l",
isPageHeading: true
},
id: "with-error",
name: "event-name",
hint: {
text: "The name you’ll use on promotional material"
},
errorMessage: {
text: "Enter an event name"
}
}) }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{% from "moduk/components/input/macro.njk" import modukInput %}

<div>
{{ modukInput({
label: {
text: "20 character width"
},
classes: "govuk-input--width-20",
id: "width-20",
name: "width-20"
}) }}

{{ modukInput({
label: {
text: "10 character width"
},
classes: "govuk-input--width-10",
id: "width-10",
name: "width-10"
}) }}

{{ modukInput({
label: {
text: "5 character width"
},
classes: "govuk-input--width-5",
id: "width-5",
name: "width-5"
}) }}

{{ modukInput({
label: {
text: "4 character width"
},
classes: "govuk-input--width-4",
id: "width-4",
name: "width-4"
}) }}

{{ modukInput({
label: {
text: "3 character width"
},
classes: "govuk-input--width-3",
id: "width-3",
name: "width-3"
}) }}

{{ modukInput({
label: {
text: "2 character width"
},
classes: "govuk-input--width-2",
id: "width-2",
name: "width-2"
}) }}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{% from "moduk/components/input/macro.njk" import modukInput %}

<div>
{{ modukInput({
label: {
text: "Full width"
},
classes: "govuk-!-width-full",
id: "full",
name: "full"
}) }}

{{ modukInput({
label: {
text: "Three-quarters width"
},
classes: "govuk-!-width-three-quarters",
id: "three-quarters",
name: "three-quarters"
}) }}

{{ modukInput({
label: {
text: "Two-thirds width"
},
classes: "govuk-!-width-two-thirds",
id: "two-thirds",
name: "two-thirds"
}) }}

{{ modukInput({
label: {
text: "One-half width"
},
classes: "govuk-!-width-one-half",
id: "one-half",
name: "one-half"
}) }}

{{ modukInput({
label: {
text: "One-third width"
},
classes: "govuk-!-width-one-third",
id: "one-third",
name: "one-third"
}) }}

{{ modukInput({
label: {
text: "One-quarter width"
},
classes: "govuk-!-width-one-quarter",
id: "one-quarter",
name: "one-quarter"
}) }}
</div>
14 changes: 14 additions & 0 deletions src/nunjucks/moduk/components/input/__examples__/with-hint.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% from "moduk/components/input/macro.njk" import modukInput %}

{{ modukInput({
label: {
text: "What is the name of the event?",
classes: "govuk-label--l",
isPageHeading: true
},
hint: {
text: "The name you’ll use on promotional material"
},
id: "with-hint",
name: "event-name"
}) }}
17 changes: 17 additions & 0 deletions src/nunjucks/moduk/components/input/__examples__/with-numeric.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% from "moduk/components/input/macro.njk" import modukInput %}

{{ modukInput({
label: {
text: "What is your account number?",
classes: "govuk-label--l",
isPageHeading: true
},
classes: "govuk-input--width-10",
hint: {
text: "Must be between 6 and 8 digits long"
},
id: "with-numeric",
name: "account-number",
inputmode: "numeric",
spellcheck: false
}) }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% from "moduk/components/input/macro.njk" import modukInput %}

{{ modukInput({
id: "with-prefix-and-suffix-error",
name: "cost-per-item",
label: {
text: "What is the cost per item, in pounds?",
classes: "govuk-label--l",
isPageHeading: true
},
prefix: {
text: "£"
},
suffix: {
text: "per item"
},
errorMessage: {
text: "Enter a cost per item, in pounds"
},
classes: "govuk-input--width-5",
spellcheck: false
}) }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% from "moduk/components/input/macro.njk" import modukInput %}

{{ modukInput({
id: "with-prefix-and-suffix",
name: "cost-per-item",
label: {
text: "What is the cost per item, in pounds?",
classes: "govuk-label--l",
isPageHeading: true
},
prefix: {
text: "£"
},
suffix: {
text: "per item"
},
classes: "govuk-input--width-5",
spellcheck: false
}) }}
16 changes: 16 additions & 0 deletions src/nunjucks/moduk/components/input/__examples__/with-prefix.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% from "moduk/components/input/macro.njk" import modukInput %}

{{ modukInput({
id: "with-prefix",
name: "cost",
label: {
text: "What is the cost in pounds?",
classes: "govuk-label--l",
isPageHeading: true
},
prefix: {
text: "£"
},
classes: "govuk-input--width-5",
spellcheck: false
}) }}
16 changes: 16 additions & 0 deletions src/nunjucks/moduk/components/input/__examples__/with-suffix.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% from "moduk/components/input/macro.njk" import modukInput %}

{{ modukInput({
id: "with-suffix",
name: "weight",
label: {
text: "What is the weight in kilograms?",
classes: "govuk-label--l",
isPageHeading: true
},
suffix: {
text: "kg"
},
classes: "govuk-input--width-5",
spellcheck: false
}) }}
Loading

0 comments on commit c615759

Please sign in to comment.