Skip to content

Commit

Permalink
fix: form control disabled states (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
eltongarbin committed Oct 1, 2020
1 parent c98b844 commit 9fd0bf0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
17 changes: 15 additions & 2 deletions packages/ocean-components/src/FormControl/FormControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export type FormControlProps = {
* @default false
*/
blocked?: boolean;
/**
* If true, should be displayed in a disabled state.
* @default false
*/
disabled?: boolean;
};

const FormControl: React.FC<FormControlProps> = ({
Expand All @@ -39,10 +44,17 @@ const FormControl: React.FC<FormControlProps> = ({
helperText,
error,
blocked,
disabled,
}) => (
<div className="ods-form-control__root">
{label && (
<label className="ods-form-control__label" htmlFor={htmlFor}>
<label
className={classNames(
'ods-form-control__label',
disabled && 'ods-form-control__label--disabled'
)}
htmlFor={htmlFor}
>
{label}
</label>
)}
Expand All @@ -58,7 +70,8 @@ const FormControl: React.FC<FormControlProps> = ({
<p
className={classNames(
'ods-form-control__helper-text',
error && 'ods-form-control__helper-text--error'
error && 'ods-form-control__helper-text--error',
disabled && 'ods-form-control__helper-text--disabled'
)}
>
{helperText}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,18 @@ test('renders a error state', () => {
'ods-form-control__helper-text ods-form-control__helper-text--error'
);
});

test('renders a disabled state', () => {
const { getByText } = setup({
disabled: true,
label: 'Label Test',
helperText: 'Error message.',
});

expect(getByText('Label Test').className).toBe(
'ods-form-control__label ods-form-control__label--disabled'
);
expect(getByText('Error message.').className).toBe(
'ods-form-control__helper-text ods-form-control__helper-text--disabled'
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ The API documentation of the FormControl React component. Learn more about the p

## CSS

| Global class | Description |
| --------------------------------------- | --------------------------------------------------------- |
| .ods-form-control\_\_root | Styles applied to the div element. |
| .ods-form-control\_\_label | Styles applied to the label element. |
| .ods-form-control\_\_element--blocked | Styles applied to the children element if blocked={true}. |
| .ods-form-control\_\_helper-text | Styles applied to the p element. |
| .ods-form-control\_\_helper-text--error | Styles applied to the p element if error={true}. |
| Global class | Description |
| ------------------------------------------ | --------------------------------------------------------- |
| .ods-form-control\_\_root | Styles applied to the div element. |
| .ods-form-control\_\_label | Styles applied to the label element. |
| .ods-form-control\_\_label--disabled | Styles applied to the label element if disabled={true}. |
| .ods-form-control\_\_element--blocked | Styles applied to the children element if blocked={true}. |
| .ods-form-control\_\_helper-text | Styles applied to the p element. |
| .ods-form-control\_\_helper-text--error | Styles applied to the p element if error={true}. |
| .ods-form-control\_\_helper-text--disabled | Styles applied to the p element if disabled={true}. |

If that's not sufficient, you can check the [implementation of the component](https://github.com/Pagnet/ocean-ds-web/blob/master/src/FormControl/FormControl.tsx) for more detail.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
line-height: $line-height-medium;
color: $color-interface-dark-down;
margin-bottom: $spacing-stack-xxs;

&--disabled {
color: $color-interface-dark-up;
}
}

&__element {
Expand All @@ -32,5 +36,9 @@
&--error {
color: $color-status-negative-pure;
}

&--disabled {
color: $color-interface-dark-up;
}
}
}
4 changes: 3 additions & 1 deletion packages/ocean-components/src/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type InputProps = {
React.ComponentPropsWithoutRef<'input'>;

const Input = React.forwardRef<HTMLInputElement, InputProps>(function Input(
{ type, className, label, helperText, blocked, error, id, ...rest },
{ type, className, label, helperText, blocked, error, id, disabled, ...rest },
ref
) {
return (
Expand All @@ -24,6 +24,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(function Input(
helperText={helperText}
error={error}
blocked={blocked}
disabled={disabled}
>
<input
ref={ref}
Expand All @@ -34,6 +35,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(function Input(
error && 'ods-input--error',
className
)}
disabled={disabled}
{...rest}
/>
</FormControl>
Expand Down
4 changes: 3 additions & 1 deletion packages/ocean-components/src/TextArea/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type TextAreaProps = Omit<FormControlProps, 'children'> &

const TextArea = React.forwardRef<HTMLTextAreaElement, TextAreaProps>(
function TextArea(
{ className, label, helperText, blocked, error, id, ...rest },
{ className, label, helperText, blocked, error, id, disabled, ...rest },
ref
) {
return (
Expand All @@ -19,6 +19,7 @@ const TextArea = React.forwardRef<HTMLTextAreaElement, TextAreaProps>(
helperText={helperText}
error={error}
blocked={blocked}
disabled={disabled}
>
<textarea
ref={ref}
Expand All @@ -28,6 +29,7 @@ const TextArea = React.forwardRef<HTMLTextAreaElement, TextAreaProps>(
error && 'ods-textarea--error',
className
)}
disabled={disabled}
{...rest}
/>
</FormControl>
Expand Down

0 comments on commit 9fd0bf0

Please sign in to comment.