@defra/lis-infra-ui-services 0.26.0 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-infra-ui-services",
3
- "version": "0.26.0",
3
+ "version": "0.27.0",
4
4
  "description": "UI-Services shared definitions for the livestock taxonomy topology.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -27,6 +27,15 @@ $lis-search-form-input-size: 50px;
27
27
  border-right-width: 0;
28
28
  @include govuk-font($size: 19);
29
29
 
30
+ &.govuk-input--error {
31
+ border-color: govuk-functional-colour('error');
32
+
33
+ // GOV.UK switches an error input back to the normal border while focused.
34
+ &:focus {
35
+ border-color: govuk-functional-colour('input-border');
36
+ }
37
+ }
38
+
30
39
  &:focus {
31
40
  outline: $govuk-focus-width solid govuk-functional-colour('focus');
32
41
  outline-offset: 0;
@@ -171,19 +171,55 @@ describe('searchForm()', () => {
171
171
  )
172
172
  })
173
173
 
174
- test('renders maxlength on the search input when given', () => {
174
+ test('renders the error message with a visually hidden prefix', () => {
175
175
  // Act
176
- const html = render({ label: 'Search', maxlength: 50 })
176
+ const html = render({ label: 'Search', errorMessage: 'Too long' })
177
177
 
178
178
  // Assert
179
- expect(html).toContain('maxlength="50"')
179
+ expect(html).toContain(
180
+ '<p id="search-error" class="govuk-error-message"><span class="govuk-visually-hidden">Error:</span> Too long</p>'
181
+ )
182
+ expect(html.indexOf('govuk-error-message')).toBeLessThan(
183
+ html.indexOf('lis-search-form__wrapper')
184
+ )
180
185
  })
181
186
 
182
- test('renders no maxlength attribute when not given', () => {
187
+ test('adds the error classes to the form group and input', () => {
183
188
  // Act
184
- const html = render({ label: 'Search' })
189
+ const html = render({ label: 'Search', errorMessage: 'Too long' })
190
+
191
+ // Assert
192
+ expect(html).toContain('govuk-form-group govuk-form-group--error')
193
+ expect(html).toContain('lis-search-form__input govuk-input--error')
194
+ })
195
+
196
+ test('describes the input by the hint then the error when both are given', () => {
197
+ // Act
198
+ const html = render({
199
+ label: 'Search',
200
+ hint: 'Search by ear tag',
201
+ errorMessage: 'Too long'
202
+ })
203
+
204
+ // Assert
205
+ expect(html).toContain('aria-describedby="search-hint search-error"')
206
+ })
207
+
208
+ test('describes the input by the error alone when there is no hint', () => {
209
+ // Act
210
+ const html = render({ label: 'Search', errorMessage: 'Too long' })
211
+
212
+ // Assert
213
+ expect(html).toContain('aria-describedby="search-error"')
214
+ })
215
+
216
+ test('renders no error markup when no error message is given', () => {
217
+ // Act
218
+ const html = render({ label: 'Search', hint: 'Search by ear tag' })
185
219
 
186
220
  // Assert
187
- expect(html).not.toContain('maxlength')
221
+ expect(html).not.toContain('govuk-error-message')
222
+ expect(html).not.toContain('--error')
223
+ expect(html).not.toContain('search-error')
188
224
  })
189
225
  })
@@ -1,7 +1,7 @@
1
1
  {%- set id = params.id or params.name or "search" -%}
2
2
 
3
3
  <form method="GET" class="lis-search-form" role="search" aria-label="{{ params.label }}">
4
- <div class="govuk-form-group">
4
+ <div class="govuk-form-group{% if params.errorMessage %} govuk-form-group--error{% endif %}">
5
5
  <h2 class="govuk-label-wrapper">
6
6
  <label class="govuk-label govuk-label--m" for="{{ id }}">
7
7
  {{ params.label }}
@@ -15,15 +15,17 @@
15
15
  {%- for field in params.hiddenFields -%}
16
16
  <input type="hidden" name="{{ field.name }}" value="{{ field.value }}">
17
17
  {%- endfor -%}
18
+ {%- if params.errorMessage -%}
19
+ <p id="{{ id }}-error" class="govuk-error-message"><span class="govuk-visually-hidden">Error:</span> {{ params.errorMessage }}</p>
20
+ {%- endif %}
18
21
  <div class="lis-search-form__wrapper">
19
22
  <input
20
- class="govuk-input lis-search-form__input"
23
+ class="govuk-input lis-search-form__input{% if params.errorMessage %} govuk-input--error{% endif %}"
21
24
  type="search"
22
25
  id="{{ id }}"
23
26
  name="{{ params.name or 'search' }}"
24
27
  value="{{ params.value }}"
25
- {% if params.hint %}aria-describedby="{{ id }}-hint"{% endif %}
26
- {% if params.maxlength %}maxlength="{{ params.maxlength }}"{% endif %}
28
+ {% if params.hint or params.errorMessage %}aria-describedby="{% if params.hint %}{{ id }}-hint{% endif %}{% if params.hint and params.errorMessage %} {% endif %}{% if params.errorMessage %}{{ id }}-error{% endif %}"{% endif %}
27
29
  >
28
30
  <div class="lis-search-form__submit-wrapper">
29
31
  <button class="lis-search-form__submit" type="submit">