Skip to content

Commit e7861d3

Browse files
authored
fix: extractStyles() logic fix & field label position fix (#138)
1 parent d0559c2 commit e7861d3

File tree

9 files changed

+82
-41
lines changed

9 files changed

+82
-41
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@cube-dev/ui-kit": patch
3+
---
4+
5+
Fix extractStyles() logic.
6+
Fix label position inside a field.

src/components/forms/Checkbox/Checkbox.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ const CheckboxWrapperElement = tasty({
6060
flow: 'row',
6161
preset: 'default',
6262
cursor: 'pointer',
63+
margin: {
64+
'': 0,
65+
'inside-form & side-label': '1.5x top',
66+
},
6367
},
6468
});
6569

@@ -149,10 +153,6 @@ function Checkbox(
149153
labelStyles.fontWeight = 400;
150154
}
151155

152-
if (insideForm && labelPosition === 'side') {
153-
inputStyles.marginTop = '1x';
154-
}
155-
156156
let { isFocused, focusProps } = useFocus({ isDisabled }, true);
157157
let { hoverProps, isHovered } = useHover({ isDisabled });
158158

@@ -201,6 +201,8 @@ function Checkbox(
201201
disabled: isDisabled,
202202
hovered: isHovered,
203203
focused: isFocused,
204+
'side-label': labelPosition === 'side',
205+
'inside-form': insideForm,
204206
};
205207

