-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jsx): class name normalization helper
- Loading branch information
1 parent
d87c180
commit 2e89b99
Showing
7 changed files
with
206 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { atom } from '@reatom/core' | ||
import { createTestCtx } from '@reatom/testing' | ||
import * as assert from 'uvu/assert' | ||
import { cn } from './utils' | ||
|
||
describe('parseClasses', () => { | ||
const ctx = createTestCtx() | ||
|
||
it('handles falsy correctly', () => { | ||
assert.is(ctx.get(cn(false)), '') | ||
assert.is(ctx.get(cn(true)), '') | ||
assert.is(ctx.get(cn(null)), '') | ||
assert.is(ctx.get(cn(undefined)), '') | ||
assert.is(ctx.get(cn({})), '') | ||
assert.is(ctx.get(cn([])), '') | ||
assert.is(ctx.get(cn(atom(undefined))), '') | ||
assert.is(ctx.get(cn(() => undefined)), '') | ||
}) | ||
|
||
it('handles falsy object correctly', () => { | ||
assert.is(ctx.get(cn({ | ||
a: '', | ||
b: 0, | ||
c: NaN, | ||
d: false, | ||
e: null, | ||
f: undefined, | ||
g: atom(undefined), | ||
})), '') | ||
}) | ||
|
||
it('handles falsy array correctly', () => { | ||
assert.is(ctx.get(cn([ | ||
'', | ||
null, | ||
undefined, | ||
{}, | ||
[], | ||
atom(undefined), | ||
() => undefined, | ||
])), '') | ||
}) | ||
|
||
it('handles object correctly', () => { | ||
assert.is(ctx.get(cn({ | ||
a: 'a', | ||
b: 1, | ||
c: true, | ||
d: {}, | ||
e: [], | ||
f: atom(true), | ||
g: () => undefined, | ||
})), 'a b c d e f g') | ||
}) | ||
|
||
it('handles deep array correctly', () => { | ||
assert.is(ctx.get(cn(['a', ['b', ['c']]])), 'a b c') | ||
}) | ||
|
||
it('handles deep atom correctly', () => { | ||
assert.is(ctx.get(cn(atom(() => atom(() => atom('a'))))), 'a') | ||
}) | ||
|
||
it('handles deep getter correctly', () => { | ||
assert.is(ctx.get(cn(() => () => () => 'a')), 'a') | ||
}) | ||
|
||
it('handles complex correctly', () => { | ||
const isBAtom = atom(true) | ||
const stringAtom = atom('d') | ||
const classNameAtom = cn(() => atom(() => [ | ||
'a', | ||
{b: isBAtom}, | ||
['c'], | ||
stringAtom, | ||
() => 'e', | ||
])) | ||
|
||
assert.is(ctx.get(classNameAtom), 'a b c d e') | ||
|
||
isBAtom(ctx, false) | ||
stringAtom(ctx, 'dd') | ||
|
||
assert.is(ctx.get(classNameAtom), 'a c dd e') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { atom, Atom, AtomMaybe, CtxSpy, isAtom } from '@reatom/core' | ||
import { isObject} from '@reatom/utils' | ||
|
||
export type ClassNameValue = AtomMaybe< | ||
| string | ||
| number | ||
| boolean | ||
| null | ||
| undefined | ||
| Array<ClassNameValue> | ||
| Atom<ClassNameValue> | ||
| Record<string, AtomMaybe<string | number | boolean | null | undefined | object>> | ||
| ((ctx: CtxSpy) => ClassNameValue) | ||
> | ||
|
||
export const cn = (value: ClassNameValue): Atom<string> => atom((ctx) => parseClasses(ctx, value)) | ||
|
||
const parseClasses = (ctx: CtxSpy, value: ClassNameValue): string => { | ||
let className = '' | ||
while (isAtom(value)) value = ctx.spy(value) | ||
if (typeof value === 'string') { | ||
className = value | ||
} else if (typeof value === 'function') { | ||
className = parseClasses(ctx, value(ctx)) | ||
} else if (Array.isArray(value)) { | ||
for (let i = 0; i < value.length; i++) { | ||
const parsed = parseClasses(ctx, value[i]) | ||
if (parsed !== '') className += className === '' ? parsed : ' ' + parsed | ||
} | ||
} else if (isObject(value)) { | ||
for (const name in value) { | ||
let val = value[name] | ||
while (isAtom(val)) val = ctx.spy(val) | ||
if (val) className += className === '' ? name : ' ' + name | ||
} | ||
} | ||
return className | ||
} |