Skip to content

Commit da8ab93

Browse files
committed
TextFiled Presenter getColorByState fix (#2290)
* TextFiled Presenter getColorByState fix, new function isDesignToken to check if the color that passed is design token * Fixed review notes, added test to colors.spec file
1 parent 8bd9f9b commit da8ab93

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/incubator/TextField/Presenter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export function getColorByState(color?: ColorType, context?: FieldContextType) {
99
let finalColor: string | undefined;
1010
if (_.isString(color)) {
1111
finalColor = color;
12+
} else if (Colors.isDesignToken(color)) {
13+
finalColor = color?.toString();
1214
} else if (_.isPlainObject(color)) {
1315
if (context?.disabled) {
1416
finalColor = color?.disabled;
@@ -18,9 +20,8 @@ export function getColorByState(color?: ColorType, context?: FieldContextType) {
1820
finalColor = color?.focus;
1921
}
2022

21-
finalColor = finalColor || color?.default || (color as string) || Colors.$textDefault;
23+
finalColor = finalColor || color?.default || Colors.$textDefault;
2224
}
23-
2425
return finalColor;
2526
}
2627

src/style/__tests__/colors.spec.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('style/Colors', () => {
8181
expect(uut.getColorTint('#000000', 60)).toEqual('#808080');
8282
});
8383

84-
it('should handle color that does not exist in uilib', () => {
84+
it('should handle color that does not exist in `uilib`', () => {
8585
expect(uut.getColorTint('#F1BE0B', 10)).toEqual('#8D7006'); //
8686
expect(uut.getColorTint('#F1BE0B', 20)).toEqual('#BE9609'); //
8787
expect(uut.getColorTint('#F1BE0B', 30)).toEqual('#F1BE0B'); //
@@ -181,4 +181,13 @@ describe('style/Colors', () => {
181181
});
182182
});
183183
});
184+
185+
describe('isDesignToken(...)', () => {
186+
it('should return true if the color passed is design token', () => {
187+
expect(uut.isDesignToken({semantic: ['$textDefault'], toString: () => {}})).toEqual(true);
188+
expect(uut.isDesignToken({resource_paths: ['@color/textNeutral'], toString: () => {}})).toEqual(true);
189+
expect(uut.isDesignToken({test: 'fail', toString: () => {}})).toEqual(false);
190+
expect(uut.isDesignToken(uut.red10)).toEqual(false);
191+
});
192+
});
184193
});

src/style/colors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import DesignTokensDM from './designTokensDM';
1010
import ColorName from './colorName';
1111
import Scheme, {Schemes, SchemeType} from './scheme';
1212

13+
export type DesignToken = {semantic?: [string]; resource_paths?: [string]; toString: Function};
14+
1315
export class Colors {
1416
[key: string]: any;
1517
private shouldSupportDarkMode = false;
@@ -276,6 +278,7 @@ export class Colors {
276278
isValidHex(string: string) {
277279
return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(string);
278280
}
281+
279282
getHexString(color: tinycolor.ColorInput) {
280283
return tinycolor(color).toHexString();
281284
}
@@ -290,6 +293,9 @@ export class Colors {
290293
const colorB = colorStringValue(colorBValue);
291294
return _.toLower(colorA) === _.toLower(colorB);
292295
}
296+
isDesignToken(color?: DesignToken) {
297+
return !!(color?.semantic || color?.resource_paths);
298+
}
293299
}
294300

295301
function colorStringValue(color: string | object) {

0 commit comments

Comments
 (0)