Skip to content

Commit

Permalink
setup basic component
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-odonovan committed Oct 2, 2023
1 parent 1230601 commit 92b4291
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
65 changes: 65 additions & 0 deletions src/components/Molecules/PostcodeLookup/PostcodeLookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';

import Typeahead from '../Typeahead/Typeahead';

const optionFetcher = async query => {
const response = await axios.get(
'https://lookups.sls.comicrelief.com/schools/lookup',
{
params: { query },
timeout: 3000
}
);
return response.data.data.schools;
};

const optionParser = school => `${school.name}, ${school.post_code}`;

const SchoolLookup = React.forwardRef(
(
{
label,
placeholder,
notFoundMessage,
dropdownInstruction,
onSelect,
...rest
},
ref
) => {
const props = {
optionFetcher,
optionParser,
onSelect,
id: 'school-lookup',
name: 'school-lookup',
label,
placeholder,
notFoundMessage,
dropdownInstruction,
...rest
};

return <Typeahead {...props} ref={ref} />;
}
);

SchoolLookup.propTypes = {
/** This function is used to provide data to the parent component when a selection is made. */
onSelect: PropTypes.func.isRequired,
label: PropTypes.string,
placeholder: PropTypes.string,
dropdownInstruction: PropTypes.string,
notFoundMessage: PropTypes.string
};

SchoolLookup.defaultProps = {
label: 'Enter the name or postcode of your school or nursery',
placeholder: 'Type to start search',
dropdownInstruction: 'Please select a school from the list below',
notFoundMessage: "Sorry, we can't find this school"
};

export default SchoolLookup;
18 changes: 18 additions & 0 deletions src/components/Molecules/PostcodeLookup/PostcodeLookup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# PostcodeLookup

```js
import PostcodeLookup from './PostcodeLookup';

// This is just an example of how a parent component might handle fetch errors within the component.
const [enterManually, setEnterManually] = React.useState(false);

const fetchErrorHandler = () => {
setEnterManually(true);
}

enterManually
? 'Sorry, there appears to be a problem. Please enter the school\'s details manually.'
: (
<PostcodeLookup onSelect={school => alert(JSON.stringify(school, null, 2))} fetchErrorHandler={fetchErrorHandler} />
)
```
8 changes: 4 additions & 4 deletions src/components/Molecules/PostcodeLookup/SchoolLookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const optionFetcher = async query => {

const optionParser = school => `${school.name}, ${school.post_code}`;

const SchoolLookup = React.forwardRef(
const PostcodeLookup = React.forwardRef(
(
{
label,
Expand Down Expand Up @@ -46,7 +46,7 @@ const SchoolLookup = React.forwardRef(
}
);

SchoolLookup.propTypes = {
PostcodeLookup.propTypes = {
/** This function is used to provide data to the parent component when a selection is made. */
onSelect: PropTypes.func.isRequired,
label: PropTypes.string,
Expand All @@ -55,11 +55,11 @@ SchoolLookup.propTypes = {
notFoundMessage: PropTypes.string
};

SchoolLookup.defaultProps = {
PostcodeLookup.defaultProps = {
label: 'Enter the name or postcode of your school or nursery',
placeholder: 'Type to start search',
dropdownInstruction: 'Please select a school from the list below',
notFoundMessage: "Sorry, we can't find this school"
};

export default SchoolLookup;
export default PostcodeLookup;
8 changes: 4 additions & 4 deletions src/components/Molecules/PostcodeLookup/SchoolLookup.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SchoolLookup
# PostcodeLookup

```js
import SchoolLookup from './SchoolLookup';
import PostcodeLookup from './PostcodeLookup';

// This is just an example of how a parent component might handle fetch errors within the component.
const [enterManually, setEnterManually] = React.useState(false);
Expand All @@ -11,8 +11,8 @@ const fetchErrorHandler = () => {
}

enterManually
? 'Sorry, there appears to be a problem. Please enter the school\'s details manually.'
? 'Sorry, there appears to be a problem. Please enter the school\'s details manually.'
: (
<SchoolLookup onSelect={school => alert(JSON.stringify(school, null, 2))} fetchErrorHandler={fetchErrorHandler} />
<PostcodeLookup onSelect={school => alert(JSON.stringify(school, null, 2))} fetchErrorHandler={fetchErrorHandler} />
)
```

0 comments on commit 92b4291

Please sign in to comment.