Skip to content

Commit

Permalink
Improve styling (#247)
Browse files Browse the repository at this point in the history
* Toggle using proper font for its label

* Improve Select's styling and behaviour with blank option/placeholder
  • Loading branch information
ganlhi authored Oct 4, 2022
1 parent 9844b1a commit 4906485
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
31 changes: 26 additions & 5 deletions src/components/Select/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
import React from 'react';
import React, { useCallback, useState } from 'react';
import PropTypes from 'prop-types';

import styled from 'styled-components';
import Icon from '../Icon';
import FormElementWrapper from '../FormComponents/FormElementWrapper';
import FormElement from '../FormComponents/FormElement';
import { theme } from '../../theme';

const FormSelectElement = styled(FormElement)`
color: ${props => (props.isEmpty ? theme.color_v3.type.light : 'inherit')};
`;

const Select = React.forwardRef(
({ id, options, placeholder, ...props }, ref) => {
const [isEmpty, setIsEmpty] = useState(!props.defaultValue);

const onChange = useCallback(
e => {
setIsEmpty(!e.target.value);
if (typeof props.onChange === 'function') props.onChange(e);
},
[props],
);

return (
<FormElementWrapper invalid={props.invalid} valid={props.valid}>
<FormElement
<FormSelectElement
as='select'
id={id}
name={id}
ref={ref}
withIcon
{...props}
onChange={onChange}
defaultValue={props.defaultValue || ''}
isEmpty={isEmpty}
>
{placeholder && <option value=''>{placeholder}</option>}
<option disabled value='' hidden={!placeholder}>
{placeholder || ''}
</option>
{options.map(({ label, value, disabled = false }) => (
<option key={value} value={value} disabled={disabled}>
{label}
</option>
))}
</FormElement>
</FormSelectElement>
{props.valid ? (
<Icon icon={Icon.ICONS.IconCheck} size={Icon.SIZES.L} />
) : props.invalid ? (
Expand Down Expand Up @@ -55,6 +75,7 @@ Select.propTypes = {
),
placeholder: PropTypes.string,
valid: PropTypes.bool,
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};

Select.defaultProps = {
Expand Down
1 change: 1 addition & 0 deletions src/components/Toggle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const Toggle = React.forwardRef(
{(children || helper) && (
<ToggleLabels
className='f-Toggle-labels'
as='label'
{...labelProps}
disabled={disabled}
>
Expand Down
6 changes: 5 additions & 1 deletion src/components/Toggle/styles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from 'styled-components/macro';
import FormElementWrapper from '../FormComponents/FormElementWrapper';
import { theme } from '../../theme';
import UiText from '../UiText';

const styles = {
toggle: {
Expand Down Expand Up @@ -93,7 +94,10 @@ export const ToggleBulletLabelOn = styled(ToggleBulletLabel)`
visibility: ${({ isOn }) => (isOn ? 'visible' : 'hidden')};
`;

export const ToggleLabels = styled('span')`
export const ToggleLabels = styled(UiText).attrs({
variant: UiText.VARIANTS.content,
})`
margin: 0;
margin-left: ${theme.space.l};
cursor: ${({ disabled }) => disabled && 'not-allowed'};
`;
4 changes: 4 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,7 @@ a:hover {
img {
max-width: 100%;
}

::placeholder {
color: var(--f-color-type-light);
}

0 comments on commit 4906485

Please sign in to comment.