206208
const checkboxField = (

src/components/forms/FieldWrapper.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ const FieldElement = tasty({
2525
'': '1x',
2626
'has-sider': '@(column-gap, 1x)',
2727
},
28-
placeItems: 'baseline stretch',
28+
placeItems: 'start stretch',
2929

3030
LabelArea: {
3131
display: 'block',
3232
width: {
3333
'': 'initial',
3434
'has-sider': '@label-width',
3535
},
36+
padding: {
37+
'': 'initial',
38+
'has-sider': '1.25x top',
39+
},
3640
},
3741

3842
InputArea: {

src/components/forms/Label.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ export const LABEL_STYLES: Styles = {
6363
display: 'block',
6464
fontWeight: 600,
6565
preset: 'default',
66-
padding: {
67-
'': '0',
68-
side: '(1x - 1bw) top',
69-
},
7066
color: {
7167
'': '#dark',
7268
invalid: '#danger-text',

src/components/forms/RadioGroup/RadioGroup.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ const RadioGroupElement = tasty({
4242
'': '1x',
4343
horizontal: '1x 2x',
4444
},
45-
padding: '(1x - 1bw) 0',
45+
padding: {
46+
'': 0,
47+
'inside-form & side-label': '1.5x 0',
48+
},
4649
},
4750
});
4851

@@ -69,6 +72,7 @@ function RadioGroup(props: WithNullableValue<CubeRadioGroupProps>, ref) {
6972
isHidden,
7073
styles,
7174
groupStyles,
75+
insideForm,
7276
...otherProps
7377
} = props;
7478
let domRef = useDOMRef(ref);
@@ -84,6 +88,8 @@ function RadioGroup(props: WithNullableValue<CubeRadioGroupProps>, ref) {
8488
styles={groupStyles}
8589
mods={{
8690
horizontal: orientation === 'horizontal',
91+
'inside-form': insideForm,
92+
'side-label': labelPosition === 'side',
8793
}}
8894
>
8995
<FormContext.Provider

src/components/forms/Switch/Switch.tsx

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
Styles,
1717
tasty,
1818
} from '../../../tasty';
19-
import { Base } from '../../Base';
2019
import { useFocus } from '../../../utils/react/interactions';
2120
import { mergeProps } from '../../../utils/react';
2221
import { HiddenInput } from '../../HiddenInput';
@@ -31,14 +30,19 @@ import {
3130
WithNullableSelected,
3231
} from '../../../utils/react/nullableValue';
3332

34-
const BaseSwitchWrapperElement = tasty({
33+
const SwitchWrapperElement = tasty({
3534
qa: 'SwitchWrapper',
3635
styles: {
3736
position: 'relative',
37+
margin: {
38+
'': 0,
39+
'inside-form & side-label': '1x top',
40+
},
3841
},
3942
});
4043

41-
const SwitchWrapperElement = tasty({
44+
const SwitchLabelElement = tasty({
45+
as: 'label',
4246
qa: 'SwitchWrapper',
4347
styles: {
4448
position: 'relative',
@@ -73,10 +77,6 @@ const SwitchElement = tasty({
7377
},
7478
transition: 'theme',
7579
cursor: 'pointer',
76-
marginTop: {
77-
'': null,
78-
'inside-form & side-label': '-3px',
79-
},
8080
placeSelf: {
8181
'': null,
8282
'inside-form & side-label': 'start',
@@ -164,32 +164,31 @@ function Switch(props: WithNullableSelected<CubeSwitchProps>, ref) {
164164
// eslint-disable-next-line react-hooks/rules-of-hooks
165165
let { inputProps } = useSwitch(props, useToggleState(props), inputRef);
166166

167+
const mods = {
168+
'inside-form': insideForm,
169+
'side-label': labelPosition === 'side',
170+
checked: inputProps.checked,
171+
disabled: isDisabled,
172+
hovered: isHovered,
173+
focused: isFocused,
174+
};
175+
167176
const switchField = (
168-
<BaseSwitchWrapperElement qa={qa || 'Switch'}>
177+
<SwitchWrapperElement qa={qa || 'Switch'} mods={mods}>
169178
<HiddenInput
170179
data-qa="HiddenInput"
171180
{...mergeProps(inputProps, focusProps)}
172181
ref={inputRef}
173182
/>
174-
<SwitchElement
175-
mods={{
176-
'inside-form': insideForm,
177-
'side-label': labelPosition === 'side',
178-
checked: inputProps.checked,
179-
disabled: isDisabled,
180-
hovered: isHovered,
181-
focused: isFocused,
182-
}}
183-
styles={inputStyles}
184-
>
183+
<SwitchElement mods={mods} styles={inputStyles}>
185184
<SwitchThumbElement
186185
styles={thumbStyles}
187186
mods={{
188187
checked: inputProps.checked,
189188
}}
190189
/>
191190
</SwitchElement>
192-
</BaseSwitchWrapperElement>
191+
</SwitchWrapperElement>
193192
);
194193

195194
if (insideForm) {
@@ -217,9 +216,9 @@ function Switch(props: WithNullableSelected<CubeSwitchProps>, ref) {
217216
}
218217

219218
return (
220-
<SwitchWrapperElement
221-
as="label"
219+
<SwitchLabelElement
222220
styles={styles}
221+
mods={mods}
223222
{...hoverProps}
224223
{...filterBaseProps(otherProps)}
225224
ref={domRef}
@@ -242,7 +241,7 @@ function Switch(props: WithNullableSelected<CubeSwitchProps>, ref) {
242241
) : null}
243242
</Element>
244243
)}
245-
</SwitchWrapperElement>
244+
</SwitchLabelElement>
246245
);
247246
}
248247

src/tasty/tasty.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { tasty } from './tasty';
33
import { Button } from '../components/actions';
44
import { Block } from '../components/Block';
55
import { CONTAINER_STYLES } from './styles/list';
6+
import { Card } from '../components/content/Card/Card';
67

78
describe('tasty() API', () => {
89
beforeEach(() => {

src/tasty/utils/styles.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
applyStates,
33
computeState,
4+
extractStyles,
45
replaceStateValues,
56
styleMapToStyleMapStateList,
67
} from './styles';
@@ -375,3 +376,34 @@ describe('styleMapToStyleMapStateList', () => {
375376
]);
376377
});
377378
});
379+
380+
describe('extractStyles', () => {
381+
function checkExtraction(list) {
382+
list.forEach((obj, i) => {
383+
it(`to list ${i}`, () => {
384+
// @ts-ignore
385+
expect(extractStyles(...obj.input)).toEqual(obj.output);
386+
});
387+
});
388+
}
389+
390+
describe('Should extract styles from props', () => {
391+
checkExtraction([
392+
{
393+
input: [
394+
{
395+
fill: '#black',
396+
border: false,
397+
styles: { fill: '#clear', border: true, color: '#dark' },
398+
},
399+
['fill', 'border'],
400+
],
401+
output: {
402+
fill: '#black',
403+
border: false,
404+
color: '#dark',
405+
},
406+
},
407+
]);
408+
});
409+
});

src/tasty/utils/styles.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -616,18 +616,13 @@ export function extractStyles(
616616

617617
Object.keys(props).forEach((prop) => {
618618
const propName = propMap ? propMap[prop] || prop : prop;
619+
const isPropExist = prop in props;
619620
const value = props[prop];
620621

621622
if (ignoreList && ignoreList.includes(prop)) {
622623
// do nothing
623-
} else if (styleList.includes(propName)) {
624-
// If value is not nullish then map it to the styles
625-
if (value != null && value !== false) {
626-
styles[propName] = value;
627-
// If value is nullish then erase the default value of the style
628-
} else if (propName in styles) {
629-
delete styles[propName];
630-
}
624+
} else if (styleList.includes(propName) && isPropExist) {
625+
styles[propName] = value;
631626
}
632627
}, {});
633628

0 commit comments

Comments
 (0)