Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit b66367b

Browse files
Anton Bilovusboilund
authored andcommitted
fix(utils): handle disabled label / unit label for *Field components
Change opacity and disable pointer events for the `*Field` components label / unit label when the components are disabled. Fixes: #193
1 parent 2dce76c commit b66367b

File tree

5 files changed

+55
-18
lines changed

5 files changed

+55
-18
lines changed

packages/core/src/Input/__snapshots__/index.test.tsx.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4678,6 +4678,7 @@ exports[`TextInput TimeInput 1`] = `
46784678
<div>
46794679
<div
46804680
className="c24"
4681+
disabled={false}
46814682
>
46824683
<span
46834684
className="c25"
@@ -4716,6 +4717,7 @@ exports[`TextInput TimeInput 1`] = `
47164717
</div>
47174718
<span
47184719
className="c22 c27"
4720+
disabled={false}
47194721
>
47204722
MM
47214723
</span>
@@ -4724,6 +4726,7 @@ exports[`TextInput TimeInput 1`] = `
47244726
<div>
47254727
<div
47264728
className="c24"
4729+
disabled={false}
47274730
>
47284731
<span
47294732
className="c25"
@@ -4762,6 +4765,7 @@ exports[`TextInput TimeInput 1`] = `
47624765
</div>
47634766
<span
47644767
className="c22 c27"
4768+
disabled={false}
47654769
>
47664770
MM
47674771
</span>

packages/core/src/RadioButton/__snapshots__/index.test.tsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3094,6 +3094,7 @@ exports[`RadioButtons RadioIconGroup 1`] = `
30943094
<div>
30953095
<div
30963096
className="c2"
3097+
disabled={false}
30973098
>
30983099
<span
30993100
className="c3"

packages/core/src/Slider/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,14 +597,16 @@ export const SliderField: React.FC<
597597
> = ({ label, unitLabel, ...props }) => (
598598
<div>
599599
{label !== undefined ? (
600-
<SliderLabel compact={false}>
600+
<SliderLabel compact={false} disabled={props.disabled ?? false}>
601601
<Typography variant="navigation-label">{label}</Typography>
602602
</SliderLabel>
603603
) : null}
604604
{unitLabel !== undefined ? (
605605
<WithUnitLabelContainer>
606606
<Slider {...props} />
607-
<Unit variant="explanatory-text">{unitLabel}</Unit>
607+
<Unit variant="explanatory-text" disabled={props.disabled ?? false}>
608+
{unitLabel}
609+
</Unit>
608610
</WithUnitLabelContainer>
609611
) : (
610612
<Slider {...props} />

packages/core/src/ToggleButtonGroup/__snapshots__/index.test.tsx.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ exports[`ToggleButtonGroup ToggleButtonGroupWithField 1`] = `
452452
<div>
453453
<div
454454
className="c2"
455+
disabled={false}
455456
>
456457
<span
457458
className="c3"
@@ -681,6 +682,7 @@ exports[`ToggleButtonGroup ToggleButtonGroupWithFieldExclusive 1`] = `
681682
<div>
682683
<div
683684
className="c2"
685+
disabled={false}
684686
>
685687
<span
686688
className="c3"

packages/core/src/utils/withField.tsx

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import React from 'react'
2-
import styled, { useTheme } from 'styled-components'
3-
import { Typography } from '../Typography'
4-
import { componentSize, spacing } from '../designparams'
2+
import styled, { useTheme, css } from 'styled-components'
53

6-
export interface FieldProps {
7-
readonly label?: string
8-
/**
9-
* Override theme's default setting for `compact` if set.
4+
import { Typography } from '../Typography'
5+
import { componentSize, spacing, opacity } from '../designparams'
106

11-
*/
12-
readonly compact?: boolean
13-
readonly unitLabel?: string
7+
interface LabelProps {
8+
readonly compact: boolean
9+
readonly disabled: boolean
1410
}
1511

16-
export const Label = styled.div<{ readonly compact: boolean }>`
12+
export const Label = styled.div<LabelProps>`
1713
height: ${({ compact }) =>
1814
!compact ? componentSize.medium : componentSize.small};
1915
display: flex;
2016
align-items: center;
2117
color: ${({ theme }) => theme.color.text03()};
2218
cursor: default;
19+
20+
${({ disabled }) =>
21+
disabled
22+
? css`
23+
opacity: ${opacity[48]};
24+
pointer-events: none;
25+
`
26+
: undefined};
2327
`
2428

2529
export const WithUnitLabelContainer = styled.div`
@@ -28,31 +32,55 @@ export const WithUnitLabelContainer = styled.div`
2832
align-items: center;
2933
`
3034

31-
export const Unit = styled(Typography)`
35+
export const Unit = styled(Typography)<{
36+
readonly disabled: boolean
37+
}>`
3238
color: ${({ theme }) => theme.color.text03()};
3339
margin-left: ${spacing.medium};
3440
flex-shrink: 0;
41+
42+
${({ disabled }) =>
43+
disabled
44+
? css`
45+
opacity: ${opacity[48]};
46+
pointer-events: none;
47+
`
48+
: undefined};
3549
`
3650

51+
export interface FieldProps {
52+
readonly label?: string
53+
readonly disabled?: boolean
54+
/**
55+
* Override theme's default setting for `compact` if set.
56+
57+
*/
58+
readonly compact?: boolean
59+
readonly unitLabel?: string
60+
}
61+
3762
export function withField<T>(
38-
InputComponent: React.FunctionComponent<T>
39-
): React.FunctionComponent<FieldProps & T> {
63+
InputComponent: React.FC<T>
64+
): React.FC<FieldProps & T> {
4065
// eslint-disable-next-line react/display-name
4166
return ({ label, unitLabel, ...props }) => {
4267
const { compact: compactFromTheme } = useTheme()
4368
const compact = props.compact ?? compactFromTheme
69+
const disabled = props.disabled ?? false
4470

4571
return (
4672
<div>
4773
{label !== undefined ? (
48-
<Label compact={compact}>
74+
<Label compact={compact} disabled={disabled}>
4975
<Typography variant="navigation-label">{label}</Typography>
5076
</Label>
5177
) : null}
5278
{unitLabel !== undefined ? (
5379
<WithUnitLabelContainer>
5480
<InputComponent {...(props as T)} />
55-
<Unit variant="explanatory-text">{unitLabel}</Unit>
81+
<Unit variant="explanatory-text" disabled={disabled}>
82+
{unitLabel}
83+
</Unit>
5684
</WithUnitLabelContainer>
5785
) : (
5886
<InputComponent {...(props as T)} />

0 commit comments

Comments
 (0)