|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { useTranslation } from '@/lib/i18n'; |
| 4 | +import BasicModal from '@/components/core/modal/basic-modal'; |
| 5 | + |
| 6 | +const LinkInputModal = ({ isOpen, onClose, onConfirm }) => { |
| 7 | + const [display, setDisplay] = useState(''); |
| 8 | + const [title, setTitle] = useState(''); |
| 9 | + const [url, setUrl] = useState(''); |
| 10 | + const [openInNewTab, setOpenInNewTab] = useState(true); |
| 11 | + const t = useTranslation('link-input-modal'); |
| 12 | + |
| 13 | + const resetForm = () => { |
| 14 | + setDisplay(''); |
| 15 | + setTitle(''); |
| 16 | + setUrl(''); |
| 17 | + setOpenInNewTab(true); |
| 18 | + }; |
| 19 | + |
| 20 | + const handleClose = () => { |
| 21 | + resetForm(); |
| 22 | + onClose(); |
| 23 | + }; |
| 24 | + |
| 25 | + const handleConfirm = () => { |
| 26 | + if (!display.trim() || !url.trim()) return; |
| 27 | + const prefix = openInNewTab ? '@' : ''; |
| 28 | + const titlePart = title.trim() ? `[[${title.trim()}]]` : ''; |
| 29 | + const markdown = `${prefix}[${display.trim()}]${titlePart}(${url.trim()})`; |
| 30 | + onConfirm(markdown); |
| 31 | + handleClose(); |
| 32 | + }; |
| 33 | + |
| 34 | + return ( |
| 35 | + <BasicModal |
| 36 | + title={t('title')} |
| 37 | + isOpen={isOpen} |
| 38 | + onClose={handleClose} |
| 39 | + onCancel={handleClose} |
| 40 | + onConfirm={handleConfirm} |
| 41 | + cancelLabel={t('cancel')} |
| 42 | + confirmLabel={t('confirm')} |
| 43 | + > |
| 44 | + <div className="flex flex-col gap-6"> |
| 45 | + <div className="flex flex-col gap-2"> |
| 46 | + <label className="text-text-primary text-base" htmlFor="link-display"> |
| 47 | + {t('display')} |
| 48 | + </label> |
| 49 | + <input |
| 50 | + id="link-display" |
| 51 | + type="text" |
| 52 | + value={display} |
| 53 | + onChange={(e) => setDisplay(e.target.value)} |
| 54 | + placeholder={t('displayPlaceholder')} |
| 55 | + className="w-full border border-border-main rounded-lg px-4 py-3 text-base text-text-primary placeholder:text-text-placeholder focus:outline-none focus:ring-2 focus:ring-primary" |
| 56 | + aria-required="true" |
| 57 | + /> |
| 58 | + </div> |
| 59 | + <div className="flex flex-col gap-2"> |
| 60 | + <label |
| 61 | + className="flex gap-2 items-center text-text-primary text-base" |
| 62 | + htmlFor="link-title" |
| 63 | + > |
| 64 | + <span>{t('linkTitle')}</span> |
| 65 | + <span className="text-text-primary">{t('optional')}</span> |
| 66 | + </label> |
| 67 | + <input |
| 68 | + id="link-title" |
| 69 | + type="text" |
| 70 | + value={title} |
| 71 | + onChange={(e) => setTitle(e.target.value)} |
| 72 | + placeholder={t('titlePlaceholder')} |
| 73 | + className="w-full border border-border-main rounded-lg px-4 py-3 text-base text-text-primary placeholder:text-text-placeholder focus:outline-none focus:ring-2 focus:ring-primary" |
| 74 | + /> |
| 75 | + </div> |
| 76 | + <div className="flex flex-col gap-2"> |
| 77 | + <label className="text-text-primary text-base" htmlFor="link-url"> |
| 78 | + {t('url')} |
| 79 | + </label> |
| 80 | + <input |
| 81 | + id="link-url" |
| 82 | + type="text" |
| 83 | + value={url} |
| 84 | + onChange={(e) => setUrl(e.target.value)} |
| 85 | + placeholder={t('urlPlaceholder')} |
| 86 | + className="w-full border border-border-main rounded-lg px-4 py-3 text-base text-text-primary placeholder:text-text-placeholder focus:outline-none focus:ring-2 focus:ring-primary" |
| 87 | + aria-required="true" |
| 88 | + /> |
| 89 | + </div> |
| 90 | + <div className="flex flex-col gap-2"> |
| 91 | + <span className="text-text-primary text-base">{t('openMethod')}</span> |
| 92 | + <div className="flex items-center"> |
| 93 | + <label className="flex flex-1 items-center gap-3 py-1 cursor-pointer"> |
| 94 | + <input |
| 95 | + type="radio" |
| 96 | + name="link-open-method" |
| 97 | + checked={openInNewTab} |
| 98 | + onChange={() => setOpenInNewTab(true)} |
| 99 | + className="accent-primary w-5 h-5 shrink-0" |
| 100 | + /> |
| 101 | + <span className="text-base text-text-primary">{t('newTab')}</span> |
| 102 | + </label> |
| 103 | + <label className="flex flex-1 items-center gap-3 px-2 py-1 cursor-pointer"> |
| 104 | + <input |
| 105 | + type="radio" |
| 106 | + name="link-open-method" |
| 107 | + checked={!openInNewTab} |
| 108 | + onChange={() => setOpenInNewTab(false)} |
| 109 | + className="accent-primary w-5 h-5 shrink-0" |
| 110 | + /> |
| 111 | + <span className="text-base text-text-primary">{t('currentTab')}</span> |
| 112 | + </label> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </BasicModal> |
| 117 | + ); |
| 118 | +}; |
| 119 | + |
| 120 | +LinkInputModal.propTypes = { |
| 121 | + isOpen: PropTypes.bool.isRequired, |
| 122 | + onClose: PropTypes.func.isRequired, |
| 123 | + onConfirm: PropTypes.func.isRequired, |
| 124 | +}; |
| 125 | + |
| 126 | +export default LinkInputModal; |
0 commit comments