-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(CharacterCount): Add new component CharacterCount (#383)
- Loading branch information
Showing
6 changed files
with
675 additions
and
14 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
src/components/forms/CharacterCount/CharacterCount.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React from 'react' | ||
import { CharacterCount } from './CharacterCount' | ||
import { Form } from '../Form/Form' | ||
import { FormGroup } from '../FormGroup/FormGroup' | ||
import { Label } from '../Label/Label' | ||
|
||
export default { | ||
title: 'Forms/CharacterCount', | ||
parameters: { | ||
info: ` | ||
USWDS 2.0 Character count component | ||
Source: https://designsystem.digital.gov/components/form-controls/#character-count | ||
`, | ||
}, | ||
} | ||
const mockSubmit = (): void => { | ||
/* mock submit fn */ | ||
} | ||
|
||
export const textInput = (): React.ReactElement => ( | ||
<Form onSubmit={mockSubmit}> | ||
<FormGroup> | ||
<Label htmlFor="with-hint-input">Text input</Label> | ||
<span id="with-hint-input-hint" className="usa-hint"> | ||
This is an input with a character counter. | ||
</span> | ||
<CharacterCount | ||
id="with-hint-input" | ||
name="with-hint-input" | ||
aria-describedby="with-hint-input-info with-hint-input-hint" | ||
maxLength={25} | ||
/> | ||
</FormGroup> | ||
</Form> | ||
) | ||
|
||
export const textarea = (): React.ReactElement => ( | ||
<Form onSubmit={mockSubmit}> | ||
<FormGroup> | ||
<Label htmlFor="with-hint-textarea">Textarea</Label> | ||
<span id="with-hint-textarea-hint" className="usa-hint"> | ||
This is a textarea with a character counter. | ||
</span> | ||
<CharacterCount | ||
id="with-hint-textarea" | ||
name="with-hint-textarea" | ||
maxLength={50} | ||
isTextArea | ||
rows={2} | ||
aria-describedby="with-hint-textarea-info with-hint-textarea-hint" | ||
/> | ||
</FormGroup> | ||
</Form> | ||
) | ||
|
||
export const withCustomCharacterCount = (): React.ReactElement => { | ||
const customEmojiCharacterCount = (text: string): number => { | ||
const starCount = (text.match(/⭐️/g) || []).length | ||
return Array.from(text).length - starCount * 2 | ||
} | ||
|
||
const customEmojiMessage = (count: number, maxCount: number): string => { | ||
const remainingCount = maxCount - count | ||
if (remainingCount >= 0) return `${remainingCount} of ${maxCount} remain` | ||
} | ||
|
||
const twitterStyleMessage = (count: number, maxCount: number): string => { | ||
if (maxCount - count < 5) return `${maxCount - count}` | ||
} | ||
|
||
return ( | ||
<Form onSubmit={mockSubmit}> | ||
<FormGroup> | ||
<Label htmlFor="character-count-input">Custom - stars are free</Label> | ||
<CharacterCount | ||
id="character-count-input" | ||
name="characterCountInput" | ||
defaultValue="⭐️⭐️⭐️" | ||
getCharacterCount={customEmojiCharacterCount} | ||
getMessage={customEmojiMessage} | ||
maxLength={10} | ||
/> | ||
</FormGroup> | ||
<FormGroup> | ||
<Label htmlFor="character-count-textarea">Custom - Twitter style</Label> | ||
<CharacterCount | ||
id="character-count-textarea" | ||
name="characterCountTextArea" | ||
isTextArea | ||
defaultValue="In most cases, the text content of a Tweet can contain up to 280 characters or Unicode glyphs. The exact definition of which characters have weights greater than one character is found in the configuration file for the twitter-text Tweet parsing library. This is just an example using regex😇" | ||
maxLength={280} | ||
getMessage={twitterStyleMessage} | ||
/> | ||
</FormGroup> | ||
</Form> | ||
) | ||
} |
Oops, something went wrong.