From 770a256ab8577683a2cd255595cc2801a9f7f577 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Wed, 9 Oct 2019 15:31:24 +0700 Subject: [PATCH] submitting tags --- src/components/node/Tag/index.tsx | 11 ++++------ src/components/node/Tags/index.tsx | 33 +++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/components/node/Tag/index.tsx b/src/components/node/Tag/index.tsx index 8d339ecb..fe0a6a9f 100644 --- a/src/components/node/Tag/index.tsx +++ b/src/components/node/Tag/index.tsx @@ -1,10 +1,4 @@ -import React, { - FC, - ReactElement, - ChangeEvent, - ChangeEventHandler, - KeyboardEventHandler, -} from 'react'; +import React, { FC, ChangeEventHandler, KeyboardEventHandler, FocusEventHandler } from 'react'; import * as styles from './styles.scss'; import { ITag } from '~/redux/types'; @@ -17,6 +11,7 @@ interface IProps { is_hoverable?: boolean; onInput?: ChangeEventHandler; onKeyUp?: KeyboardEventHandler; + onBlur?: FocusEventHandler; } const Tag: FC = ({ @@ -26,6 +21,7 @@ const Tag: FC = ({ is_hoverable, onInput, onKeyUp, + onBlur, }) => (
@@ -40,6 +36,7 @@ const Tag: FC = ({ maxLength={24} onChange={onInput} onKeyUp={onKeyUp} + onBlur={onBlur} /> )}
diff --git a/src/components/node/Tags/index.tsx b/src/components/node/Tags/index.tsx index 7da014eb..cf2bcdb8 100644 --- a/src/components/node/Tags/index.tsx +++ b/src/components/node/Tags/index.tsx @@ -6,11 +6,20 @@ import React, { useEffect, KeyboardEvent, ChangeEvent, + useRef, } from 'react'; import { TagField } from '~/components/containers/TagField'; import { ITag } from '~/redux/types'; import { Tag } from '~/components/node/Tag'; import uniq from 'ramda/es/uniq'; +import equals from 'ramda/es/equals'; +import { setTimeout } from 'timers'; +import identity from 'ramda/es/identity'; +import countBy from 'ramda/es/countBy'; +import eqBy from 'ramda/es/eqBy'; +import length from 'ramda/es/length'; +import isEmpty from 'ramda/es/isEmpty'; +import symmetricDifference from 'ramda/es/symmetricDifference'; type IProps = HTMLAttributes & { tags: ITag[]; @@ -18,15 +27,17 @@ type IProps = HTMLAttributes & { onTagsChange?: (tags: string[]) => void; }; -export const Tags: FC = ({ tags, is_editable, onChange, ...props }) => { +export const Tags: FC = ({ tags, is_editable, onTagsChange, ...props }) => { const [input, setInput] = useState(''); const [data, setData] = useState(tags); + const timer = useRef(null); const onInput = useCallback( ({ target: { value } }: ChangeEvent) => { setInput(value); + if (timer) clearTimeout(timer.current); }, - [setInput] + [setInput, timer] ); const onKeyUp = useCallback( @@ -55,7 +66,23 @@ export const Tags: FC = ({ tags, is_editable, onChange, ...props }) => { [input, setInput, data, setData] ); + const onSubmit = useCallback(() => { + if (length(tags) === length(data) && isEmpty(symmetricDifference(tags, data))) return; + + onTagsChange(data.map(tag => tag.title)); + }, [tags, data, onTagsChange]); + + const onBlur = useCallback(() => { + clearTimeout(timer.current); + onSubmit(); + }, [onSubmit, timer]); + useEffect(() => setData(tags), [tags]); + useEffect(() => { + timer.current = setTimeout(onSubmit, 3000); + + return () => clearTimeout(timer.current); + }, [data]); return ( @@ -63,7 +90,7 @@ export const Tags: FC = ({ tags, is_editable, onChange, ...props }) => { ))} - {is_editable && } + {is_editable && } ); };