/
InlineControlGroup.tsx
105 lines (100 loc) · 3.61 KB
/
InlineControlGroup.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as React from 'react';
import {Box, safelySpreadBoxProps, type BoxProps, type BoxStyleProps} from '@twilio-paste/box';
import {Label} from '@twilio-paste/label';
import {HelpText} from '@twilio-paste/help-text';
import type {HTMLPasteProps} from '@twilio-paste/types';
export interface InlineControlGroupProps
extends Pick<BoxProps, 'element'>,
Omit<HTMLPasteProps<'fieldset'>, 'onChange'> {
children: React.ReactNode;
disabled?: boolean;
errorText?: string | React.ReactNode;
helpText?: string | React.ReactNode;
legend: string | NonNullable<React.ReactNode>;
orientation?: 'vertical' | 'horizontal';
required?: boolean;
i18nRequiredLabel?: string;
fieldStyleProps?: BoxStyleProps;
}
const InlineControlGroup = React.forwardRef<HTMLFieldSetElement, InlineControlGroupProps>(
(
{
children,
disabled,
element = 'INLINE_CONTROL_GROUP',
errorText,
helpText,
legend,
orientation = 'vertical',
required,
i18nRequiredLabel,
fieldStyleProps,
...props
},
ref
) => {
const isVisualPicker = Boolean(fieldStyleProps); // This prop is only used in Visual Picker
return (
<Box
{...safelySpreadBoxProps(props)}
as="fieldset"
element={element}
margin="space0"
padding="space0"
minWidth="0px"
borderWidth="borderWidth0"
ref={ref}
>
<Label
as="legend"
element="LEGEND"
htmlFor={undefined}
required={required}
marginBottom="space0"
disabled={disabled}
i18nRequiredLabel={i18nRequiredLabel}
>
{legend}
</Label>
{helpText && <HelpText marginTop="space0">{helpText}</HelpText>}
<Box element={`${element}_SET`} marginRight={isVisualPicker ? undefined : 'space20'}>
<Box
display={isVisualPicker && orientation === 'horizontal' ? 'inline-flex' : 'block'} // Sets equal heights for horizontal Visual Pickers
width={isVisualPicker ? '100%' : undefined} // Allows vertical Visual Pickers to take up the full width of the container
>
{React.Children.map(children, (child, index) => {
return (
<Box
element={`${element}_FIELD`}
display={orientation === 'horizontal' ? 'inline-block' : 'block'}
flexBasis={isVisualPicker ? '50%' : undefined} // Makes horizontal Visual Pickers grow to fill the width of the container
marginTop={
fieldStyleProps?.marginTop
? // eslint-disable-next-line unicorn/no-nested-ternary
index === 0 && orientation === 'vertical'
? 'space40'
: fieldStyleProps?.marginTop
: 'space40'
}
marginRight={orientation === 'horizontal' && !isVisualPicker ? 'space70' : null}
marginLeft={
isVisualPicker && orientation === 'horizontal' ? (index === 0 ? 'space0' : 'space30') : 'space0' // Sets spacing between horizontal Visual Pickers except for the first one
}
>
{child}
</Box>
);
})}
</Box>
{errorText && (
<Box element={`${element}_ERROR_TEXT_WRAPPER`} marginTop="space40">
<HelpText variant="error">{errorText}</HelpText>
</Box>
)}
</Box>
</Box>
);
}
);
InlineControlGroup.displayName = 'InlineControlGroup';
export {InlineControlGroup};