Skip to content

Commit

Permalink
react-select demo
Browse files Browse the repository at this point in the history
  • Loading branch information
benkeen committed Dec 3, 2024
1 parent bc42f83 commit 0a6188a
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 44 deletions.
42 changes: 2 additions & 40 deletions apps/docs/README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,3 @@
# Website
# Docs

This website is built using [Docusaurus](https://docusaurus.io/), a modern static website generator.

### Installation

```
$ yarn
```

### Local Development

```
$ yarn start
```

This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server.

### Build

```
$ yarn build
```

This command generates static content into the `build` directory and can be served using any static contents hosting service.

### Deployment

Using SSH:

```
$ USE_SSH=true yarn deploy
```

Not using SSH:

```
$ GIT_USER=<Your GitHub username> yarn deploy
```

If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch.
This package contains the documentation and demos for `react-country-region-selector`.
4 changes: 2 additions & 2 deletions apps/docs/docs/Demos/integrations/MaterialUI.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ sidebar_position: 1

import MaterialUI from '@site/src/components/demos/integrations/MaterialUI';

This demo illustrates how you can integrate this script with Material UI. Note the `customRender` method and how
its handling the actual rendering of the dropdowns.
This shows an integration with [Material UI](https://mui.com/material-ui/getting-started/). Note the
`customRender` method and how its handling the actual rendering of the dropdowns.

<MaterialUI />

Expand Down
94 changes: 94 additions & 0 deletions apps/docs/docs/Demos/integrations/ReactSelect.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
title: React Select
---

import ReactSelect from '@site/src/components/demos/integrations/ReactSelect';

[React-select](https://react-select.com/) is a popular standalone component for dropdowns. This shows an
example integration.

_Warning: opinions!_ I've used react-select on many projects and it's a beautiful library. Once it's integrated into
your app it looks and works beautifully. But the API of passing a complex object as the `value` is very poor design.
Even simple things like accommodating localization becomes very complicated since the value prop changes based on
language and you need to change the value prop each time.

This integration highlights that difficult API decision on their parts. We have to store in state all the info
react-select needs - then it'll be up to you to store that info.

The code here could be improved; typings too. But hopefully it gets you on the right path.

<ReactSelect />

---

```tsx
import React, { useState } from 'react';
import Select from 'react-select';
import { CountryDropdown, RegionDropdown } from 'react-country-region-selector';

const customRender = (props) => {
const {
options,
value,
disabled,
onChange,
reactSelectValue,
onBlur,
...selectProps
} = props;

return (
<Select
{...selectProps}
options={options}
isDisabled={disabled}
isSearchable={true}
isClearable={true}
value={reactSelectValue}
onChange={(data: any) => {
onChange({ target: { value: data } }, null);
}}
/>
);
};

const ReactSelect = () => {
const [country, setCountry] = useState();
const [region, setRegion] = useState();

return (
<>
<div style={{ width: 200, display: 'inline-block', marginRight: 8 }}>
<CountryDropdown
value={country?.value || ''}
reactSelectValue={country}
className="country"
name="country-field"
classNamePrefix="country-"
onChange={(val) => {
setCountry(val ? val : undefined);
setRegion(null);
}}
customRender={customRender}
/>
</div>
<div style={{ width: 200, display: 'inline-block' }}>
<RegionDropdown
country={country?.value || ''}
value={region?.value || null}
reactSelectValue={region}
className="region"
name="region-field"
classNamePrefix="region-"
onChange={(val) => {
setRegion(val);
}}
customRender={customRender}
/>
</div>
</>
);
};

export default ReactSelect;
```
3 changes: 2 additions & 1 deletion apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"prism-react-renderer": "^2.3.0",
"react": "^18.0.0",
"react-country-region-selector": "workspace:*",
"react-dom": "^18.0.0"
"react-dom": "^18.0.0",
"react-select": "^5.8.3"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "3.6.3",
Expand Down
70 changes: 70 additions & 0 deletions apps/docs/src/components/demos/integrations/ReactSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React, { useState } from 'react';
import Select from 'react-select';
import { CountryDropdown, RegionDropdown } from 'react-country-region-selector';

const customRender = (props) => {
const {
options,
value,
disabled,
onChange,
reactSelectValue,
onBlur,
...selectProps
} = props;

return (
<Select
{...selectProps}
options={options}
isDisabled={disabled}
isSearchable={true}
isClearable={true}
value={reactSelectValue}
onChange={(data: any) => {
onChange({ target: { value: data } }, null);
}}
/>
);
};

const ReactSelect = () => {
const [country, setCountry] = useState();
const [region, setRegion] = useState();

return (
<>
<div style={{ width: 200, display: 'inline-block', marginRight: 8 }}>
<CountryDropdown
value={country?.value || ''}
reactSelectValue={country}
className="country"
name="country-field"
classNamePrefix="country-"
onChange={(val) => {
setCountry(val ? val : undefined);
setRegion(null);
}}
customRender={customRender}
/>
</div>
<div style={{ width: 200, display: 'inline-block' }}>
<RegionDropdown
country={country?.value || ''}
value={region?.value || null}
reactSelectValue={region}
className="region"
name="region-field"
classNamePrefix="region-"
onChange={(val) => {
console.log('... ', val);
setRegion(val);
}}
customRender={customRender}
/>
</div>
</>
);
};

export default ReactSelect;
2 changes: 1 addition & 1 deletion packages/react-country-region-selector/src/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const defaultRender = (data: RenderData) => {
return (
<select {...rest}>
{options
.filter((row) => !row)
.filter((row) => row)
.map(({ label, value, key }) => (
<option value={value} key={key}>
{label}
Expand Down
40 changes: 40 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0a6188a

Please sign in to comment.