Skip to content

Commit 95be9ce

Browse files
committed
tidy up code
1 parent 68c2067 commit 95be9ce

File tree

4 files changed

+168
-143
lines changed

4 files changed

+168
-143
lines changed

digitransit-component/packages/digitransit-component-autosuggest/src/components/Input.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function Input({
4040
autoFocus={autoFocus}
4141
placeholder={placeholder}
4242
required={required}
43+
type="text"
4344
className={cx(
4445
styles.input,
4546
isMobile && transportMode ? styles.thin : '',

digitransit-component/packages/digitransit-component-autosuggest/src/components/MobileView.js

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,35 @@ const isKeyboardSelectionEvent = event => {
2323
return true;
2424
};
2525

26+
/**
27+
* @typedef MobileViewProps
28+
* @property {string} appElement
29+
* @property {string} id
30+
* @property {string} placeholder
31+
* @property {function} closeHandle
32+
* @property {function} clearInput
33+
* @property {array} suggestions
34+
* @property {function} onSelectedItemChange
35+
* @property {object} fontWeights
36+
* @property {function} clearOldSearches
37+
* @property {object} itemProps
38+
* @property {string} color
39+
* @property {string} accessiblePrimaryColor
40+
* @property {string} hoverColor
41+
* @property {string} lng
42+
* @property {object} ariaProps
43+
* @property {boolean} renderMobile
44+
* @property {string} clearButtonColor
45+
* @property {string} inputValue
46+
* @property {function} setInputValue
47+
* @property {string} inputClassName
48+
* @property {boolean} required
49+
* @property {string} [mobileLabel]
50+
* @property {boolean} [showScroll]
51+
*
52+
* @param {MobileViewProps} props
53+
* @returns {JSX.Element}
54+
*/
2655
const MobileView = ({
2756
appElement,
2857
id,
@@ -51,12 +80,11 @@ const MobileView = ({
5180
const [t] = useTranslation();
5281
const { lock, unlock } = hooks.useScrollLock();
5382
const styles = showScroll ? mobileStyles : mobileNoScrollStyles;
54-
5583
const inputId = `${id}-input`;
5684
const labelId = `${id}-label`;
57-
5885
const [isDialogOpen, setDialogOpen] = useState(false);
5986
const inputRef = React.useRef();
87+
6088
useEffect(() => {
6189
ReactModal.setAppElement(appElement);
6290
}, []);
@@ -70,7 +98,7 @@ const MobileView = ({
7098
}, [renderMobile]);
7199

72100
/**
73-
* Downshift hooks do not support conditional rendering so we need independent hooks in mobile view.
101+
* independent hooks in mobile view.
74102
* inputValue and suggestion states are kept in the parent
75103
*/
76104
const {
@@ -84,8 +112,9 @@ const MobileView = ({
84112
inputValue,
85113
onInputValueChange: ({ inputValue: newValue }) => setInputValue(newValue),
86114
onSelectedItemChange,
87-
defaultHighlightedIndex: 0,
115+
defaultHighlightedIndex: -1,
88116
});
117+
// call to suppress ref errors from downshift, might need better solution
89118
getLabelProps({}, { suppressRefError: true });
90119
getMenuProps({}, { suppressRefError: true });
91120
getInputProps({}, { suppressRefError: true });
@@ -103,7 +132,6 @@ const MobileView = ({
103132
.concat(SearchBarId)
104133
.concat(' ')
105134
.concat(ariaCurrentSuggestion);
106-
107135
return (
108136
<ReactModal
109137
isOpen={renderMobile}
@@ -138,7 +166,7 @@ const MobileView = ({
138166
id={labelId}
139167
{...getLabelProps()}
140168
>
141-
{mobileLabel || t(id, { lng })}
169+
{mobileLabel}
142170
</span>
143171
<Input
144172
placeholder={
@@ -200,46 +228,35 @@ const MobileView = ({
200228
};
201229

202230
MobileView.propTypes = {
203-
lng: PropTypes.string.isRequired,
204-
color: PropTypes.string,
231+
appElement: PropTypes.string.isRequired,
232+
id: PropTypes.string.isRequired,
205233
clearInput: PropTypes.func.isRequired,
234+
clearOldSearches: PropTypes.func.isRequired,
235+
closeHandle: PropTypes.func.isRequired,
236+
onSelectedItemChange: PropTypes.func.isRequired,
237+
inputValue: PropTypes.string.isRequired,
238+
setInputValue: PropTypes.func.isRequired,
239+
color: PropTypes.string.isRequired,
206240
accessiblePrimaryColor: PropTypes.string.isRequired,
207-
hoverColor: PropTypes.string,
241+
clearButtonColor: PropTypes.string.isRequired,
242+
hoverColor: PropTypes.string.isRequired,
208243
fontWeights: PropTypes.shape({
209-
normal: PropTypes.number,
210-
medium: PropTypes.number,
211-
bold: PropTypes.number,
244+
medium: PropTypes.number.isRequired,
212245
}).isRequired,
213-
appElement: PropTypes.string.isRequired,
214-
id: PropTypes.string.isRequired,
215-
mobileLabel: PropTypes.string,
216-
clearOldSearches: PropTypes.func.isRequired,
217246
itemProps: PropTypes.shape({}).isRequired,
218247
suggestions: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
219-
closeHandle: PropTypes.func.isRequired,
220-
showScroll: PropTypes.bool,
221-
focusInput: PropTypes.bool,
222-
clearButtonColor: PropTypes.string.isRequired,
223-
onSelectedItemChange: PropTypes.func.isRequired,
224248
ariaProps: PropTypes.shape({
225249
ariaRequiredText: PropTypes.string.isRequired,
226250
SearchBarId: PropTypes.string.isRequired,
227251
ariaCurrentSuggestion: PropTypes.string.isRequired,
228252
}).isRequired,
229-
placeholder: PropTypes.string.isRequired,
230-
inputValue: PropTypes.string.isRequired,
231-
setInputValue: PropTypes.func.isRequired,
232-
renderMobile: PropTypes.bool.isRequired,
233253
inputClassName: PropTypes.string.isRequired,
254+
placeholder: PropTypes.string.isRequired,
255+
mobileLabel: PropTypes.string.isRequired,
234256
required: PropTypes.bool.isRequired,
235-
};
236-
237-
MobileView.defaultProps = {
238-
mobileLabel: undefined,
239-
focusInput: false,
240-
color: undefined,
241-
hoverColor: undefined,
242-
showScroll: undefined,
257+
renderMobile: PropTypes.bool.isRequired,
258+
showScroll: PropTypes.bool.isRequired,
259+
lng: PropTypes.string.isRequired,
243260
};
244261

245262
export default MobileView;

digitransit-component/packages/digitransit-component-autosuggest/src/components/Suggestions.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ export function Suggestions({
106106
return (
107107
<Suggestion
108108
key={`${suggestion.type}-${
109-
suggestion.properties.id || suggestion.properties.name
109+
suggestion.properties.id ||
110+
suggestion.properties.name ||
111+
suggestion.properties.gtfsId
110112
}`}
111113
highlightedIndex={highlightedIndex}
112114
itemIndex={i}
@@ -119,7 +121,7 @@ export function Suggestions({
119121
);
120122
})}
121123
{renderClearHistoryButton && (
122-
<li>
124+
<li {...getItemProps({ index: suggestions.length })}>
123125
<button
124126
onClick={handleClearHistory}
125127
type="button"

0 commit comments

Comments
 (0)