Skip to content

Commit e7b59a6

Browse files
MichaelDeBoeySaraVieira
authored andcommitted
Refactor DocSearch to functional component with hooks (codesandbox#1940)
1 parent e637402 commit e7b59a6

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed
Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { Component } from 'react';
21
import Input from '@codesandbox/common/lib/components/Input';
2+
import React, { useEffect, useState } from 'react';
33
import styled from 'styled-components';
44
import DocSearchStyle from '../css/docSearch';
55

@@ -10,53 +10,48 @@ const StyledInput = styled(Input)`
1010
padding-left: 8px;
1111
border-radius: 4px;
1212
padding-right: 30px;
13-
box-shadow: inset 0px 2px 2px rgba(0, 0, 0, 0.25);
13+
box-shadow: inset 0 2px 2px rgba(0, 0, 0, 0.25);
1414
1515
&:focus,
1616
&:hover {
1717
border: 1px solid #0d7bc9;
18-
box-shadow: inset 0px 2px 2px rgba(0, 0, 0, 0.6);
18+
box-shadow: inset 0 2px 2px rgba(0, 0, 0, 0.6);
1919
}
2020
`;
2121

22-
class DocSearch extends Component {
23-
state = {
24-
enabled: true,
25-
};
26-
componentDidMount() {
22+
const DocSearch = () => {
23+
const [enabled, setEnabled] = useState(true);
24+
25+
useEffect(() => {
2726
// Initialize Algolia search.
28-
// eslint-disable-next-line no-undef
2927
if (window.docsearch) {
3028
window.docsearch({
3129
apiKey: '45db7de01ac97a7c4c673846830c4117',
30+
debug: false,
3231
indexName: 'codesandbox',
3332
inputSelector: '#algolia-doc-search',
34-
debug: false,
3533
});
3634
} else {
3735
// eslint-disable-next-line
3836
console.warn('Search has failed to load and now is being disabled');
39-
// eslint-disable-next-line
40-
this.setState({ enabled: false });
37+
38+
setEnabled(false);
4139
}
42-
}
40+
}, []);
4341

44-
render() {
45-
const { enabled } = this.state;
46-
47-
return enabled ? (
48-
<form style={{ margin: 0 }}>
49-
<StyledInput
50-
id="algolia-doc-search"
51-
type="search"
52-
placeholder="Search our docs"
53-
aria-label="Search docs"
54-
block
55-
/>
56-
<DocSearchStyle />
57-
</form>
58-
) : null;
59-
}
60-
}
42+
return enabled ? (
43+
<form style={{ margin: 0 }}>
44+
<StyledInput
45+
aria-label="Search docs"
46+
block
47+
id="algolia-doc-search"
48+
placeholder="Search our docs"
49+
type="search"
50+
/>
51+
52+
<DocSearchStyle />
53+
</form>
54+
) : null;
55+
};
6156

6257
export default DocSearch;

0 commit comments

Comments
 (0)