Skip to content

fix: insert tag #485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions src/components/write/TagInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import palette from '../../lib/styles/palette';
import transitions from '../../lib/styles/transitions';
import { mediaQuery } from '../../lib/styles/media';
import { useTransition, animated } from 'react-spring';
import OutsideClickHandler from 'react-outside-click-handler';

export interface TagInputProps {
ref?: React.RefObject<HTMLDivElement>;
Expand All @@ -24,7 +25,6 @@ const TagInput: React.FC<TagInputProps> = ({ onChange, tags: initialTags }) => {
const [value, setValue] = useState('');
const [focus, setFocus] = useState(false);
const ignore = useRef(false);
const editableDiv = useRef<HTMLDivElement>(null);

useEffect(() => {
if (tags.length === 0) return;
Expand All @@ -39,13 +39,10 @@ const TagInput: React.FC<TagInputProps> = ({ onChange, tags: initialTags }) => {
setValue(e.target.value);
};

useEffect(() => {
if (editableDiv.current) {
if (value === '') {
editableDiv.current.innerText = value;
}
}
}, [value]);
const onOutsideClick = () => {
if (value === '') return;
insertTag(value);
};

const insertTag = useCallback(
(tag: string) => {
Expand Down Expand Up @@ -96,23 +93,25 @@ const TagInput: React.FC<TagInputProps> = ({ onChange, tags: initialTags }) => {
};

return (
<TagInputBlock>
{tags.map((tag) => (
<TagItem key={tag} onClick={() => onRemove(tag)}>
{tag}
</TagItem>
))}
<StyledInput
placeholder="태그를 입력하세요"
tabIndex={2}
onKeyDown={onKeyDown}
onChange={onChangeInput}
value={value}
onFocus={() => setFocus(true)}
onBlur={() => setFocus(false)}
/>
<Help focus={focus} />
</TagInputBlock>
<OutsideClickHandler onOutsideClick={onOutsideClick}>
<TagInputBlock>
{tags.map((tag) => (
<TagItem key={tag} onClick={() => onRemove(tag)}>
{tag}
</TagItem>
))}
<StyledInput
placeholder="태그를 입력하세요"
tabIndex={2}
onKeyDown={onKeyDown}
onChange={onChangeInput}
value={value}
onFocus={() => setFocus(true)}
onBlur={() => setFocus(false)}
/>
<Help focus={focus} />
</TagInputBlock>
</OutsideClickHandler>
);
};

Expand Down