-
Notifications
You must be signed in to change notification settings - Fork 3
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
[Feature] Tags on study group opening creation page #23
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
import styled from '@emotion/styled'; | ||
|
||
const TagsFormWrapper = styled.div``; | ||
|
||
const TagsForm = ({ onChange, tags }) => { | ||
const [tag, setTag] = useState(''); | ||
const [inputTags, setInputTags] = useState([]); | ||
|
||
const validateInput = (value) => { | ||
if (!value) { | ||
return; | ||
} | ||
|
||
if (inputTags.includes(value)) { | ||
return; | ||
} | ||
|
||
const resultTags = [...inputTags, value]; | ||
|
||
setInputTags(resultTags); | ||
onChange(resultTags); | ||
}; | ||
|
||
const handleChange = (e) => { | ||
setTag(e.target.value); | ||
}; | ||
|
||
const handleRemove = (removeTag) => { | ||
const removeTags = inputTags.filter((value) => value !== removeTag); | ||
|
||
setInputTags(removeTags); | ||
onChange(removeTags); | ||
}; | ||
|
||
const handleSubmit = () => { | ||
validateInput(tag.trim()); | ||
setTag(''); | ||
}; | ||
|
||
useEffect(() => { | ||
setInputTags(tags); | ||
}, [tags]); | ||
|
||
return ( | ||
<TagsFormWrapper> | ||
<h4>태그</h4> | ||
<input | ||
type="text" | ||
placeholder="태그를 입력하세요" | ||
value={tag} | ||
onChange={handleChange} | ||
onKeyPress={handleSubmit} | ||
/> | ||
<div> | ||
{inputTags.map((inputTag) => ( | ||
<span | ||
key={inputTag} | ||
onClick={() => handleRemove(inputTag)} | ||
onKeyPress={() => {}} | ||
role="button" | ||
tabIndex="-1" | ||
> | ||
{`#${inputTag}`} | ||
</span> | ||
))} | ||
</div> | ||
</TagsFormWrapper> | ||
); | ||
}; | ||
|
||
export default TagsForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import React from 'react'; | ||
|
||
import { fireEvent, render } from '@testing-library/react'; | ||
|
||
import TagsForm from './TagsForm'; | ||
|
||
describe('TagsForm', () => { | ||
const handleChange = jest.fn(); | ||
|
||
beforeEach(() => { | ||
handleChange.mockClear(); | ||
}); | ||
|
||
const renderTagsTagsForm = (tags) => render(( | ||
<TagsForm | ||
tags={tags} | ||
onChange={handleChange} | ||
/> | ||
)); | ||
|
||
describe('render Tag Form Container contents text', () => { | ||
it('renders tag form text', () => { | ||
const { getByPlaceholderText, container } = renderTagsTagsForm([]); | ||
|
||
expect(getByPlaceholderText('태그를 입력하세요')).not.toBeNull(); | ||
expect(container).toHaveTextContent('태그'); | ||
}); | ||
}); | ||
|
||
describe('Check input tag validate', () => { | ||
context('with tag input value', () => { | ||
const tags = ['JavaScript', 'React']; | ||
it('listens write field change events', () => { | ||
const { getByPlaceholderText } = renderTagsTagsForm(['CSS']); | ||
|
||
const input = getByPlaceholderText('태그를 입력하세요'); | ||
|
||
tags.forEach((tag) => { | ||
fireEvent.change(input, { target: { value: tag } }); | ||
|
||
fireEvent.keyPress(input, { key: 'Enter', code: 13, charCode: 13 }); | ||
|
||
expect(input).toHaveValue(''); | ||
}); | ||
expect(handleChange).toBeCalledTimes(2); | ||
}); | ||
}); | ||
|
||
context('without tag input value', () => { | ||
const tags = []; | ||
it("doesn't listens write field change events", () => { | ||
const { getByPlaceholderText } = renderTagsTagsForm(tags); | ||
|
||
const input = getByPlaceholderText('태그를 입력하세요'); | ||
|
||
fireEvent.change(input, { target: { value: '' } }); | ||
|
||
fireEvent.keyPress(input, { key: 'Enter', code: 13, charCode: 13 }); | ||
|
||
expect(handleChange).not.toBeCalled(); | ||
}); | ||
}); | ||
|
||
describe('It is not executed because the current tag value is included.', () => { | ||
const tags = ['JavaScript', 'React']; | ||
it('listens write field change events', () => { | ||
const { getByPlaceholderText } = renderTagsTagsForm(tags); | ||
|
||
const input = getByPlaceholderText('태그를 입력하세요'); | ||
|
||
tags.forEach((tag) => { | ||
fireEvent.change(input, { target: { value: tag } }); | ||
|
||
fireEvent.keyPress(input, { key: 'Enter', code: 13, charCode: 13 }); | ||
|
||
expect(input).toHaveValue(''); | ||
|
||
expect(handleChange).not.toBeCalled(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
context('with tags', () => { | ||
const tags = ['JavaScript', 'React']; | ||
|
||
it('renders Tags are below the input window', () => { | ||
const { container } = renderTagsTagsForm(tags); | ||
|
||
expect(container).toHaveTextContent('#JavaScript'); | ||
expect(container).toHaveTextContent('#React'); | ||
}); | ||
|
||
it('Click event remove tag', () => { | ||
const { getByText, container } = renderTagsTagsForm(tags); | ||
|
||
tags.forEach((tag) => { | ||
fireEvent.click(getByText(`#${tag}`)); | ||
|
||
expect(container).not.toHaveTextContent(`#${tag}`); | ||
}); | ||
|
||
expect(handleChange).toBeCalledTimes(2); | ||
}); | ||
}); | ||
|
||
context('without tags', () => { | ||
const tags = []; | ||
|
||
it('renders Tags are below the input window', () => { | ||
const { container } = renderTagsTagsForm(tags); | ||
|
||
expect(container).not.toHaveTextContent('#JavaScript'); | ||
expect(container).not.toHaveTextContent('#React'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
|
||
import { useDispatch, useSelector } from 'react-redux'; | ||
|
||
import { get } from '../../util/utils'; | ||
import TagsForm from '../../components/write/TagsForm'; | ||
import { changeWriteField } from '../../reducers/slice'; | ||
|
||
const TagsFormContainer = () => { | ||
const dispatch = useDispatch(); | ||
|
||
const { tags } = useSelector(get('writeField')); | ||
|
||
const onChangeTags = (nextTags) => { | ||
dispatch( | ||
changeWriteField({ | ||
name: 'tags', | ||
value: nextTags, | ||
}), | ||
); | ||
}; | ||
|
||
return ( | ||
<TagsForm onChange={onChangeTags} tags={tags} /> | ||
); | ||
}; | ||
|
||
export default TagsFormContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React from 'react'; | ||
|
||
import { fireEvent, render } from '@testing-library/react'; | ||
|
||
import { useDispatch, useSelector } from 'react-redux'; | ||
|
||
import TagsFormContainer from './TagsFormContainer'; | ||
|
||
describe('TagsFormContainer', () => { | ||
const dispatch = jest.fn(); | ||
|
||
beforeEach(() => { | ||
dispatch.mockClear(); | ||
|
||
useDispatch.mockImplementation(() => dispatch); | ||
|
||
useSelector.mockImplementation((state) => state({ | ||
writeField: { | ||
tags: [], | ||
}, | ||
})); | ||
}); | ||
|
||
const renderTagsFormContainer = () => render(( | ||
<TagsFormContainer /> | ||
)); | ||
|
||
describe('render Tag Form Container contents text', () => { | ||
it('renders tag form text', () => { | ||
const { getByPlaceholderText, container } = renderTagsFormContainer(); | ||
|
||
expect(getByPlaceholderText('태그를 입력하세요')).not.toBeNull(); | ||
expect(container).toHaveTextContent('태그'); | ||
}); | ||
}); | ||
|
||
describe('calls dispatch tags change action', () => { | ||
const tags = ['JavaScript', 'React']; | ||
it('change tags', () => { | ||
const { getByPlaceholderText } = renderTagsFormContainer(); | ||
|
||
const input = getByPlaceholderText('태그를 입력하세요'); | ||
|
||
tags.forEach((tag) => { | ||
fireEvent.change(input, { target: { value: tag } }); | ||
|
||
fireEvent.keyPress(input, { key: 'Enter', code: 13, charCode: 13 }); | ||
|
||
expect(input).toHaveValue(''); | ||
}); | ||
expect(dispatch).toBeCalledWith({ | ||
type: 'application/changeWriteField', | ||
payload: { name: 'tags', value: tags }, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import React from 'react'; | ||
|
||
import TagFormContainer from '../containers/write/TagsFormContainer'; | ||
|
||
import Responsive from '../styles/Responsive'; | ||
|
||
const IntroducePage = () => ( | ||
|
@@ -9,11 +11,17 @@ const IntroducePage = () => ( | |
<input type="text" placeholder="제목을 입력하세요" /> | ||
</div> | ||
<div> | ||
<textarea rows="10" cols="100" placeholder="내용" /> | ||
<span>모집 마감 날짜</span> | ||
<input type="date" /> | ||
</div> | ||
<div> | ||
<input type="text" placeholder="태그를 입력하세요" /> | ||
<span>참여 인원 수</span> | ||
<input type="number" /> | ||
</div> | ||
<div> | ||
<textarea rows="10" cols="100" placeholder="내용" /> | ||
</div> | ||
<TagFormContainer /> | ||
<div> | ||
<button type="button">저장</button> | ||
</div> | ||
Comment on lines
11
to
27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.