Skip to content

Commit

Permalink
Merge pull request #19 from dennispassway/feature/custom-type-select
Browse files Browse the repository at this point in the history
add select component
  • Loading branch information
dennispassway authored Feb 3, 2021
2 parents 39b833c + 5f5853b commit 0ee7ac3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,18 @@ Example with asset fields listed and custom fields added:
"name": "location",
"label": "Location",
"type": "location"
}
},
{
"label": "Copyright",
"name": "copyright",
"placeholder": "pick one…",
"type": "select",
"options": [
{ "title": "Copyright", "value": "copyright" },
{ "title": "Public Domain", "value": "public-domain" },
{ "title": "Creative Commons", "value": "creative-commons" }
]
},
],
}
```
Expand Down
65 changes: 65 additions & 0 deletions src/components/LabelWithInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Props {
| 'email'
| 'location'
| 'number'
| 'select'
| 'tel'
| 'text'
| 'time'
Expand Down Expand Up @@ -84,17 +85,51 @@ const StyledTextArea = styled.textarea`
width: 100%;
`;

const StyledSelectWrapper = styled.div`
position: relative;
`;

const StyledSelectArrow = styled.span`
background-color: ${({ theme }) => theme.inputTextColor};
bottom: 0;
clip-path: polygon(100% 0%, 0 0%, 50% 100%);
content: '';
height: 0.5em;
pointer-events: none;
position: absolute;
right: 16px;
top: 50%;
transform: translateY(-50%);
width: 0.8em;
`;

const StyledSelect = styled.select`
appearance: none;
background-color: ${({ theme }) => theme.inputBackgroundColor};
border-radius: ${({ theme }) => theme.appBorderRadius};
border: 0;
color: ${({ theme }) => theme.inputTextColor};
font-family: ${({ theme }) => theme.appFontFamily};
font-size: 16px 16px 34px 16px;
line-height: 1.1;
outline: 0;
padding: 16px;
width: 100%;
`;

export const LabelWithInput = ({ label, onChange, type = 'text', ...rest }: Props) => {
const elementsMap: { [key: string]: any } = {
checkbox: Checkbox,
location: Location,
textArea: StyledTextArea,
select: Select,
};

const onChangeMap: { [key: string]: (e: any) => void } = {
checkbox: (e: ChangeEvent<HTMLInputElement>) => onChange(e.target.checked),
location: (value: Geopoint | undefined) => onChange(value),
number: (e: ChangeEvent<HTMLInputElement>) => onChange(parseFloat(e.target.value)),
select: (e: ChangeEvent<HTMLSelectElement>) => onChange(e.target.value),
};

const defaultOnChange = (e: ChangeEvent<HTMLInputElement>) => onChange(e.target.value);
Expand Down Expand Up @@ -140,3 +175,33 @@ const Location = ({
</StyledContainer>
);
};

type SelectOption = {
title: string;
value: string;
};

const Select = ({
options,
placeholder,
value,
...rest
}: {
options: Array<SelectOption>;
placeholder: string | undefined;
value: string | undefined;
}) => {
return (
<StyledSelectWrapper>
<StyledSelect {...rest}>
<option value="">{placeholder}</option>
{options?.map((option: SelectOption) => (
<option key={option.value} value={option.value} selected={option.value === value}>
{option.title}
</option>
))}
</StyledSelect>
<StyledSelectArrow />
</StyledSelectWrapper>
);
};

0 comments on commit 0ee7ac3

Please sign in to comment.