Skip to content

Commit

Permalink
Merge pull request #521 from newfold-labs/add/word-count-in-details
Browse files Browse the repository at this point in the history
Character count in Site Details
  • Loading branch information
arunshenoy99 authored Apr 8, 2024
2 parents 10d748b + 653bc99 commit 0854eb5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
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 TextInputSiteGen = ( {
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,6 +29,9 @@ const TextInputSiteGen = ( {
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 ) => {
Expand Down Expand Up @@ -90,6 +97,11 @@ const TextInputSiteGen = ( {
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

0 comments on commit 0854eb5

Please sign in to comment.