Complete select component

This commit is contained in:
Hazem Krimi
2021-04-20 23:23:55 +01:00
parent 52fd3b3be3
commit d4434d03dd
3 changed files with 281 additions and 4 deletions
+55 -2
View File
@@ -1,7 +1,60 @@
import { Wrapper } from './styles';
import { Text } from '..';
const Select = () => {
return <Wrapper></Wrapper>;
type SelectProps = {
className?: string;
color?:
| 'client'
| 'productOwner'
| 'developer'
| 'admin'
| 'success'
| 'warning'
| 'error'
| 'black'
| 'white';
error?: boolean;
errorMessage?: string;
options: Array<{ value: any; label: string }>;
value: string;
label: string;
fullWidth?: boolean;
onChange: (event: React.ChangeEvent<HTMLSelectElement>) => void;
};
const Select = ({
color = 'client',
label,
value,
options,
onChange,
error,
errorMessage,
...props
}: SelectProps) => {
return (
<Wrapper label={label} error={error} color={color} {...props}>
<div className='info'>
{label && (
<Text variant='body' weight='bold' className='label'>
{label}
</Text>
)}
{error && errorMessage && (
<Text variant='body' color='error' className='error-message'>
{errorMessage}
</Text>
)}
</div>
<div className='select'>
<select value={value} onChange={onChange}>
{options.map((option) => (
<option value={option.value}>{option.label}</option>
))}
</select>
</div>
</Wrapper>
);
};
export default Select;
+223 -1
View File
@@ -1,3 +1,225 @@
import styled, { css } from 'styled-components';
export const Wrapper = styled.div``;
type WrapperProps = {
color?:
| 'client'
| 'productOwner'
| 'developer'
| 'admin'
| 'success'
| 'warning'
| 'error'
| 'black'
| 'gray'
| 'white';
error?: boolean;
errorMessage?: string;
type?: 'text' | 'email' | 'password' | 'file' | 'number';
label: string;
fullWidth?: boolean;
};
export const Wrapper = styled.div<WrapperProps>`
.select {
width: inherit;
height: inherit;
border-radius: 5px;
padding: 1rem;
position: relative;
color: ${({ theme }) => theme.colors.black.main};
background: ${({ theme }) => theme.colors.white.main};
background-clip: padding-box;
border: 2px solid transparent;
display: flex;
flex-direction: row;
align-items: center;
&:before {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: inherit;
height: inherit;
z-index: -1;
margin: -2px;
border-radius: inherit;
}
}
.info {
margin-bottom: 5px;
display: grid;
grid-template-columns: repeat(2, 1fr);
align-items: center;
p {
background: ${({ theme }) => theme.colors.gray.dark};
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.label {
justify-self: flex-start;
}
.error-message {
justify-self: flex-end;
}
}
select {
width: 100%;
background: none;
border: none;
color: ${({ theme }) => theme.colors.black.main};
background-image: url('../../assets/icons/chevron-down.svg');
}
.icon {
cursor: pointer;
display: inline-flex;
align-items: center;
}
.icon.left {
margin-right: 0.5rem;
}
${({ color, theme }) => {
switch (color) {
case 'client':
return css`
.select:before {
background: ${theme.colors.client.light};
}
.icon svg path {
stroke: ${theme.colors.client.main};
}
`;
case 'productOwner':
return css`
.select:before {
background: ${theme.colors.productOwner.light};
}
.icon svg path {
stroke: ${theme.colors.productOwner.main};
}
`;
case 'developer':
return css`
.select:before {
background: ${theme.colors.developer.light};
}
.icon svg path {
stroke: ${theme.colors.developer.main};
}
`;
case 'admin':
return css`
.select:before {
background: ${theme.colors.admin.light};
}
.icon svg path {
stroke: ${theme.colors.admin.main};
}
`;
case 'success':
return css`
.select:before {
background: ${theme.colors.success.main};
}
.icon svg path {
stroke: ${theme.colors.success.main};
}
`;
case 'warning':
return css`
.select:before {
background: ${theme.colors.warning.main};
}
.icon svg path {
stroke: ${theme.colors.warning.main};
}
`;
case 'error':
return css`
.select:before {
background: ${theme.colors.error.main};
}
.icon svg path {
stroke: ${theme.colors.error.main};
}
`;
case 'black':
return css`
.select:before {
background: ${theme.colors.black.main};
}
.icon svg path {
stroke: ${theme.colors.black.main};
}
`;
case 'white':
return css`
.select:before {
background: ${theme.colors.white.main};
}
.icon svg path {
stroke: ${theme.colors.white.main};
}
`;
default:
return css`
.select:before {
background: ${theme.colors.client.light};
}
.icon svg path {
stroke: ${theme.colors.client.main};
}
`;
}
}}
${({ error, theme }) =>
error &&
css`
.info p {
background: ${theme.colors.error.main};
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.select:before {
background: ${theme.colors.error.main};
}
.icon svg path {
stroke: ${theme.colors.error.main};
}
`}
${({ fullWidth }) =>
fullWidth &&
css`
width: 100%;
font-size: 1.25rem;
.icon svg {
width: 1.25rem;
height: 1.25rem;
}
`};
`;
+3 -1
View File
@@ -3,5 +3,7 @@ import Box from './Box';
import Text from './Text';
import Link from './Link';
import Input from './Input';
import TextArea from './TextArea';
import Select from './Select';
export { Button, Box, Text, Link, Input };
export { Button, Box, Text, Link, Input, TextArea, Select };