PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Admin / fields / LockedTextInput / index.tsx

index.tsx in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Admin/fields/LockedTextInput/index.tsx

68 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import Notice from '@givewp/admin/components/Notices';
2 import {AdminSectionField} from '@givewp/components/AdminDetailsPage/AdminSection';
3 import {useEffect, useState} from '@wordpress/element';
4 import {__} from '@wordpress/i18n';
5 import {useFormContext, useFormState} from 'react-hook-form';
6 import {EditIcon} from './Icons';
7 import styles from './styles.module.scss';
8
9 interface LockedTextInputProps {
10 name: string;
11 label: string;
12 description: string;
13 placeholder?: string;
14 warningMessage: string;
15 }
16
17 /**
18 * @since 4.11.0
19 */
20 export default function LockedTextInput({name, label, description, placeholder, warningMessage}: LockedTextInputProps) {
21 const {register, setFocus} = useFormContext();
22 const {errors, isSubmitSuccessful} = useFormState();
23 const [isEditing, setIsEditing] = useState(false);
24
25 const handleEditClick = () => {
26 const newEditingState = !isEditing;
27 setIsEditing(newEditingState);
28
29 // Focus on input when editing is activated
30 if (newEditingState) {
31 setFocus(name);
32 }
33 };
34
35 // Reset editing mode when form is successfully submitted
36 useEffect(() => {
37 if (isSubmitSuccessful) {
38 setIsEditing(false);
39 }
40 }, [isSubmitSuccessful]);
41
42 return (
43 <AdminSectionField error={errors[name]?.message as string}>
44 <label htmlFor={name}>{label}</label>
45 <p>{description}</p>
46 <div className={styles.inputContainer}>
47 <input
48 id={name}
49 type="text"
50 className={styles.input}
51 {...register(name)}
52 placeholder={placeholder}
53 readOnly={!isEditing}
54 />
55 <button
56 type="button"
57 className={`${styles.editButton} ${isEditing ? styles.editing : ''}`}
58 onClick={handleEditClick}
59 aria-label={isEditing ? __('Cancel editing', 'give') : __('Edit field', 'give')}
60 >
61 <EditIcon strokeColor={isEditing ? '#9ca0af' : '#000'} />
62 </button>
63 </div>
64 {isEditing && <Notice type="warning">{warningMessage}</Notice>}
65 </AdminSectionField>
66 );
67 }
68