-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve customizability of Select
component
#413
Merged
+212
−49
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,126 @@ | ||
import React from "react"; | ||
import ReactSelect, { components } from "react-select"; | ||
import AsyncSelect from "react-select/async"; | ||
import CreatableSelect from "react-select/creatable"; | ||
import PropTypes from "prop-types"; | ||
import { classNames } from "../../utils"; | ||
import styles from "./styles.css"; | ||
|
||
const DropdownIndicator = (props) => { | ||
return ( | ||
<components.DropdownIndicator {...props}> | ||
<div | ||
className={classNames( | ||
styles.arrowAnchor, | ||
props.selectProps.menuIsOpen && styles.arrowAnchorOpen | ||
)} | ||
/> | ||
</components.DropdownIndicator> | ||
); | ||
}; | ||
const DropdownIndicator = (props) => ( | ||
<components.DropdownIndicator {...props}> | ||
<div | ||
className={classNames( | ||
styles.arrowAnchor, | ||
props.selectProps.menuIsOpen && styles.arrowAnchorOpen | ||
)} | ||
/> | ||
</components.DropdownIndicator> | ||
); | ||
|
||
DropdownIndicator.propTypes = { | ||
selectProps: PropTypes.object, | ||
}; | ||
|
||
const Select = ({ width, isMobile, ...props }) => { | ||
const customStyles = { | ||
const Select = ({ | ||
width, | ||
isMobile, | ||
isAsync, | ||
isCreatable, | ||
customStyles, | ||
customComponents, | ||
...props | ||
}) => { | ||
const defaultStyles = { | ||
indicatorSeparator: () => ({ | ||
display: "none", | ||
}), | ||
container: (provided) => ({ | ||
...provided, | ||
width: width ? width : provided.width, | ||
padding: "0.2rem 0 0.2rem 0.4rem", | ||
}), | ||
control: (provided) => ({ | ||
...provided, | ||
"user-select": "none", | ||
"box-shadow": "none", | ||
"border-radius": "0.2rem", | ||
"min-height": isMobile ? "4.4rem" : "3rem", | ||
"min-width": "6rem", | ||
"border-color": "var(--input-border-color)", | ||
"background-color": "var(--card-background)", | ||
control: () => ({ | ||
userSelect: "none", | ||
boxShadow: "none", | ||
borderRadius: "0.2rem", | ||
minHeight: isMobile ? "4.4rem" : "3rem", | ||
minWidth: "6rem", | ||
borderColor: "var(--input-border-color)", | ||
backgroundColor: "var(--card-background)", | ||
"&:hover": { | ||
"border-color": "var(--input-border-color)", | ||
borderColor: "var(--input-border-color)", | ||
}, | ||
}), | ||
menu: (provided) => ({ | ||
...provided, | ||
"z-index": "999", | ||
"box-shadow": "0px 1rem 2rem rgba(0, 0, 0, 0.16)", | ||
"border-radius": "0.2rem", | ||
"background-color": "var(--card-background)", | ||
menu: () => ({ | ||
zIndex: "999", | ||
boxShadow: "0px 1rem 2rem rgba(0, 0, 0, 0.16)", | ||
borderRadius: "0.2rem", | ||
backgroundColor: "var(--card-background)", | ||
}), | ||
menuList: () => ({ | ||
padding: "0", | ||
}), | ||
singleValue: (provided) => ({ | ||
...provided, | ||
singleValue: () => ({ | ||
color: "var(--color-primary-dark)", | ||
}), | ||
option: (provided) => ({ | ||
...provided, | ||
option: (_, state) => ({ | ||
color: "var(--tab-text-color)", | ||
"background-color": "var(--card-background)", | ||
backgroundColor: state.isFocused | ||
? "var(--color-blue-lighter)" | ||
: "var(--card-background)", | ||
|
||
cursor: "pointer", | ||
"&:hover": { | ||
"background-color": "var(--color-blue-lighter)", | ||
}, | ||
}), | ||
}; | ||
|
||
const mergedCustomStyles = [ | ||
...new Set([...Object.keys(defaultStyles), ...Object.keys(customStyles)]), | ||
] | ||
.map((key) => ({ | ||
[key]: (provided, state) => ({ | ||
...provided, | ||
...(defaultStyles[key] && defaultStyles[key](provided, state)), | ||
...(customStyles[key] && customStyles[key](provided, state)), | ||
}), | ||
})) | ||
.reduce((prev, curr) => ({ ...prev, ...curr }), {}); | ||
|
||
const SelectComponent = isAsync | ||
? AsyncSelect | ||
: isCreatable | ||
? CreatableSelect | ||
: ReactSelect; | ||
|
||
return ( | ||
<div> | ||
<ReactSelect | ||
styles={customStyles} | ||
components={{ DropdownIndicator }} | ||
<SelectComponent | ||
styles={mergedCustomStyles} | ||
components={{ | ||
DropdownIndicator, | ||
...(customComponents && customComponents), | ||
}} | ||
{...props} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
Select.propTypes = { | ||
options: PropTypes.array.isRequired, | ||
options: PropTypes.array, | ||
isSearchable: PropTypes.bool, | ||
width: PropTypes.string, | ||
isCreatable: PropTypes.bool, | ||
width: PropTypes.number, | ||
value: PropTypes.object, | ||
isMobile: PropTypes.bool, | ||
onChange: PropTypes.func, | ||
customStyles: PropTypes.object, | ||
customComponents: PropTypes.object, | ||
}; | ||
|
||
Select.defaultProps = { | ||
isSearchable: false, | ||
isCreatable: false, | ||
customStyles: {}, | ||
customComponents: {}, | ||
}; | ||
|
||
export default React.memo(Select); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wondering why we had this here, as
src/components/Select/styles.css
file didn't exist before this commit 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It didn't exist before this commit, but it did exist before this PR.
The
globalModulePaths
config was introduced by: #227There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, i see, makes sense.
it was imported then as plain .css as it included pure css classes prefixed with
customSelect
which isn'tthe case anymore, glad it evolved to a css module. 👍🏽