Skip to content

Instantly share code, notes, and snippets.

@bohdanzahurskyi1
Created November 10, 2019 19:58
Show Gist options
  • Select an option

  • Save bohdanzahurskyi1/8a226cacc3ff7ac1d33e516aebcab3af to your computer and use it in GitHub Desktop.

Select an option

Save bohdanzahurskyi1/8a226cacc3ff7ac1d33e516aebcab3af to your computer and use it in GitHub Desktop.
React Select customized
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