Skip to content

Commit 9bee8f5

Browse files
authored
fix: should skip appendStyle when value is null or undefined (#242)
* fix: should convert undefined value to 0 * update * update * update * update test case
1 parent c7c8631 commit 9bee8f5

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-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: 4 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, isNonNullable, toStyleStr, where } from '../util';
1919
import {
2020
CSS_FILE_STYLE,
2121
existPath,
@@ -323,7 +323,9 @@ export const parseStyle = (
323323
appendStyle(key, item);
324324
});
325325
} else {
326-
appendStyle(key, actualValue);
326+
if (isNonNullable(actualValue)) {
327+
appendStyle(key, actualValue);
328+
}
327329
}
328330
}
329331
});

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 isNonNullable = <T>(val: T): val is NonNullable<T> => {
205+
return val !== undefined && val !== null;
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 skip appendStyle when value is null or undefined', () => {
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).not.toContain('padding');
982+
});
951983
});

0 commit comments

Comments
 (0)