-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update story and enhanch component file structure for gende…
…r id -tckt-365
- Loading branch information
kalasgarov
committed
Dec 6, 2024
1 parent
6cc6d47
commit 491d00c
Showing
3 changed files
with
127 additions
and
154 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
112 changes: 0 additions & 112 deletions
112
packages/design/src/Form/components/GenderId/GenderId.tsx
This file was deleted.
Oops, something went wrong.
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,3 +1,114 @@ | ||
import { GenderIdPattern } from './GenderId.js'; | ||
import React, { useEffect, useState } from 'react'; | ||
import classNames from 'classnames'; | ||
import { useFormContext } from 'react-hook-form'; | ||
import { type GenderIdProps } from '@atj/forms'; | ||
|
||
import { type PatternComponent } from '../../index.js'; | ||
|
||
const GenderIdPattern: PatternComponent<GenderIdProps> = ({ | ||
genderId, | ||
hint, | ||
label, | ||
required, | ||
error, | ||
value, | ||
preferNotToAnswerText, | ||
preferNotToAnswerChecked: initialPreferNotToAnswerChecked = false, | ||
}) => { | ||
const { register, setValue } = useFormContext(); | ||
const [preferNotToAnswerChecked, setPreferNotToAnswerChecked] = useState( | ||
initialPreferNotToAnswerChecked | ||
); | ||
|
||
const errorId = `input-error-message-${genderId}`; | ||
const hintId = `hint-${genderId}`; | ||
const preferNotToAnswerId = `prefer-not-to-answer-${genderId}`; | ||
|
||
useEffect(() => { | ||
if (preferNotToAnswerChecked) { | ||
setValue(genderId, preferNotToAnswerText, { shouldValidate: true }); | ||
} else { | ||
setValue(genderId, value, { shouldValidate: true }); | ||
} | ||
}, [ | ||
preferNotToAnswerChecked, | ||
setValue, | ||
genderId, | ||
preferNotToAnswerText, | ||
value, | ||
]); | ||
|
||
const handleCheckboxChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setPreferNotToAnswerChecked(event.target.checked); | ||
setValue(preferNotToAnswerId, event.target.checked); | ||
}; | ||
|
||
return ( | ||
<fieldset className="usa-fieldset"> | ||
<div className={classNames('usa-form-group margin-top-2')}> | ||
<label | ||
className={classNames('usa-label', { | ||
'usa-label--error': error, | ||
})} | ||
htmlFor={genderId} | ||
> | ||
{label || 'Gender identity'} | ||
{required && <span className="required-indicator">*</span>} | ||
</label> | ||
{hint && ( | ||
<div className="usa-hint" id={hintId}> | ||
{hint} | ||
</div> | ||
)} | ||
{error && ( | ||
<div className="usa-error-message" id={errorId} role="alert"> | ||
{error.message} | ||
</div> | ||
)} | ||
<input | ||
className={classNames('usa-input', { | ||
'usa-input--error': error, | ||
})} | ||
style={ | ||
preferNotToAnswerChecked | ||
? { | ||
backgroundColor: '#e9ecef', | ||
pointerEvents: 'none', | ||
opacity: 0.65, | ||
} | ||
: {} | ||
} | ||
id={genderId} | ||
type="text" | ||
defaultValue={value} | ||
{...register(genderId, { required })} | ||
aria-describedby={ | ||
`${hint ? `${hintId}` : ''}${error ? ` ${errorId}` : ''}`.trim() || | ||
undefined | ||
} | ||
/> | ||
{preferNotToAnswerText && ( | ||
<div className="usa-checkbox"> | ||
<input | ||
className="usa-checkbox__input" | ||
id={preferNotToAnswerId} | ||
type="checkbox" | ||
value="prefer-not-to-answer" | ||
checked={preferNotToAnswerChecked} | ||
{...register(preferNotToAnswerId)} | ||
onChange={handleCheckboxChange} | ||
/> | ||
<label | ||
className="usa-checkbox__label" | ||
htmlFor={preferNotToAnswerId} | ||
> | ||
{preferNotToAnswerText} | ||
</label> | ||
</div> | ||
)} | ||
</div> | ||
</fieldset> | ||
); | ||
}; | ||
|
||
export default GenderIdPattern; |