Created
November 10, 2019 19:58
-
-
Save bohdanzahurskyi1/8a226cacc3ff7ac1d33e516aebcab3af to your computer and use it in GitHub Desktop.
React Select customized
This file contains hidden or 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
| import React from 'react' | |
| import styled from 'styled-components' | |
| import ReactSelect from 'react-select' | |
| export const App = () => { | |
| return ( | |
| <Container> | |
| <Select /> | |
| </Container> | |
| ) | |
| } | |
| const Select = () => { | |
| const [value, setValue] = React.useState(options[0]) | |
| return ( | |
| <ReactSelect | |
| options={options} | |
| value={value} | |
| styles={styles} | |
| onChange={(val) => setValue(val)} | |
| placeholder="Select any..." | |
| /> | |
| ) | |
| } | |
| const options = [ | |
| { value: '', label: 'Select an option...' }, | |
| { value: 'ua', label: 'Ukraine' }, | |
| { value: 'ru', label: 'Russia' }, | |
| { value: 'us', label: 'USA' }, | |
| ] | |
| const styles = { | |
| control: (styles, { isFocused }) => ({ | |
| ...styles, | |
| borderRadius: 2, | |
| ...branch(isFocused, { | |
| boxShadow: '0 0 0 1px #4CAF50', | |
| borderColor: '#4CAF50', | |
| }), | |
| ':hover': { | |
| borderColor: '#4CAF50', | |
| boxShadow: '0 0 0 1px #4CAF50', | |
| }, | |
| }) | |
| } | |
| const branch = (condition, pass, fail = {}) => condition ? pass : fail | |
| const Container = styled.div` | |
| width: 300px; | |
| padding: 20px; | |
| ` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment