Skip to content

Commit

Permalink
Add tag validation #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Godsenal committed Jul 22, 2019
1 parent e7a574b commit e369281
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/containers/EditProfile/TagEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const TagEdit: React.SFC<ITagEditProps> = ({ tags, handleUpdateUser }) => {
};
return (
<div>
<TagSelect tags={[]} value={[]} setTags={handleTagAdd} />
<TagSelect tags={[]} setTags={handleTagAdd} />
<Divider withMargin />
{
tags.map(tag => <Tag key={tag} name={tag} onDelete={handleTagDelete(tag)} />)
Expand Down
25 changes: 21 additions & 4 deletions src/containers/TagSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,38 @@ import debounce from 'lodash/debounce';
import { IOptionValue } from '../models/select';
import { makeSelectable } from '../utils/select';
import { searchTags } from '../api/tag';
import { isTag } from '../utils/validation';
import { notifier } from '../utils/renoti';
export interface ITagSelectProps extends SelectComponentsProps {
tags: string[];
setTags: (tags: string[]) => void;
}
const TagSelect: React.SFC<ITagSelectProps> = ({ tags, setTags, ...props }) => {
const [defaultValue] = useState(() => makeSelectable(tags));
const selectableTags = useMemo(() => makeSelectable(tags), [tags]);
const handleCreate = useCallback(
(newValue: ValueType<IOptionValue>) => {
if (!newValue || !Array.isArray(newValue)) {
return;
}
let error = '';
const newTags: string[] = newValue.map(
(value: IOptionValue) => value.value,
);
).filter(tag => {
try {
isTag.validateSync(tag);
return true;
} catch (err) {
error = err.message;
return false;
}
});
if (error) {
notifier.notify({
type: 'error',
position: 'top-center',
message: error,
});
}
setTags(newTags);
},
[setTags],
Expand Down Expand Up @@ -74,13 +92,12 @@ const TagSelect: React.SFC<ITagSelectProps> = ({ tags, setTags, ...props }) => {
isMulti
placeholder="태그를 검색해보세요."
cacheOptions
defaultValue={defaultValue}
defaultOptions={[]}
onChange={handleCreate}
loadOptions={loadOptions}
loadingMessage={loadingMessage}
noOptionsMessage={noOptionsMessage}
formatCreateLabel={formatCreateLabel}
value={selectableTags}
{...props}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion src/containers/WathcedTags/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const WatchedTags = () => {
<ContainerContents isActive={isTagSearch}>
{isEdit && (
<TagSelectWrapper>
<TagSelect tags={[]} value={[]} setTags={onTagSelect} />
<TagSelect tags={[]} setTags={onTagSelect} />
</TagSelectWrapper>
)}
{tags.map(tag => (
Expand Down
2 changes: 2 additions & 0 deletions src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ export const isGiturl = string()
'github 프로필 url을 적어주세요.',
)
.required('필수 항목입니다.');

export const isTag = string().trim().min(2, '태그는 2자 이상 10자 이하여야 합니다.').max(10, '태그는 2자 이상 10자 이하여야 합니다.');

0 comments on commit e369281

Please sign in to comment.