ywana-core8 0.1.75 → 0.1.77
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ACCORDION_EVALUATION.md +583 -0
- package/CHECKBOX_EVALUATION.md +273 -0
- package/CHIP_EVALUATION.md +542 -0
- package/COLOR_EVALUATION.md +524 -0
- package/COMPONENTS_EVALUATION.md +477 -0
- package/FORM_EVALUATION.md +459 -0
- package/HEADER_EVALUATION.md +436 -0
- package/ICON_EVALUATION.md +254 -0
- package/LIST_EVALUATION.md +574 -0
- package/PROGRESS_EVALUATION.md +450 -0
- package/RADIO_EVALUATION.md +439 -0
- package/RADIO_VISUAL_FIX.md +183 -0
- package/SECTION_IMPROVEMENTS.md +153 -0
- package/SWITCH_EVALUATION.md +335 -0
- package/SWITCH_VISUAL_FIX.md +232 -0
- package/TAB_EVALUATION.md +626 -0
- package/TEXTFIELD_EVALUATION.md +747 -0
- package/TOOLTIP_FIX.md +157 -0
- package/TREE_EVALUATION.md +708 -0
- package/dist/index.cjs +10893 -1969
- package/dist/index.cjs.map +1 -1
- package/dist/index.css +7768 -1096
- package/dist/index.css.map +1 -1
- package/dist/index.modern.js +10921 -2005
- package/dist/index.modern.js.map +1 -1
- package/dist/index.umd.js +10893 -1969
- package/dist/index.umd.js.map +1 -1
- package/jest.config.js +24 -0
- package/package.json +10 -1
- package/src/html/accordion.css +208 -4
- package/src/html/accordion.example.js +390 -0
- package/src/html/accordion.js +284 -28
- package/src/html/accordion.unit.test.js +334 -0
- package/src/html/button.css +157 -16
- package/src/html/button.example.js +374 -0
- package/src/html/button.js +240 -60
- package/src/html/button.test.js +422 -0
- package/src/html/checkbox.css +74 -2
- package/src/html/checkbox.example.js +316 -0
- package/src/html/checkbox.js +113 -26
- package/src/html/checkbox.test.js +285 -0
- package/src/html/chip.css +230 -19
- package/src/html/chip.example.js +355 -0
- package/src/html/chip.js +321 -25
- package/src/html/chip.test.js +425 -0
- package/src/html/color.css +435 -6
- package/src/html/color.example.js +527 -0
- package/src/html/color.js +458 -9
- package/src/html/color.test.js +362 -4
- package/src/html/components.example.js +492 -0
- package/src/html/components_enhanced.test.js +581 -0
- package/src/html/form.css +70 -3
- package/src/html/form.example.js +385 -0
- package/src/html/form.js +232 -34
- package/src/html/form.test.js +369 -0
- package/src/html/header2.css +264 -0
- package/src/html/header2.example.js +411 -0
- package/src/html/header2.js +203 -0
- package/src/html/header2.test.js +377 -0
- package/src/html/icon.css +20 -2
- package/src/html/icon.example.js +268 -0
- package/src/html/icon.js +86 -16
- package/src/html/icon.test.js +231 -0
- package/src/html/index.js +4 -1
- package/src/html/list.css +393 -1
- package/src/html/list.example.js +404 -0
- package/src/html/list.js +583 -40
- package/src/html/list.test.js +383 -0
- package/src/html/progress.css +707 -17
- package/src/html/progress.example.js +424 -0
- package/src/html/progress.js +906 -9
- package/src/html/progress.test.js +313 -0
- package/src/html/property.css +399 -0
- package/src/html/property.example.js +553 -0
- package/src/html/property.js +393 -15
- package/src/html/property.test.js +351 -2
- package/src/html/radio-visual-test.js +289 -0
- package/src/html/radio.css +137 -11
- package/src/html/radio.example.js +389 -0
- package/src/html/radio.js +234 -10
- package/src/html/radio.test.js +318 -0
- package/src/html/section.example.js +99 -0
- package/src/html/section.js +40 -3
- package/src/html/section.test.js +131 -0
- package/src/html/selector.css +329 -3
- package/src/html/selector.js +369 -23
- package/src/html/switch-debug.js +197 -0
- package/src/html/switch-test-visual.js +294 -0
- package/src/html/switch.css +200 -0
- package/src/html/switch.example.js +461 -0
- package/src/html/switch.js +283 -23
- package/src/html/switch.test.js +355 -0
- package/src/html/tab.css +289 -0
- package/src/html/tab.example.js +446 -0
- package/src/html/tab.js +387 -22
- package/src/html/tab_enhanced.js +378 -0
- package/src/html/tab_enhanced.test.js +504 -0
- package/src/html/table2.css +576 -0
- package/src/html/table2.example.js +703 -0
- package/src/html/table2.js +1252 -0
- package/src/html/table2.migration.md +328 -0
- package/src/html/table2.test.js +582 -0
- package/src/html/text.css +375 -0
- package/src/html/text.js +311 -20
- package/src/html/textfield2.css +841 -0
- package/src/html/textfield2.example.js +1370 -0
- package/src/html/textfield2.js +1143 -0
- package/src/html/textfield2.test.js +950 -0
- package/src/html/thumbnail.css +289 -2
- package/src/html/thumbnail.js +214 -9
- package/src/html/tokenfield.css +449 -1
- package/src/html/tokenfield.example.js +503 -0
- package/src/html/tokenfield.js +561 -56
- package/src/html/tokenfield.test.js +423 -0
- package/src/html/tooltip-positioning-demo.js +187 -0
- package/src/html/tooltip.css +25 -2
- package/src/html/tree.css +240 -10
- package/src/html/tree.example.js +475 -0
- package/src/html/tree.js +714 -28
- package/src/html/tree_enhanced.test.js +495 -0
- package/table2.test.js +454 -0
- package/src/html/button.tsx +0 -38
package/src/html/radio.js
CHANGED
@@ -1,25 +1,249 @@
|
|
1
|
-
import React from 'react'
|
2
|
-
import
|
1
|
+
import React, { useState, useEffect } from 'react'
|
2
|
+
import PropTypes from 'prop-types'
|
3
|
+
import { Text } from './text'
|
3
4
|
import './radio.css'
|
4
5
|
|
5
6
|
/**
|
6
|
-
*
|
7
|
+
* RadioButton component for single selection within a group
|
7
8
|
*/
|
8
9
|
export const RadioButton = (props) => {
|
10
|
+
const {
|
11
|
+
id,
|
12
|
+
name,
|
13
|
+
label,
|
14
|
+
value,
|
15
|
+
checked = false,
|
16
|
+
disabled = false,
|
17
|
+
readOnly = false,
|
18
|
+
error = false,
|
19
|
+
required = false,
|
20
|
+
className,
|
21
|
+
ariaLabel,
|
22
|
+
onChange
|
23
|
+
} = props
|
9
24
|
|
10
|
-
|
25
|
+
// Validate required props
|
26
|
+
if (!id) {
|
27
|
+
console.warn('RadioButton component: id prop is required')
|
28
|
+
return null
|
29
|
+
}
|
30
|
+
|
31
|
+
if (!name) {
|
32
|
+
console.warn('RadioButton component: name prop is required for radio button groups')
|
33
|
+
}
|
11
34
|
|
12
35
|
function change(event) {
|
13
|
-
|
14
|
-
|
36
|
+
if (!disabled && !readOnly && onChange) {
|
37
|
+
onChange(id, value || id, event)
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
// Handle keyboard events for accessibility
|
42
|
+
const handleKeyDown = (event) => {
|
43
|
+
if (event.key === ' ') {
|
44
|
+
event.preventDefault()
|
45
|
+
change(event)
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
const safeClassName = className || ''
|
50
|
+
const labelTxt = label && <Text>{label}</Text>
|
51
|
+
|
52
|
+
// Generate CSS classes
|
53
|
+
const cssClasses = [
|
54
|
+
'radio',
|
55
|
+
disabled && 'disabled',
|
56
|
+
readOnly && 'readonly',
|
57
|
+
error && 'error',
|
58
|
+
safeClassName
|
59
|
+
].filter(Boolean).join(' ')
|
60
|
+
|
61
|
+
// Accessibility attributes
|
62
|
+
const ariaAttributes = {
|
63
|
+
'aria-label': ariaLabel || label,
|
64
|
+
'aria-required': required,
|
65
|
+
'aria-invalid': error,
|
66
|
+
'aria-describedby': error && typeof error === 'string' ? `${id}-error` : undefined
|
15
67
|
}
|
16
68
|
|
17
|
-
const labelTxt = <Text>{label}</Text>
|
18
69
|
return (
|
19
|
-
<div className=
|
20
|
-
<input
|
21
|
-
|
70
|
+
<div className={cssClasses} onKeyDown={handleKeyDown} tabIndex={disabled ? -1 : 0}>
|
71
|
+
<input
|
72
|
+
id={id}
|
73
|
+
name={name}
|
74
|
+
type="radio"
|
75
|
+
value={value || id}
|
76
|
+
checked={checked}
|
77
|
+
onChange={change}
|
78
|
+
disabled={disabled || readOnly}
|
79
|
+
required={required}
|
80
|
+
{...ariaAttributes}
|
81
|
+
/>
|
82
|
+
<span className="checkmark" />
|
22
83
|
<label htmlFor={id}>{labelTxt}</label>
|
84
|
+
{error && typeof error === 'string' && error.length > 0 && (
|
85
|
+
<span id={`${id}-error`} className="error-message" role="alert">
|
86
|
+
{error}
|
87
|
+
</span>
|
88
|
+
)}
|
23
89
|
</div>
|
24
90
|
)
|
25
91
|
}
|
92
|
+
|
93
|
+
/**
|
94
|
+
* RadioGroup component for managing a group of radio buttons
|
95
|
+
*/
|
96
|
+
export const RadioGroup = (props) => {
|
97
|
+
const {
|
98
|
+
id,
|
99
|
+
label,
|
100
|
+
value,
|
101
|
+
options = [],
|
102
|
+
disabled = false,
|
103
|
+
readOnly = false,
|
104
|
+
error = false,
|
105
|
+
required = false,
|
106
|
+
className,
|
107
|
+
ariaLabel,
|
108
|
+
onChange
|
109
|
+
} = props
|
110
|
+
|
111
|
+
const [selectedValue, setSelectedValue] = useState(value)
|
112
|
+
|
113
|
+
// Sync with external value
|
114
|
+
useEffect(() => {
|
115
|
+
setSelectedValue(value)
|
116
|
+
}, [value])
|
117
|
+
|
118
|
+
const handleChange = (_, optionValue, event) => {
|
119
|
+
if (!disabled && !readOnly) {
|
120
|
+
setSelectedValue(optionValue)
|
121
|
+
if (onChange) {
|
122
|
+
onChange(id || 'radiogroup', optionValue, event)
|
123
|
+
}
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
const safeClassName = className || ''
|
128
|
+
const groupName = id || `radiogroup-${Math.random().toString(36).substring(2, 11)}`
|
129
|
+
|
130
|
+
// Generate CSS classes
|
131
|
+
const cssClasses = [
|
132
|
+
'radio-group',
|
133
|
+
disabled && 'disabled',
|
134
|
+
readOnly && 'readonly',
|
135
|
+
error && 'error',
|
136
|
+
safeClassName
|
137
|
+
].filter(Boolean).join(' ')
|
138
|
+
|
139
|
+
return (
|
140
|
+
<div className={cssClasses} role="radiogroup" aria-label={ariaLabel || label}>
|
141
|
+
{label && (
|
142
|
+
<label className="radio-group-label">
|
143
|
+
<Text>{label}</Text>
|
144
|
+
{required && <span className="required-indicator" aria-label="required">*</span>}
|
145
|
+
</label>
|
146
|
+
)}
|
147
|
+
<div className="radio-group-options">
|
148
|
+
{options.map((option, index) => (
|
149
|
+
<RadioButton
|
150
|
+
key={option.value || option.id || index}
|
151
|
+
id={option.id || `${groupName}-${index}`}
|
152
|
+
name={groupName}
|
153
|
+
label={option.label}
|
154
|
+
value={option.value || option.id}
|
155
|
+
checked={selectedValue === (option.value || option.id)}
|
156
|
+
disabled={disabled || option.disabled}
|
157
|
+
readOnly={readOnly}
|
158
|
+
error={error}
|
159
|
+
onChange={handleChange}
|
160
|
+
ariaLabel={option.ariaLabel}
|
161
|
+
/>
|
162
|
+
))}
|
163
|
+
</div>
|
164
|
+
{error && typeof error === 'string' && error.length > 0 && (
|
165
|
+
<span id={`${id}-error`} className="error-message" role="alert">
|
166
|
+
{error}
|
167
|
+
</span>
|
168
|
+
)}
|
169
|
+
</div>
|
170
|
+
)
|
171
|
+
}
|
172
|
+
|
173
|
+
// PropTypes para RadioButton
|
174
|
+
RadioButton.propTypes = {
|
175
|
+
/** Unique identifier for the radio button (required) */
|
176
|
+
id: PropTypes.string.isRequired,
|
177
|
+
/** Name attribute for grouping radio buttons (required) */
|
178
|
+
name: PropTypes.string.isRequired,
|
179
|
+
/** Label text for the radio button */
|
180
|
+
label: PropTypes.string,
|
181
|
+
/** Value of the radio button */
|
182
|
+
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
183
|
+
/** Whether the radio button is checked */
|
184
|
+
checked: PropTypes.bool,
|
185
|
+
/** Whether the radio button is disabled */
|
186
|
+
disabled: PropTypes.bool,
|
187
|
+
/** Whether the radio button is read-only */
|
188
|
+
readOnly: PropTypes.bool,
|
189
|
+
/** Error state - boolean or error message string */
|
190
|
+
error: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
|
191
|
+
/** Whether the radio button is required */
|
192
|
+
required: PropTypes.bool,
|
193
|
+
/** Additional CSS classes */
|
194
|
+
className: PropTypes.string,
|
195
|
+
/** Accessibility label for screen readers */
|
196
|
+
ariaLabel: PropTypes.string,
|
197
|
+
/** Change handler function */
|
198
|
+
onChange: PropTypes.func
|
199
|
+
}
|
200
|
+
|
201
|
+
RadioButton.defaultProps = {
|
202
|
+
checked: false,
|
203
|
+
disabled: false,
|
204
|
+
readOnly: false,
|
205
|
+
error: false,
|
206
|
+
required: false,
|
207
|
+
className: ''
|
208
|
+
}
|
209
|
+
|
210
|
+
// PropTypes para RadioGroup
|
211
|
+
RadioGroup.propTypes = {
|
212
|
+
/** Unique identifier for the radio group */
|
213
|
+
id: PropTypes.string,
|
214
|
+
/** Label text for the radio group */
|
215
|
+
label: PropTypes.string,
|
216
|
+
/** Currently selected value */
|
217
|
+
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
218
|
+
/** Array of radio button options */
|
219
|
+
options: PropTypes.arrayOf(PropTypes.shape({
|
220
|
+
id: PropTypes.string,
|
221
|
+
label: PropTypes.string.isRequired,
|
222
|
+
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
223
|
+
disabled: PropTypes.bool,
|
224
|
+
ariaLabel: PropTypes.string
|
225
|
+
})),
|
226
|
+
/** Whether the entire group is disabled */
|
227
|
+
disabled: PropTypes.bool,
|
228
|
+
/** Whether the entire group is read-only */
|
229
|
+
readOnly: PropTypes.bool,
|
230
|
+
/** Error state - boolean or error message string */
|
231
|
+
error: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
|
232
|
+
/** Whether the group selection is required */
|
233
|
+
required: PropTypes.bool,
|
234
|
+
/** Additional CSS classes */
|
235
|
+
className: PropTypes.string,
|
236
|
+
/** Accessibility label for screen readers */
|
237
|
+
ariaLabel: PropTypes.string,
|
238
|
+
/** Change handler function */
|
239
|
+
onChange: PropTypes.func
|
240
|
+
}
|
241
|
+
|
242
|
+
RadioGroup.defaultProps = {
|
243
|
+
options: [],
|
244
|
+
disabled: false,
|
245
|
+
readOnly: false,
|
246
|
+
error: false,
|
247
|
+
required: false,
|
248
|
+
className: ''
|
249
|
+
}
|
@@ -0,0 +1,318 @@
|
|
1
|
+
import React from 'react'
|
2
|
+
import { RadioButton, RadioGroup } from './radio'
|
3
|
+
|
4
|
+
// Pruebas unitarias para los componentes Radio
|
5
|
+
describe('Radio Components', () => {
|
6
|
+
// Mock del componente Text
|
7
|
+
const mockText = jest.fn()
|
8
|
+
|
9
|
+
beforeEach(() => {
|
10
|
+
jest.clearAllMocks()
|
11
|
+
|
12
|
+
// Mock del componente Text
|
13
|
+
jest.doMock('./text', () => ({
|
14
|
+
Text: mockText
|
15
|
+
}))
|
16
|
+
|
17
|
+
// Mock de console.warn para las pruebas
|
18
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {})
|
19
|
+
})
|
20
|
+
|
21
|
+
afterEach(() => {
|
22
|
+
console.warn.mockRestore()
|
23
|
+
})
|
24
|
+
|
25
|
+
describe('RadioButton Component', () => {
|
26
|
+
test('component exports correctly', () => {
|
27
|
+
expect(RadioButton).toBeDefined()
|
28
|
+
expect(typeof RadioButton).toBe('function')
|
29
|
+
})
|
30
|
+
|
31
|
+
test('component has correct PropTypes', () => {
|
32
|
+
expect(RadioButton.propTypes).toBeDefined()
|
33
|
+
expect(RadioButton.propTypes.id).toBeDefined()
|
34
|
+
expect(RadioButton.propTypes.name).toBeDefined()
|
35
|
+
expect(RadioButton.propTypes.label).toBeDefined()
|
36
|
+
expect(RadioButton.propTypes.value).toBeDefined()
|
37
|
+
expect(RadioButton.propTypes.checked).toBeDefined()
|
38
|
+
expect(RadioButton.propTypes.disabled).toBeDefined()
|
39
|
+
expect(RadioButton.propTypes.readOnly).toBeDefined()
|
40
|
+
expect(RadioButton.propTypes.error).toBeDefined()
|
41
|
+
expect(RadioButton.propTypes.required).toBeDefined()
|
42
|
+
expect(RadioButton.propTypes.className).toBeDefined()
|
43
|
+
expect(RadioButton.propTypes.ariaLabel).toBeDefined()
|
44
|
+
expect(RadioButton.propTypes.onChange).toBeDefined()
|
45
|
+
})
|
46
|
+
|
47
|
+
test('component has correct defaultProps', () => {
|
48
|
+
expect(RadioButton.defaultProps).toBeDefined()
|
49
|
+
expect(RadioButton.defaultProps.checked).toBe(false)
|
50
|
+
expect(RadioButton.defaultProps.disabled).toBe(false)
|
51
|
+
expect(RadioButton.defaultProps.readOnly).toBe(false)
|
52
|
+
expect(RadioButton.defaultProps.error).toBe(false)
|
53
|
+
expect(RadioButton.defaultProps.required).toBe(false)
|
54
|
+
expect(RadioButton.defaultProps.className).toBe('')
|
55
|
+
})
|
56
|
+
|
57
|
+
test('warns when id prop is missing', () => {
|
58
|
+
const validateId = (id) => {
|
59
|
+
if (!id) {
|
60
|
+
console.warn('RadioButton component: id prop is required')
|
61
|
+
return null
|
62
|
+
}
|
63
|
+
return true
|
64
|
+
}
|
65
|
+
|
66
|
+
const result = validateId(undefined)
|
67
|
+
expect(result).toBeNull()
|
68
|
+
expect(console.warn).toHaveBeenCalledWith('RadioButton component: id prop is required')
|
69
|
+
})
|
70
|
+
|
71
|
+
test('warns when name prop is missing', () => {
|
72
|
+
const validateName = (name) => {
|
73
|
+
if (!name) {
|
74
|
+
console.warn('RadioButton component: name prop is required for radio button groups')
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
validateName(undefined)
|
79
|
+
expect(console.warn).toHaveBeenCalledWith('RadioButton component: name prop is required for radio button groups')
|
80
|
+
})
|
81
|
+
|
82
|
+
test('generates correct CSS classes for different states', () => {
|
83
|
+
const generateClasses = (disabled, readOnly, error, className) => {
|
84
|
+
return [
|
85
|
+
'radio',
|
86
|
+
disabled && 'disabled',
|
87
|
+
readOnly && 'readonly',
|
88
|
+
error && 'error',
|
89
|
+
className || ''
|
90
|
+
].filter(Boolean).join(' ')
|
91
|
+
}
|
92
|
+
|
93
|
+
expect(generateClasses(false, false, false, '')).toBe('radio')
|
94
|
+
expect(generateClasses(true, false, false, '')).toBe('radio disabled')
|
95
|
+
expect(generateClasses(false, true, false, '')).toBe('radio readonly')
|
96
|
+
expect(generateClasses(false, false, true, '')).toBe('radio error')
|
97
|
+
expect(generateClasses(false, false, false, 'custom')).toBe('radio custom')
|
98
|
+
expect(generateClasses(true, true, true, 'custom')).toBe('radio disabled readonly error custom')
|
99
|
+
})
|
100
|
+
|
101
|
+
test('change handler works correctly', () => {
|
102
|
+
const mockOnChange = jest.fn()
|
103
|
+
|
104
|
+
const simulateChange = (disabled, readOnly, onChange, id, value) => {
|
105
|
+
const event = { target: { value } }
|
106
|
+
|
107
|
+
if (!disabled && !readOnly && onChange) {
|
108
|
+
onChange(id, value || id, event)
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
// Change normal
|
113
|
+
simulateChange(false, false, mockOnChange, 'test', 'value1')
|
114
|
+
expect(mockOnChange).toHaveBeenCalledWith('test', 'value1', { target: { value: 'value1' } })
|
115
|
+
|
116
|
+
mockOnChange.mockClear()
|
117
|
+
|
118
|
+
// Change con disabled
|
119
|
+
simulateChange(true, false, mockOnChange, 'test', 'value1')
|
120
|
+
expect(mockOnChange).not.toHaveBeenCalled()
|
121
|
+
|
122
|
+
// Change con readOnly
|
123
|
+
simulateChange(false, true, mockOnChange, 'test', 'value1')
|
124
|
+
expect(mockOnChange).not.toHaveBeenCalled()
|
125
|
+
})
|
126
|
+
|
127
|
+
test('keyboard event handling works correctly', () => {
|
128
|
+
const mockChange = jest.fn()
|
129
|
+
|
130
|
+
const simulateKeyDown = (key, change) => {
|
131
|
+
const event = {
|
132
|
+
key,
|
133
|
+
preventDefault: jest.fn()
|
134
|
+
}
|
135
|
+
|
136
|
+
if (event.key === ' ') {
|
137
|
+
event.preventDefault()
|
138
|
+
change(event)
|
139
|
+
}
|
140
|
+
|
141
|
+
return event
|
142
|
+
}
|
143
|
+
|
144
|
+
// Space key
|
145
|
+
const spaceEvent = simulateKeyDown(' ', mockChange)
|
146
|
+
expect(spaceEvent.preventDefault).toHaveBeenCalled()
|
147
|
+
expect(mockChange).toHaveBeenCalledWith(spaceEvent)
|
148
|
+
|
149
|
+
mockChange.mockClear()
|
150
|
+
|
151
|
+
// Other key (should not trigger)
|
152
|
+
const otherEvent = simulateKeyDown('Enter', mockChange)
|
153
|
+
expect(otherEvent.preventDefault).not.toHaveBeenCalled()
|
154
|
+
expect(mockChange).not.toHaveBeenCalled()
|
155
|
+
})
|
156
|
+
|
157
|
+
test('accessibility attributes generation works correctly', () => {
|
158
|
+
const generateAriaAttributes = (ariaLabel, label, required, error, id) => {
|
159
|
+
return {
|
160
|
+
'aria-label': ariaLabel || label,
|
161
|
+
'aria-required': required,
|
162
|
+
'aria-invalid': error,
|
163
|
+
'aria-describedby': error && typeof error === 'string' ? `${id}-error` : undefined
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
const basic = generateAriaAttributes(null, 'Test Label', false, false, 'test')
|
168
|
+
expect(basic['aria-label']).toBe('Test Label')
|
169
|
+
expect(basic['aria-required']).toBe(false)
|
170
|
+
expect(basic['aria-invalid']).toBe(false)
|
171
|
+
expect(basic['aria-describedby']).toBeUndefined()
|
172
|
+
|
173
|
+
const withError = generateAriaAttributes(null, 'Test Label', false, 'Error message', 'test')
|
174
|
+
expect(withError['aria-invalid']).toBe('Error message')
|
175
|
+
expect(withError['aria-describedby']).toBe('test-error')
|
176
|
+
})
|
177
|
+
})
|
178
|
+
|
179
|
+
describe('RadioGroup Component', () => {
|
180
|
+
test('component exports correctly', () => {
|
181
|
+
expect(RadioGroup).toBeDefined()
|
182
|
+
expect(typeof RadioGroup).toBe('function')
|
183
|
+
})
|
184
|
+
|
185
|
+
test('component has correct PropTypes', () => {
|
186
|
+
expect(RadioGroup.propTypes).toBeDefined()
|
187
|
+
expect(RadioGroup.propTypes.id).toBeDefined()
|
188
|
+
expect(RadioGroup.propTypes.label).toBeDefined()
|
189
|
+
expect(RadioGroup.propTypes.value).toBeDefined()
|
190
|
+
expect(RadioGroup.propTypes.options).toBeDefined()
|
191
|
+
expect(RadioGroup.propTypes.disabled).toBeDefined()
|
192
|
+
expect(RadioGroup.propTypes.readOnly).toBeDefined()
|
193
|
+
expect(RadioGroup.propTypes.error).toBeDefined()
|
194
|
+
expect(RadioGroup.propTypes.required).toBeDefined()
|
195
|
+
expect(RadioGroup.propTypes.className).toBeDefined()
|
196
|
+
expect(RadioGroup.propTypes.ariaLabel).toBeDefined()
|
197
|
+
expect(RadioGroup.propTypes.onChange).toBeDefined()
|
198
|
+
})
|
199
|
+
|
200
|
+
test('component has correct defaultProps', () => {
|
201
|
+
expect(RadioGroup.defaultProps).toBeDefined()
|
202
|
+
expect(RadioGroup.defaultProps.options).toEqual([])
|
203
|
+
expect(RadioGroup.defaultProps.disabled).toBe(false)
|
204
|
+
expect(RadioGroup.defaultProps.readOnly).toBe(false)
|
205
|
+
expect(RadioGroup.defaultProps.error).toBe(false)
|
206
|
+
expect(RadioGroup.defaultProps.required).toBe(false)
|
207
|
+
expect(RadioGroup.defaultProps.className).toBe('')
|
208
|
+
})
|
209
|
+
|
210
|
+
test('generates unique group name when id is not provided', () => {
|
211
|
+
const generateGroupName = (id) => {
|
212
|
+
return id || `radiogroup-${Math.random().toString(36).substring(2, 11)}`
|
213
|
+
}
|
214
|
+
|
215
|
+
const name1 = generateGroupName(undefined)
|
216
|
+
const name2 = generateGroupName(undefined)
|
217
|
+
|
218
|
+
expect(name1).toMatch(/^radiogroup-/)
|
219
|
+
expect(name2).toMatch(/^radiogroup-/)
|
220
|
+
expect(name1).not.toBe(name2) // Should be different
|
221
|
+
})
|
222
|
+
|
223
|
+
test('state synchronization works correctly', () => {
|
224
|
+
let internalState = null
|
225
|
+
|
226
|
+
const syncState = (externalValue) => {
|
227
|
+
internalState = externalValue
|
228
|
+
}
|
229
|
+
|
230
|
+
syncState('option1')
|
231
|
+
expect(internalState).toBe('option1')
|
232
|
+
|
233
|
+
syncState('option2')
|
234
|
+
expect(internalState).toBe('option2')
|
235
|
+
})
|
236
|
+
|
237
|
+
test('change handler works correctly', () => {
|
238
|
+
const mockOnChange = jest.fn()
|
239
|
+
let selectedValue = null
|
240
|
+
|
241
|
+
const simulateChange = (disabled, readOnly, onChange, id, optionValue) => {
|
242
|
+
if (!disabled && !readOnly) {
|
243
|
+
selectedValue = optionValue
|
244
|
+
if (onChange) {
|
245
|
+
onChange(id || 'radiogroup', optionValue, {})
|
246
|
+
}
|
247
|
+
}
|
248
|
+
}
|
249
|
+
|
250
|
+
// Normal change
|
251
|
+
simulateChange(false, false, mockOnChange, 'test-group', 'option1')
|
252
|
+
expect(selectedValue).toBe('option1')
|
253
|
+
expect(mockOnChange).toHaveBeenCalledWith('test-group', 'option1', {})
|
254
|
+
|
255
|
+
mockOnChange.mockClear()
|
256
|
+
|
257
|
+
// Disabled group
|
258
|
+
simulateChange(true, false, mockOnChange, 'test-group', 'option2')
|
259
|
+
expect(selectedValue).toBe('option1') // Should not change
|
260
|
+
expect(mockOnChange).not.toHaveBeenCalled()
|
261
|
+
|
262
|
+
// ReadOnly group
|
263
|
+
simulateChange(false, true, mockOnChange, 'test-group', 'option2')
|
264
|
+
expect(selectedValue).toBe('option1') // Should not change
|
265
|
+
expect(mockOnChange).not.toHaveBeenCalled()
|
266
|
+
})
|
267
|
+
|
268
|
+
test('CSS classes generation works correctly', () => {
|
269
|
+
const generateClasses = (disabled, readOnly, error, className) => {
|
270
|
+
return [
|
271
|
+
'radio-group',
|
272
|
+
disabled && 'disabled',
|
273
|
+
readOnly && 'readonly',
|
274
|
+
error && 'error',
|
275
|
+
className || ''
|
276
|
+
].filter(Boolean).join(' ')
|
277
|
+
}
|
278
|
+
|
279
|
+
expect(generateClasses(false, false, false, '')).toBe('radio-group')
|
280
|
+
expect(generateClasses(true, false, false, '')).toBe('radio-group disabled')
|
281
|
+
expect(generateClasses(false, true, false, '')).toBe('radio-group readonly')
|
282
|
+
expect(generateClasses(false, false, true, '')).toBe('radio-group error')
|
283
|
+
expect(generateClasses(false, false, false, 'custom')).toBe('radio-group custom')
|
284
|
+
})
|
285
|
+
|
286
|
+
test('options mapping works correctly', () => {
|
287
|
+
const options = [
|
288
|
+
{ id: 'opt1', label: 'Option 1', value: 'value1' },
|
289
|
+
{ id: 'opt2', label: 'Option 2', value: 'value2' },
|
290
|
+
{ label: 'Option 3' } // Without id or value
|
291
|
+
]
|
292
|
+
|
293
|
+
const mapOptions = (options, groupName) => {
|
294
|
+
return options.map((option, index) => ({
|
295
|
+
key: option.value || option.id || index,
|
296
|
+
id: option.id || `${groupName}-${index}`,
|
297
|
+
name: groupName,
|
298
|
+
label: option.label,
|
299
|
+
value: option.value || option.id
|
300
|
+
}))
|
301
|
+
}
|
302
|
+
|
303
|
+
const mapped = mapOptions(options, 'test-group')
|
304
|
+
|
305
|
+
expect(mapped[0].key).toBe('value1')
|
306
|
+
expect(mapped[0].id).toBe('opt1')
|
307
|
+
expect(mapped[0].value).toBe('value1')
|
308
|
+
|
309
|
+
expect(mapped[1].key).toBe('value2')
|
310
|
+
expect(mapped[1].id).toBe('opt2')
|
311
|
+
expect(mapped[1].value).toBe('value2')
|
312
|
+
|
313
|
+
expect(mapped[2].key).toBe(2)
|
314
|
+
expect(mapped[2].id).toBe('test-group-2')
|
315
|
+
expect(mapped[2].value).toBeUndefined()
|
316
|
+
})
|
317
|
+
})
|
318
|
+
})
|
@@ -0,0 +1,99 @@
|
|
1
|
+
import React from 'react'
|
2
|
+
import { Section } from './section'
|
3
|
+
import { Button } from './button'
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Ejemplos de uso del componente Section mejorado
|
7
|
+
*/
|
8
|
+
export const SectionExamples = () => {
|
9
|
+
return (
|
10
|
+
<div style={{ padding: '2rem', maxWidth: '800px' }}>
|
11
|
+
<h1>Ejemplos del Componente Section Mejorado</h1>
|
12
|
+
|
13
|
+
{/* Ejemplo básico */}
|
14
|
+
<Section title="Sección Básica" icon="info">
|
15
|
+
<p>Esta es una sección básica que está cerrada por defecto.</p>
|
16
|
+
<p>Haz clic en el icono de expansión para abrirla.</p>
|
17
|
+
</Section>
|
18
|
+
|
19
|
+
{/* Sección abierta por defecto */}
|
20
|
+
<Section title="Sección Abierta" icon="folder_open" open={true}>
|
21
|
+
<p>Esta sección está abierta por defecto.</p>
|
22
|
+
<p>El icono cambia dinámicamente según el estado.</p>
|
23
|
+
<ul>
|
24
|
+
<li>✅ Icono dinámico (expand_more/expand_less)</li>
|
25
|
+
<li>✅ Tooltip informativo</li>
|
26
|
+
<li>✅ Atributos de accesibilidad</li>
|
27
|
+
</ul>
|
28
|
+
</Section>
|
29
|
+
|
30
|
+
{/* Sección con acciones */}
|
31
|
+
<Section
|
32
|
+
title="Sección con Acciones"
|
33
|
+
icon="settings"
|
34
|
+
actions={
|
35
|
+
<div style={{ display: 'flex', gap: '0.5rem' }}>
|
36
|
+
<Button label="Editar" icon="edit" outlined />
|
37
|
+
<Button label="Eliminar" icon="delete" outlined />
|
38
|
+
</div>
|
39
|
+
}
|
40
|
+
>
|
41
|
+
<p>Esta sección incluye botones de acción en el header.</p>
|
42
|
+
<p>Las acciones se muestran junto al botón de colapsar.</p>
|
43
|
+
</Section>
|
44
|
+
|
45
|
+
{/* Sección no colapsable */}
|
46
|
+
<Section
|
47
|
+
title="Sección No Colapsable"
|
48
|
+
icon="lock"
|
49
|
+
canCollapse={false}
|
50
|
+
className="always-open"
|
51
|
+
>
|
52
|
+
<p>Esta sección no puede colapsarse.</p>
|
53
|
+
<p>No muestra el icono de expansión/contracción.</p>
|
54
|
+
<p>Útil para contenido que siempre debe estar visible.</p>
|
55
|
+
</Section>
|
56
|
+
|
57
|
+
{/* Sección con clase personalizada */}
|
58
|
+
<Section
|
59
|
+
title="Sección Personalizada"
|
60
|
+
icon="palette"
|
61
|
+
className="custom-section"
|
62
|
+
open={true}
|
63
|
+
>
|
64
|
+
<p>Esta sección tiene una clase CSS personalizada.</p>
|
65
|
+
<p>El manejo de className es seguro y no causa errores.</p>
|
66
|
+
<div style={{
|
67
|
+
background: '#f0f8ff',
|
68
|
+
padding: '1rem',
|
69
|
+
borderRadius: '4px',
|
70
|
+
border: '1px solid #e0e0e0'
|
71
|
+
}}>
|
72
|
+
<strong>Mejoras implementadas:</strong>
|
73
|
+
<ul>
|
74
|
+
<li>🔧 Icono dinámico corregido</li>
|
75
|
+
<li>🛡️ Manejo seguro de className</li>
|
76
|
+
<li>📝 PropTypes y documentación</li>
|
77
|
+
<li>♿ Atributos de accesibilidad</li>
|
78
|
+
<li>💬 Tooltips informativos</li>
|
79
|
+
<li>✅ Pruebas unitarias</li>
|
80
|
+
</ul>
|
81
|
+
</div>
|
82
|
+
</Section>
|
83
|
+
|
84
|
+
{/* Sección sin título */}
|
85
|
+
<Section icon="description">
|
86
|
+
<p>Esta sección no tiene título, solo icono.</p>
|
87
|
+
<p>Demuestra la flexibilidad del componente.</p>
|
88
|
+
</Section>
|
89
|
+
|
90
|
+
{/* Sección mínima */}
|
91
|
+
<Section>
|
92
|
+
<p>Sección mínima sin título ni icono.</p>
|
93
|
+
<p>Solo con el contenido y funcionalidad de colapsar.</p>
|
94
|
+
</Section>
|
95
|
+
</div>
|
96
|
+
)
|
97
|
+
}
|
98
|
+
|
99
|
+
export default SectionExamples
|