Skip to content

Commit 168e6ec

Browse files
committed
fix: should convert undefined value to 0
1 parent c7c8631 commit 168e6ec

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ es
2929
dist
3030
yarn.lock
3131
package-lock.json
32+
pnpm-lock.yaml
3233
coverage/
3334
.doc
3435

@@ -43,4 +44,4 @@ coverage/
4344
.dumi/tmp-production
4445
.dumi/tmp-test
4546

46-
bun.lockb
47+
bun.lockb

src/hooks/useStyleRegister.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import StyleContext, {
1515
ATTR_MARK,
1616
CSS_IN_JS_INSTANCE,
1717
} from '../StyleContext';
18-
import { isClientSide, toStyleStr, where } from '../util';
18+
import { isClientSide, isNullable, toStyleStr, where } from '../util';
1919
import {
2020
CSS_FILE_STYLE,
2121
existPath,
@@ -323,7 +323,12 @@ export const parseStyle = (
323323
appendStyle(key, item);
324324
});
325325
} else {
326-
appendStyle(key, actualValue);
326+
// Convert undefined to 0 for padding
327+
if (key.toLowerCase() === 'padding' && isNullable(actualValue)) {
328+
appendStyle(key, 0);
329+
} else {
330+
appendStyle(key, actualValue);
331+
}
327332
}
328333
}
329334
});

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export {
5151
unit,
5252
genCalc,
5353
};
54+
5455
export type {
5556
TokenType,
5657
CSSObject,

src/util/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,7 @@ export function where(options?: {
200200
const hashSelector = `.${hashCls}`;
201201
return hashPriority === 'low' ? `:where(${hashSelector})` : hashSelector;
202202
}
203+
204+
export const isNullable = <T>(val: T): val is Extract<T, null | undefined> => {
205+
return val === null || val === undefined;
206+
};

tests/index.spec.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,4 +948,36 @@ describe('csssinjs', () => {
948948
);
949949
expect(styles3[2].innerHTML).not.toEqual(style);
950950
});
951+
952+
it('should convert undefined value to 0 for padding', () => {
953+
const genStyle = (): CSSInterpolation => ({
954+
'.undefined-test': {
955+
padding: undefined,
956+
},
957+
});
958+
959+
const Demo: React.FC = () => {
960+
const [token, hashId] = useCacheToken<DerivativeToken>(theme, [], {
961+
salt: 'test',
962+
cssVar: { key: 'css-var-test' },
963+
});
964+
965+
useStyleRegister(
966+
{ theme, token, hashId, path: ['cssinjs-undefined-to-zero'] },
967+
genStyle,
968+
);
969+
970+
const className = clsx('undefined-test', hashId);
971+
972+
return <div className={className}>test</div>;
973+
};
974+
975+
render(<Demo />);
976+
977+
const styles = Array.from(document.head.querySelectorAll('style'));
978+
979+
const styleContent = styles[styles.length - 1];
980+
981+
expect(styleContent.innerHTML).toContain('padding:0');
982+
});
951983
});

0 commit comments

Comments
 (0)