-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.tsx
227 lines (200 loc) · 6.83 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* External dependencies
*/
import React from 'react';
import { useDispatch, useSelect } from '@wordpress/data';
import { Suggestions } from '@automattic/components';
import { useI18n } from '@automattic/react-i18n';
import { __experimentalCreateInterpolateElement } from '@wordpress/element';
import { ENTER, TAB } from '@wordpress/keycodes';
import classnames from 'classnames';
import { remove } from 'lodash';
/**
* Internal dependencies
*/
import { STORE_KEY as ONBOARD_STORE } from '../../../stores/onboard';
import { Verticals } from '@automattic/data-stores';
import { SiteVertical } from '../../../stores/onboard/types';
import useTyper from '../../../hooks/use-typer';
import Arrow from '../arrow';
/**
* Style dependencies
*/
import './style.scss';
type Suggestion = SiteVertical & { category?: string };
const VERTICALS_STORE = Verticals.register();
const VerticalSelect: React.FunctionComponent = () => {
const { __: NO__ } = useI18n();
const inputRef = React.useRef< HTMLSpanElement >( document.createElement( 'span' ) );
const [ isFocused, setIsFocused ] = React.useState< boolean >( false );
const [ suggestions, setSuggestions ] = React.useState< Suggestion[] >( [] );
/**
* Ref to the <Suggestions />, necessary for handling input events
*
* This ref is effectively `any` and should therefore be considered _dangerous_.
*
* TODO: This should be a typed ref to Suggestions, but the component is not typed.
*
* Using `Suggestions` here would effectively be `any`.
*/
const suggestionRef = React.createRef< any >();
const verticals = useSelect( select =>
select( VERTICALS_STORE )
.getVerticals()
.map( x => ( {
label: x.vertical_name,
id: x.vertical_id,
slug: x.vertical_slug,
} ) )
);
const { siteVertical, siteTitle } = useSelect( select => select( ONBOARD_STORE ).getState() );
const { setSiteVertical, resetSiteVertical } = useDispatch( ONBOARD_STORE );
const inputText = inputRef.current.innerText || '';
const isInputEmpty = ! inputText.length;
const showArrow = ! siteTitle && ! siteVertical && inputText.length > 2;
const animatedPlaceholder = useTyper(
[
NO__( 'photography' ),
NO__( 'blogging' ),
NO__( 'travel' ),
NO__( 'marketing' ),
NO__( 'fashion' ),
NO__( 'shopping' ),
NO__( 'design' ),
NO__( 'real estate' ),
NO__( 'food' ),
NO__( 'sports' ),
],
isInputEmpty,
{ delayBetweenWords: 800, delayBetweenCharacters: 110 }
);
const updateSuggestions = ( inputValue: string ) => {
if ( inputValue.length < 3 ) {
setSuggestions( [] );
return;
}
const normalizedInputValue = inputValue.toLowerCase();
let newSuggestions = verticals.filter( vertical =>
vertical.label.toLowerCase().includes( normalizedInputValue )
);
// Does the verticals list include an exact match?
// If yes, we store it in firstSuggestion (for later use), and remove it from newSuggestions...
const firstSuggestion = remove(
newSuggestions,
suggestion => suggestion.label.toLowerCase() === normalizedInputValue
)[ 0 ] ?? {
// ...otherwise, we set firstSuggestion to the user-supplied vertical...
label: inputValue.trim(),
id: '', // User-supplied verticals don't have IDs or slugs
slug: '',
};
// ...and finally, we prepend firstSuggestion to our suggestions list.
newSuggestions.unshift( firstSuggestion );
// If there is only one suggestion and that suggestion matches the user input value,
// do not show any suggestions.
// TODO: write a more advanced compare fn https://github.com/Automattic/wp-calypso/pull/40645#discussion_r402156751
if (
newSuggestions.length === 1 &&
newSuggestions[ 0 ].label.toLowerCase() === normalizedInputValue
) {
newSuggestions = [];
}
setSuggestions( newSuggestions );
};
const handleInputKeyUpEvent = ( e: React.KeyboardEvent< HTMLSpanElement > ) => {
const input = e.currentTarget.innerText.trim();
if ( ! input.length ) {
resetSiteVertical();
}
updateSuggestions( input );
};
const handleSelect = ( vertical: SiteVertical ) => {
setSiteVertical( vertical );
setIsFocused( false ); // prevent executing handleBlur()
};
const handleBlur = () => {
const lastQuery = inputText.trim();
if ( isFocused && lastQuery.length ) {
const vertical = suggestions[ 0 ] ?? { label: lastQuery, id: '', slug: '' };
handleSelect( vertical );
}
};
const handleInputKeyDownEvent = ( e: React.KeyboardEvent< HTMLSpanElement > ) => {
const input = e.currentTarget.innerText.trim();
if ( suggestionRef.current ) {
suggestionRef.current.handleKeyEvent( e );
}
if ( e.keyCode === ENTER ) {
e.preventDefault();
input.length && ! suggestions.length && handleSelect( { label: input } );
return;
}
if ( e.keyCode === TAB ) {
e.preventDefault();
handleBlur();
}
};
React.useEffect( () => {
if ( isInputEmpty ) {
inputRef.current.focus();
}
}, [] ); // eslint-disable-line react-hooks/exhaustive-deps
React.useEffect( () => {
inputRef.current.innerText = siteVertical?.label || '';
}, [ siteVertical, inputRef ] );
// translators: Form input for a site's topic where "<Input />" is replaced by user input and must be preserved verbatim in translated string.
const madlibTemplate = NO__( 'My site is about <Input />' );
// translators: Form input for a site's topic where "<Input />" is replaced with the topic selected by the user.
const madlibTemplateWithPeriod = NO__( 'My site is about <Input />.' );
const madlib = __experimentalCreateInterpolateElement(
siteVertical ? madlibTemplateWithPeriod : madlibTemplate,
{
Input: (
<span className="vertical-select__suggestions-wrapper">
<span className="vertical-select__input-wrapper">
{ isInputEmpty && (
<span className="vertical-select__placeholder">{ animatedPlaceholder }</span>
) }
<span
contentEditable
tabIndex={ 0 }
role="textbox"
aria-multiline="true"
spellCheck={ false }
ref={ inputRef }
/* eslint-disable-next-line wpcalypso/jsx-classname-namespace */
className="madlib__input"
onKeyDown={ handleInputKeyDownEvent }
onKeyUp={ handleInputKeyUpEvent }
onFocus={ () => setIsFocused( true ) }
onBlur={ handleBlur }
/>
</span>
{ /* us visibility to keep the layout fixed with and without the arrow */ }
{ showArrow && <Arrow className="vertical-select__arrow" /> }
<div className="vertical-select__suggestions">
{ isFocused && !! verticals.length && (
<Suggestions
ref={ suggestionRef }
query={ inputText }
suggestions={ suggestions }
suggest={ handleSelect }
title={ NO__( 'Suggestions' ) }
/>
) }
</div>
</span>
),
}
);
return (
<form
className={ classnames( 'vertical-select', {
'vertical-select--without-value': isInputEmpty,
} ) }
>
{ madlib }
</form>
);
};
export default VerticalSelect;