forked from carbon-design-system/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TileGroup.tsx
185 lines (157 loc) · 4.64 KB
/
TileGroup.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import PropTypes, { ReactNodeLike } from 'prop-types';
import React, { useState } from 'react';
import RadioTile from '../RadioTile';
import { usePrefix } from '../../internal/usePrefix';
import { ReactAttr } from '../../types/common';
import { noopFn } from '../../internal/noopFn';
type ExcludedAttributes = 'onChange';
export interface TileGroupProps
extends Omit<ReactAttr<HTMLFieldSetElement>, ExcludedAttributes> {
/**
* Provide a collection of <RadioTile> components to render in the group
*/
children?: ReactNodeLike;
/**
* Provide an optional className to be applied to the container node
*/
className?: string;
/**
* Specify the the value of <RadioTile> to be selected by default
*/
defaultSelected?: string | number;
/**
* Specify whether the group is disabled
*/
disabled?: boolean;
/**
* Provide an optional legend for this group
*/
legend?: string;
/**
* Specify the name of the underlying `<input>` nodes
*/
name: string;
/**
* Provide an optional `onChange` hook that is called whenever the value of the group changes
*/
onChange?: (selection: unknown, name: string, evt: unknown) => void;
/**
* Specify the value that is currently selected in the group
*/
valueSelected?: string | number;
}
const TileGroup = (props) => {
const {
children,
className,
defaultSelected,
disabled,
legend,
name,
onChange = noopFn,
valueSelected,
} = props;
const prefix = usePrefix();
const [selected, setSelected] = useState(valueSelected ?? defaultSelected);
const [prevValueSelected, setPrevValueSelected] = useState(valueSelected);
/**
* prop + state alignment - getDerivedStateFromProps
* only update if selected prop changes
*/
if (valueSelected !== prevValueSelected) {
setSelected(valueSelected);
setPrevValueSelected(valueSelected);
}
const getRadioTilesWithWrappers = (children) => {
const traverseAndModifyChildren = (children) => {
return React.Children.map(children, (child) => {
// If RadioTile found, return it with necessary props
if (child.type === RadioTile) {
const { value, ...otherProps } = child.props;
return (
<RadioTile
{...otherProps}
name={name}
key={value}
value={value}
onChange={handleChange}
checked={value === selected}
/>
);
} else if (child.props && child.props.children) {
// If the child is not RadioTile and has children, recheck the children
return React.cloneElement(child, {
...child.props,
children: traverseAndModifyChildren(child.props.children),
});
} else {
// If the child is neither a RadioTile nor has children, return it as is
return child;
}
});
};
return <>{traverseAndModifyChildren(children)}</>;
};
const handleChange = (newSelection, value, evt) => {
if (newSelection !== selected) {
setSelected(newSelection);
onChange(newSelection, name, evt);
}
};
const renderLegend = (legend) => {
if (legend) {
return <legend className={`${prefix}--label`}>{legend}</legend>;
}
};
return (
<fieldset
className={className ?? `${prefix}--tile-group`}
disabled={disabled}>
{renderLegend(legend)}
<div>{getRadioTilesWithWrappers(children)}</div>
</fieldset>
);
};
TileGroup.propTypes = {
/**
* Provide a collection of <RadioTile> components to render in the group
*/
children: PropTypes.node,
/**
* Provide an optional className to be applied to the container node
*/
className: PropTypes.string,
/**
* Specify the the value of <RadioTile> to be selected by default
*/
defaultSelected: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* Specify whether the group is disabled
*/
disabled: PropTypes.bool,
/**
* Provide an optional legend for this group
*/
legend: PropTypes.string,
/**
* Specify the name of the underlying `<input>` nodes
*/
name: PropTypes.string.isRequired,
/**
* Provide an optional `onChange` hook that is called whenever the value of
* the group changes
*/
onChange: PropTypes.func,
/**
* Specify the value that is currently selected in the group
*/
valueSelected: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};
TileGroup.displayName = 'TileGroup';
export default TileGroup;