Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Character count in Site Details #521

Merged
merged 12 commits into from
Apr 8, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { __, sprintf } from '@wordpress/i18n';

const getContents = ( characterCount ) => {
return {
characterCount: sprintf(
/* translators: 1: characterCount */
__( '%d Characters left', 'wp-module-onboarding' ),
characterCount
),
};
};

export default getContents;
12 changes: 12 additions & 0 deletions src/OnboardingSPA/components/TextInput/TextInputSiteGen/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import classNames from 'classnames';
import { __ } from '@wordpress/i18n';
import { useRef, useEffect, useState, memo } from '@wordpress/element';
import getContents from './contents';

const TextInputSiteGen = ( {
hint,
Expand All @@ -16,6 +17,9 @@
const textareaRef = useRef( null );
const [ analysisScore, setAnalysisScore ] = useState( 0 );
const [ inputText, setInputText ] = useState( 'nfd-sg-input-box__field' );
const [ remainingCharacterCount, setRemainingCharacterCount ] =
useState( 0 );
const content = getContents( remainingCharacterCount );

useEffect( () => {
textareaRef.current.style.height = height;
Expand All @@ -25,7 +29,10 @@
setAnalysisScore( analysisResult );
setCustomerInputStrength( analysisResult );
setIsValidInput( analysisResult >= 2 );
setRemainingCharacterCount(
Math.max( 200 - ( customerInput?.length ?? 0 ), 0 )
);
}, [ customerInput ] );

Check warning on line 35 in src/OnboardingSPA/components/TextInput/TextInputSiteGen/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'height', 'setCustomerInputStrength', and 'setIsValidInput'. Either include them or remove the dependency array. If 'setCustomerInputStrength' changes too often, find the parent component that defines it and wrap that definition in useCallback

const calculateAnalysisScore = ( input ) => {
/* Number of Characters in the input
Expand Down Expand Up @@ -90,6 +97,11 @@
onChange={ ( e ) => onTextChange( e ) }
/>
</div>
{ remainingCharacterCount > 0 && (
<p className={ 'nfd-sg-input-box__count' }>
{ content.characterCount }
</p>
) }
<div className={ 'nfd-sg-input-box_bottom' }>
{ customerInput ? (
<div className={ 'nfd-sg-input-box__info' }>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ $selected-detail: #1de082;
}
}

&__count {
margin: 3px;
text-align: end;
font-size: 0.87rem;
animation: fadeIn 100ms ease-in;
color: var(--nfd-onboarding-primary);

@media (prefers-reduced-motion) {
animation: none !important;
}
}


&__hint {
font-weight: 300;
font-size: 0.87rem;
Expand Down
Loading