| 1 |
import { useState, useEffect, useRef } from '@wordpress/element'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
|
| 4 |
/** |
| 5 |
* Modal for editing the campaign name inline. |
| 6 |
* Opens when user clicks the name in the top nav bar. |
| 7 |
*/ |
| 8 |
export default function CampaignNameModal( { currentName, onSave, onClose } ) { |
| 9 |
const [ name, setName ] = useState( currentName || '' ); |
| 10 |
const inputRef = useRef( null ); |
| 11 |
|
| 12 |
useEffect( () => { |
| 13 |
inputRef.current?.focus(); |
| 14 |
inputRef.current?.select(); |
| 15 |
}, [] ); |
| 16 |
|
| 17 |
const handleSubmit = ( e ) => { |
| 18 |
e.preventDefault(); |
| 19 |
const trimmed = name.trim(); |
| 20 |
if ( trimmed ) { |
| 21 |
onSave( trimmed ); |
| 22 |
} |
| 23 |
onClose(); |
| 24 |
}; |
| 25 |
|
| 26 |
const handleKeyDown = ( e ) => { |
| 27 |
if ( e.key === 'Escape' ) onClose(); |
| 28 |
}; |
| 29 |
|
| 30 |
return ( |
| 31 |
<div className="bp-name-modal-overlay" onClick={ onClose }> |
| 32 |
<div className="bp-name-modal" onClick={ ( e ) => e.stopPropagation() }> |
| 33 |
<h3 className="bp-name-modal__title">{ __( 'Edit Campaign Name', 'better-payment' ) }</h3> |
| 34 |
<form onSubmit={ handleSubmit }> |
| 35 |
<input |
| 36 |
ref={ inputRef } |
| 37 |
className="bp-name-modal__input" |
| 38 |
type="text" |
| 39 |
value={ name } |
| 40 |
onChange={ ( e ) => setName( e.target.value ) } |
| 41 |
onKeyDown={ handleKeyDown } |
| 42 |
placeholder={ __( 'Campaign name…', 'better-payment' ) } |
| 43 |
maxLength={ 200 } |
| 44 |
/> |
| 45 |
<div className="bp-name-modal__actions"> |
| 46 |
<button type="button" className="bp-cb-btn bp-cb-btn--ghost-dark" onClick={ onClose }> |
| 47 |
{ __( 'Cancel', 'better-payment' ) } |
| 48 |
</button> |
| 49 |
<button type="submit" className="bp-cb-btn bp-cb-btn--blue" disabled={ ! name.trim() }> |
| 50 |
{ __( 'Update', 'better-payment' ) } |
| 51 |
</button> |
| 52 |
</div> |
| 53 |
</form> |
| 54 |
</div> |
| 55 |
</div> |
| 56 |
); |
| 57 |
} |
| 58 |
|