Skip to content

Commit

Permalink
feat: show inline error for password limit & uncheck password protect…
Browse files Browse the repository at this point in the history
… option when password is empty
  • Loading branch information
sarthaknagoshe2002 committed Nov 15, 2024
1 parent 4d0245c commit fe82fdb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/editor/src/components/post-status/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export default function PostStatus() {
);
const { editEntityRecord } = useDispatch( coreStore );
const [ popoverAnchor, setPopoverAnchor ] = useState( null );
const [ error, setError ] = useState( '' );
// Memoize popoverProps to avoid returning a new object every time.
const popoverProps = useMemo(
() => ( {
Expand Down Expand Up @@ -163,6 +164,21 @@ export default function PostStatus() {
} );
};

const handlePassword = ( { password: newPassword = password } ) => {
if ( newPassword.length > 255 ) {
setError( __( 'Password cannot exceed 255 characters.' ) );
} else {
setError( '' );
updatePost( { password: newPassword } );
}
};

const handleFocusOut = () => {
if ( password.trim() === '' ) {
handleTogglePassword( false );
}
};

return (
<PostPanelRow label={ __( 'Status' ) } ref={ setPopoverAnchor }>
{ canEdit ? (
Expand All @@ -171,6 +187,7 @@ export default function PostStatus() {
contentClassName="editor-change-status__content"
popoverProps={ popoverProps }
focusOnMount
onToggle={ handleFocusOut }
renderToggle={ ( { onToggle, isOpen } ) => (
<Button
className="editor-post-status__toggle"
Expand Down Expand Up @@ -244,7 +261,7 @@ export default function PostStatus() {
'Password'
) }
onChange={ ( value ) =>
updatePost( {
handlePassword( {
password: value,
} )
}
Expand All @@ -256,8 +273,12 @@ export default function PostStatus() {
id={ passwordInputId }
__next40pxDefaultSize
__nextHasNoMarginBottom
maxLength={ 255 }
/>
{ error && (
<p className="editor-change-status__error">
{ error }
</p>
) }
</div>
) }
</VStack>
Expand Down
8 changes: 8 additions & 0 deletions packages/editor/src/components/post-status/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@
margin-top: $grid-unit-05;
}

.editor-change-status__error {
background-color: var(--wp--preset--color--accent-6);
color: $alert-red;
padding: 10px;
border-radius: 4px;
margin-bottom: 0;
}

}

0 comments on commit fe82fdb

Please sign in to comment.