Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
feat: Add floating label to select (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire Hsu authored Sep 10, 2019
1 parent c5e680b commit d8f506a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Form/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const IconContainer = styled(Flex)`
const SelectInput = createComponent({
name: 'Select',
tag: 'select',
style: css`
style: ({ isFloating }) => css`
position: relative;
z-index: 2;
padding: 0 8px;
Expand All @@ -62,6 +62,10 @@ const SelectInput = createComponent({
background: transparent;
border: none;
-webkit-appearance: none;
${isFloating &&
css`
margin-top: 14px;
`};
`,
});

Expand Down Expand Up @@ -117,20 +121,28 @@ export class Select extends Component {
};

render() {
const { id, name, options, placeholder, error, size = 'md', label, ...props } = this.props;
const { id, name, options, placeholder, error, size = 'md', label, floating, ...props } = this.props;
const { value } = this.state;
const isFloating = floating && value != undefined && `${value}`.trim();
const FloatingLabel = (
<Label htmlFor={id} size={size} isFloating={isFloating} isFloatable={floating}>
{label}
</Label>
);

return (
<Field>
{label && <Label size={size}>{label}</Label>}
{label && !floating && <Label size={size}>{label}</Label>}
<SelectContainer value={value} size={size}>
{label && isFloating && FloatingLabel}
<SelectInput
{...props}
size={size}
ref={this.ref}
name={name}
value={value}
onChange={this.handleChange}
isFloating={isFloating}
onBlur={this.handleBlur}>
<SelectOption value="">{placeholder || 'Select an option...'}</SelectOption>
{options.map(option => (
Expand Down
2 changes: 2 additions & 0 deletions src/Form/Select.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ const defaultProps = {
};

export const Basic = () => <Select {...defaultProps} />;

export const FloatingLabel = () => <Select {...defaultProps} floating />;

0 comments on commit d8f506a

Please sign in to comment.