/
InlineControlGroup.tsx
101 lines (96 loc) · 2.97 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
import * as React from 'react';
import * as PropTypes from 'prop-types';
import {Box, safelySpreadBoxProps} from '@twilio-paste/box';
import type {BoxProps} from '@twilio-paste/box';
import {Label} from '@twilio-paste/label';
import {HelpText} from '@twilio-paste/help-text';
export interface InlineControlGroupProps
extends Pick<BoxProps, 'element'>,
Omit<React.FieldsetHTMLAttributes<HTMLFieldSetElement>, 'onChange'> {
children: React.ReactNode;
disabled?: boolean;
errorText?: string | React.ReactNode;
helpText?: string | React.ReactNode;
legend: string | NonNullable<React.ReactNode>;
orientation?: 'vertical' | 'horizontal';
ref?: any;
required?: boolean;
i18nRequiredLabel?: string;
}
const InlineControlGroup = React.forwardRef<HTMLFieldSetElement, InlineControlGroupProps>(
(
{
children,
disabled,
element = 'INLINE_CONTROL_GROUP',
errorText,
helpText,
legend,
orientation = 'vertical',
required,
i18nRequiredLabel,
...props
},
ref
) => {
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`} marginLeft="space20" marginRight="space20">
{React.Children.map(children, (child) => {
return (
<Box
element={`${element}_FIELD`}
display={orientation === 'horizontal' ? 'inline-block' : 'block'}
marginTop="space40"
marginRight={orientation === 'horizontal' ? 'space70' : null}
>
{child}
</Box>
);
})}
{errorText && (
<Box element={`${element}_ERROR_TEXT_WRAPPER`} marginTop="space40">
<HelpText variant="error">{errorText}</HelpText>
</Box>
)}
</Box>
</Box>
);
}
);
InlineControlGroup.displayName = 'InlineControlGroup';
if (process.env.NODE_ENV === 'development') {
InlineControlGroup.propTypes = {
children: PropTypes.node.isRequired,
disabled: PropTypes.bool,
errorText: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
helpText: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
legend: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired,
name: PropTypes.string.isRequired,
orientation: PropTypes.oneOf(['vertical', 'horizontal']),
required: PropTypes.bool,
i18nRequiredLabel: PropTypes.string,
};
}
export {InlineControlGroup};