-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
polls: refactor PollQuestion.jsx and add TextareaWithCounter.jsx
- Loading branch information
Showing
4 changed files
with
76 additions
and
60 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import React from 'react' | ||
import django from 'django' | ||
|
||
export const CharCounter = (props) => { | ||
const current = props.value.length | ||
const translated = { | ||
characters: django.gettext('characters') | ||
} | ||
|
||
export const CharCounter = ({ value, max, id }) => { | ||
const current = value.length | ||
|
||
return ( | ||
<span>{current}/{props.max}</span> | ||
<span className="char-counter" id={id}> | ||
{current}/{max} {translated.characters} | ||
</span> | ||
) | ||
} |
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
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
38 changes: 38 additions & 0 deletions
38
adhocracy4/polls/static/PollDetail/TextareaWithCounter.jsx
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,38 @@ | ||
import React from 'react' | ||
import django from 'django' | ||
import { CharCounter } from './CharCounter' | ||
import FormFieldError from '../../../static/FormFieldError' | ||
|
||
const translated = { | ||
specify: django.gettext('Please specify (required)') | ||
} | ||
|
||
export const TextareaWithCounter = ({ value, onChange, disabled, maxLength, error, id, name }) => { | ||
const handleInputChange = (e) => { | ||
onChange(e) | ||
e.target.style.height = 'auto' | ||
e.target.style.height = e.target.scrollHeight + 'px' | ||
} | ||
|
||
return ( | ||
<div className="textarea-with-counter"> | ||
<label htmlFor={'id_choice-' + id + '-other'} className="textarea-with-counter__label"> | ||
{translated.specify} | ||
</label> | ||
<textarea | ||
className="textarea-with-counter__textarea" | ||
name={name} | ||
id={'id_choice-' + id + '-other'} | ||
value={value} | ||
onChange={handleInputChange} | ||
disabled={disabled} | ||
maxLength={maxLength} | ||
aria-invalid={error ? 'true' : 'false'} | ||
aria-describedby={'id_error-' + id + ' id_char-count-' + id} | ||
rows={3} | ||
/> | ||
<CharCounter value={value} max={maxLength} id={'id_char-count-' + id} /> | ||
{error && <FormFieldError id={'id_error-' + id} error={error} field={id} />} | ||
</div> | ||
) | ||
} |