Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[theme] Correct augmentColor TypeScript definition #13376

Merged
merged 2 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions packages/material-ui/src/styles/createPalette.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Color, PaletteType } from '..';
import { CommonColors } from '../colors/common';

export type ColorPartial = Partial<Color>;

export interface TypeText {
primary: string;
secondary: string;
Expand All @@ -24,7 +26,7 @@ export interface TypeBackground {

export type TypeDivider = string;

export type PaletteColorOptions = SimplePaletteColorOptions | Partial<Color>;
export type PaletteColorOptions = SimplePaletteColorOptions | ColorPartial;

export interface SimplePaletteColorOptions {
light?: string;
Expand Down Expand Up @@ -64,16 +66,18 @@ export interface Palette {
action: TypeAction;
background: TypeBackground;
getContrastText: (background: string) => string;
augmentColor: (
color: SimplePaletteColorOptions,
mainShade: number | string,
lightShade: number | string,
darkShade: number | string,
) => void;
augmentColor: {
(
color: ColorPartial,
mainShade?: number | string,
lightShade?: number | string,
darkShade?: number | string,
): void;
(color: PaletteColorOptions): void;
};
}

export type PartialTypeObject = { [P in keyof TypeObject]?: Partial<TypeObject[P]> };
export type ColorPartial = Partial<Color>;

export interface PaletteOptions {
primary?: PaletteColorOptions;
Expand Down
2 changes: 2 additions & 0 deletions packages/material-ui/src/styles/createPalette.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ export default function createPalette(palette) {
if (!color.contrastText) {
color.contrastText = getContrastText(color.main);
}

return color;
}

augmentColor(primary);
Expand Down
22 changes: 22 additions & 0 deletions packages/material-ui/src/styles/createPalette.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Color } from '@material-ui/core';
import blue from '@material-ui/core/colors/blue';
import createPalette, {
PaletteColorOptions,
SimplePaletteColorOptions,
} from '@material-ui/core/styles/createPalette';

{
const palette = createPalette({});
const color: Color = blue;
const option: SimplePaletteColorOptions = { main: blue[400] };
const colorOrOption: PaletteColorOptions = undefined as any;

palette.augmentColor(color);
palette.augmentColor(color, 400);
palette.augmentColor(color, 400, 200, 600);
palette.augmentColor(color, 400, undefined, 600);
palette.augmentColor(option);
palette.augmentColor(option, 400); // $ExpectError
palette.augmentColor(colorOrOption);
palette.augmentColor(colorOrOption, 400); // $ExpectError
}
56 changes: 55 additions & 1 deletion packages/material-ui/src/styles/createPalette.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,65 @@ describe('createPalette()', () => {
});

describe('augmentColor', () => {
const palette = createPalette({});

it('should throw when the input is invalid', () => {
const palette = createPalette({});
assert.throws(() => {
palette.augmentColor({});
}, /The color object needs to have a/);
});

it('should accept a color', () => {
const color1 = palette.augmentColor({ ...indigo });
assert.deepEqual(
{
main: color1.main,
light: color1.light,
dark: color1.dark,
contrastText: color1.contrastText,
},
{
dark: '#303f9f',
light: '#7986cb',
main: '#3f51b5',
contrastText: '#fff',
},
);
const color2 = palette.augmentColor({ ...indigo }, 400, 200, 600);
assert.deepEqual(
{
light: color2.light,
main: color2.main,
dark: color2.dark,
contrastText: color2.contrastText,
},
{
light: '#9fa8da',
main: '#5c6bc0',
dark: '#3949ab',
contrastText: '#fff',
},
);
});

it('should accept a partial palette color', () => {
const color = palette.augmentColor({
main: indigo[500],
});
assert.deepEqual(
{
light: color.light,
main: color.main,
dark: color.dark,
contrastText: color.contrastText,
},
{
light: 'rgb(101, 115, 195)',
main: '#3f51b5',
dark: 'rgb(44, 56, 126)',
contrastText: '#fff',
},
);
});
});
});