Skip to content

Commit b6b2fdc

Browse files
Update character count for NHS.UK frontend v10.0.0
1 parent 8dd5d75 commit b6b2fdc

File tree

6 files changed

+24
-71
lines changed

6 files changed

+24
-71
lines changed

src/__tests__/index.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ describe('Index', () => {
1515
'Button',
1616
'Card',
1717
'CharacterCount',
18-
'CharacterCountType',
1918
'Checkboxes',
2019
'ChevronRightCircleIcon',
2120
'Clearfix',

src/components/form-elements/character-count/CharacterCount.tsx

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
'use client';
2-
import React, { FC, useEffect, useRef, useState } from 'react';
2+
import React, { FC, HTMLProps, useEffect, useRef, useState } from 'react';
33
import { CharacterCount } from 'nhsuk-frontend';
4-
import { HTMLAttributesWithData } from '@util/types/NHSUKTypes';
54

6-
export enum CharacterCountType {
7-
Characters,
8-
Words,
9-
}
10-
11-
type CharacterCountProps = React.HTMLAttributes<HTMLDivElement> & {
12-
children: React.ReactNode;
13-
maxLength: number;
14-
countType: CharacterCountType;
5+
type CharacterCountProps = HTMLProps<HTMLDivElement> & {
6+
maxLength?: number;
7+
maxWords?: number;
158
textAreaId: string;
16-
thresholdPercent?: number;
9+
threshold?: number;
1710
};
1811

1912
const CharacterCountComponent: FC<CharacterCountProps> = ({
2013
children,
2114
maxLength,
22-
countType,
15+
maxWords,
2316
textAreaId,
24-
thresholdPercent,
17+
threshold,
2518
...rest
2619
}) => {
2720
const moduleRef = useRef<HTMLDivElement>(null);
@@ -35,21 +28,15 @@ const CharacterCountComponent: FC<CharacterCountProps> = ({
3528
setInstance(new CharacterCount(moduleRef.current));
3629
}, [moduleRef, instance]);
3730

38-
const characterCountProps: HTMLAttributesWithData<HTMLDivElement> =
39-
countType === CharacterCountType.Characters
40-
? { ...rest, ['data-maxlength']: maxLength }
41-
: { ...rest, ['data-maxwords']: maxLength };
42-
43-
if (thresholdPercent) {
44-
characterCountProps['data-threshold'] = thresholdPercent;
45-
}
46-
4731
return (
4832
<div
4933
className="nhsuk-character-count"
5034
data-module="nhsuk-character-count"
35+
data-maxlength={maxLength}
36+
data-maxwords={maxWords}
37+
data-threshold={threshold}
5138
ref={moduleRef}
52-
{...characterCountProps}
39+
{...rest}
5340
>
5441
<div className="nhsuk-form-group">{children}</div>
5542

src/components/form-elements/character-count/__tests__/CharacterCount.test.tsx

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { render } from '@testing-library/react';
3-
import CharacterCountComponent, { CharacterCountType } from '../CharacterCount';
3+
import CharacterCountComponent from '../CharacterCount';
44
import Label from '@components/form-elements/label/Label';
55
import HintText from '@components/form-elements/hint-text/HintText';
66
import Textarea from '@components/form-elements/textarea/Textarea';
@@ -24,11 +24,7 @@ describe('Character Count', () => {
2424

2525
it('Matches snapshot', () => {
2626
const { container } = render(
27-
<CharacterCountComponent
28-
maxLength={200}
29-
countType={CharacterCountType.Characters}
30-
textAreaId="more-detail"
31-
>
27+
<CharacterCountComponent maxLength={200} textAreaId="more-detail">
3228
{children}
3329
</CharacterCountComponent>,
3430
);
@@ -38,11 +34,7 @@ describe('Character Count', () => {
3834

3935
it('Sets the data-maxlength attribute when counting characters', () => {
4036
const { container } = render(
41-
<CharacterCountComponent
42-
maxLength={200}
43-
countType={CharacterCountType.Characters}
44-
textAreaId="more-detail"
45-
>
37+
<CharacterCountComponent maxLength={200} textAreaId="more-detail">
4638
{children}
4739
</CharacterCountComponent>,
4840
);
@@ -60,11 +52,7 @@ describe('Character Count', () => {
6052

6153
it('Sets the data-maxwords attribute when counting words', () => {
6254
const { container } = render(
63-
<CharacterCountComponent
64-
maxLength={200}
65-
countType={CharacterCountType.Words}
66-
textAreaId="more-detail"
67-
>
55+
<CharacterCountComponent maxWords={200} textAreaId="more-detail">
6856
{children}
6957
</CharacterCountComponent>,
7058
);
@@ -82,12 +70,7 @@ describe('Character Count', () => {
8270

8371
it('Sets the data-threshold attribute when threshold is specified', () => {
8472
const { container } = render(
85-
<CharacterCountComponent
86-
maxLength={200}
87-
countType={CharacterCountType.Characters}
88-
thresholdPercent={50}
89-
textAreaId="more-detail"
90-
>
73+
<CharacterCountComponent maxLength={200} threshold={50} textAreaId="more-detail">
9174
{children}
9275
</CharacterCountComponent>,
9376
);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default, CharacterCountType } from './CharacterCount';
1+
export { default } from './CharacterCount';

src/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ export { default as BackLink } from './components/navigation/back-link';
33
export { default as Breadcrumb } from './components/navigation/breadcrumb';
44
export { default as Button } from './components/form-elements/button';
55
export { default as Card } from './components/navigation/card';
6-
export {
7-
default as CharacterCount,
8-
CharacterCountType,
9-
} from './components/form-elements/character-count';
6+
export { default as CharacterCount } from './components/form-elements/character-count';
107
export { default as Checkboxes } from './components/form-elements/checkboxes';
118
export { default as ContentsList } from './components/navigation/contents-list';
129
export { default as DateInput } from './components/form-elements/date-input';

stories/Form Elements/CharacterCount.stories.tsx

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { CharacterCount, CharacterCountType, HintText, Label, Textarea } from '../../src';
2+
import { CharacterCount, HintText, Label, Textarea } from '../../src';
33
import { Meta, StoryObj } from '@storybook/react';
44

55
/**
@@ -20,11 +20,7 @@ type Story = StoryObj<typeof CharacterCount>;
2020

2121
export const Standard: Story = {
2222
render: () => (
23-
<CharacterCount
24-
maxLength={200}
25-
countType={CharacterCountType.Characters}
26-
textAreaId="more-detail"
27-
>
23+
<CharacterCount maxLength={200} textAreaId="more-detail">
2824
<Label htmlFor="more-detail">Can you provide more detail?</Label>
2925
<HintText id="more-detail-hint">
3026
Do not include personal information like your name, date of birth or NHS number.
@@ -47,11 +43,7 @@ export const Standard: Story = {
4743
*/
4844
export const WordCountLimit: Story = {
4945
render: () => (
50-
<CharacterCount
51-
maxLength={150}
52-
countType={CharacterCountType.Words}
53-
textAreaId="job-description-detail"
54-
>
46+
<CharacterCount maxWords={150} textAreaId="job-description-detail">
5547
<Label htmlFor="job-description-detail" size="l">
5648
Enter a job description
5749
</Label>
@@ -68,16 +60,11 @@ export const WordCountLimit: Story = {
6860
/**
6961
* If the limit is much higher than most users are likely to reach, you can choose to only display the message after a user has entered a certain amount.
7062
*
71-
* Use the `thresholdPercent` prop to only show the count message when users have reached that percentage of the limit.
63+
* Use the `threshold` prop to only show the count message when users have reached that percentage of the limit.
7264
*/
73-
export const MessageThresholdPercentage: Story = {
65+
export const MessageThreshold: Story = {
7466
render: () => (
75-
<CharacterCount
76-
maxLength={112}
77-
countType={CharacterCountType.Characters}
78-
textAreaId="threshold"
79-
thresholdPercent={75}
80-
>
67+
<CharacterCount maxLength={112} textAreaId="threshold" threshold={75}>
8168
<Label htmlFor="threshold">Can you provide more detail?</Label>
8269
<Textarea
8370
id="threshold"

0 commit comments

Comments
 (0)