-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Update classes generation logic (#24371)
- Loading branch information
Showing
14 changed files
with
202 additions
and
152 deletions.
There are no files selected for viewing
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
5 changes: 5 additions & 0 deletions
5
packages/material-ui-unstyled/src/composeClasses/composeClasses.d.ts
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,5 @@ | ||
export default function composeClasses<T extends keyof any>(options: { | ||
slots: Record<T, string[]>; | ||
classes?: Record<string, string>; | ||
getUtilityClass: (slot: string) => string; | ||
}): Record<T, string>; |
21 changes: 21 additions & 0 deletions
21
packages/material-ui-unstyled/src/composeClasses/composeClasses.js
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,21 @@ | ||
function composeClasses({ slots, classes, getUtilityClass }) { | ||
const output = {}; | ||
|
||
Object.keys(slots).forEach((slot) => { | ||
output[slot] = slots[slot] | ||
.reduce((acc, key) => { | ||
if (key) { | ||
if (classes && classes[key]) { | ||
acc.push(classes[key]); | ||
} | ||
acc.push(getUtilityClass(key)); | ||
} | ||
return acc; | ||
}, []) | ||
.join(' '); | ||
}); | ||
|
||
return output; | ||
} | ||
|
||
export default composeClasses; |
57 changes: 57 additions & 0 deletions
57
packages/material-ui-unstyled/src/composeClasses/composeClasses.test.js
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,57 @@ | ||
import { expect } from 'chai'; | ||
import composeClasses from './composeClasses'; | ||
|
||
describe('composeClasses', () => { | ||
it('should generate the classes based on the slots', () => { | ||
expect( | ||
composeClasses({ | ||
slots: { | ||
root: ['root', 'standard'], | ||
slot: ['slot'], | ||
}, | ||
getUtilityClass: (slot) => `MuiTest-${slot}`, | ||
}), | ||
).to.deep.equal({ | ||
root: 'MuiTest-root MuiTest-standard', | ||
slot: 'MuiTest-slot', | ||
}); | ||
}); | ||
|
||
it('should consider classes if available', () => { | ||
expect( | ||
composeClasses({ | ||
slots: { | ||
root: ['root', 'standard'], | ||
slot: ['slot'], | ||
}, | ||
getUtilityClass: (slot) => `MuiTest-${slot}`, | ||
classes: { | ||
standard: 'standardOverride', | ||
slot: 'slotOverride', | ||
}, | ||
}), | ||
).to.deep.equal({ | ||
root: 'MuiTest-root standardOverride MuiTest-standard', | ||
slot: 'slotOverride MuiTest-slot', | ||
}); | ||
}); | ||
|
||
it('should ignore false values', () => { | ||
expect( | ||
composeClasses({ | ||
slots: { | ||
root: ['root', false, 'standard'], | ||
slot: ['slot'], | ||
}, | ||
getUtilityClass: (slot) => `MuiTest-${slot}`, | ||
classes: { | ||
standard: 'standardOverride', | ||
slot: 'slotOverride', | ||
}, | ||
}), | ||
).to.deep.equal({ | ||
root: 'MuiTest-root standardOverride MuiTest-standard', | ||
slot: 'slotOverride MuiTest-slot', | ||
}); | ||
}); | ||
}); |
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,2 @@ | ||
export { default } from './composeClasses'; | ||
export * from './composeClasses'; |
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 @@ | ||
export { default } from './composeClasses'; |
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
Oops, something went wrong.