Skip to content

Commit e2d6a54

Browse files
authored
refactor(chip): add stricter types (#31019)
Issue number: N/A --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> After working on spinner, I realized that some of the type can be more strict. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Updated types ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/docs/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> N/A
1 parent 1ce0e2d commit e2d6a54

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

core/src/components.d.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { AlertButton, AlertInput } from "./components/alert/alert-interface";
1414
import { RouteID, RouterDirection, RouterEventDetail, RouteWrite } from "./components/router/utils/interface";
1515
import { BreadcrumbCollapsedClickEventDetail } from "./components/breadcrumb/breadcrumb-interface";
1616
import { CheckboxChangeEventDetail } from "./components/checkbox/checkbox-interface";
17+
import { IonChipFill, IonChipHue, IonChipShape, IonChipSize } from "./components/chip/chip.interfaces";
1718
import { ScrollBaseDetail, ScrollDetail } from "./components/content/content-interface";
1819
import { DatetimeChangeEventDetail, DatetimeHighlight, DatetimeHighlightCallback, DatetimeHourCycle, DatetimePresentation, FormatOptions, TitleSelectedDatesFormatter } from "./components/datetime/datetime-interface";
1920
import { SpinnerTypes } from "./components/spinner/spinner-configs";
@@ -53,6 +54,7 @@ export { AlertButton, AlertInput } from "./components/alert/alert-interface";
5354
export { RouteID, RouterDirection, RouterEventDetail, RouteWrite } from "./components/router/utils/interface";
5455
export { BreadcrumbCollapsedClickEventDetail } from "./components/breadcrumb/breadcrumb-interface";
5556
export { CheckboxChangeEventDetail } from "./components/checkbox/checkbox-interface";
57+
export { IonChipFill, IonChipHue, IonChipShape, IonChipSize } from "./components/chip/chip.interfaces";
5658
export { ScrollBaseDetail, ScrollDetail } from "./components/content/content-interface";
5759
export { DatetimeChangeEventDetail, DatetimeHighlight, DatetimeHighlightCallback, DatetimeHourCycle, DatetimePresentation, FormatOptions, TitleSelectedDatesFormatter } from "./components/datetime/datetime-interface";
5860
export { SpinnerTypes } from "./components/spinner/spinner-configs";
@@ -876,11 +878,11 @@ export namespace Components {
876878
/**
877879
* The fill for the chip. Set to `"outline"` for a chip with a border and background. Set to `"solid"` for a chip with a background. Defaults to `"solid"` if both the fill property and theme config are unset.
878880
*/
879-
"fill"?: 'outline' | 'solid';
881+
"fill"?: IonChipFill;
880882
/**
881883
* Set to `"bold"` for a chip with vibrant, bold colors or to `"subtle"` for a chip with muted, subtle colors. Defaults to `"subtle"` if both the hue property and theme config are unset.
882884
*/
883-
"hue"?: 'bold' | 'subtle';
885+
"hue"?: IonChipHue;
884886
/**
885887
* The mode determines the platform behaviors of the component.
886888
*/
@@ -894,11 +896,11 @@ export namespace Components {
894896
/**
895897
* Set to `"soft"` for a chip with slightly rounded corners, `"round"` for a chip with fully rounded corners, or `"rectangular"` for a chip without rounded corners. Defaults to `"round"` if both the shape property and theme config are unset.
896898
*/
897-
"shape"?: 'soft' | 'round' | 'rectangular';
899+
"shape"?: IonChipShape;
898900
/**
899901
* Set to `"small"` for a chip with less height and padding. Defaults to `"large"` if both the size property and theme config are unset.
900902
*/
901-
"size"?: 'small' | 'large';
903+
"size"?: IonChipSize;
902904
}
903905
interface IonCol {
904906
/**
@@ -6846,11 +6848,11 @@ declare namespace LocalJSX {
68466848
/**
68476849
* The fill for the chip. Set to `"outline"` for a chip with a border and background. Set to `"solid"` for a chip with a background. Defaults to `"solid"` if both the fill property and theme config are unset.
68486850
*/
6849-
"fill"?: 'outline' | 'solid';
6851+
"fill"?: IonChipFill;
68506852
/**
68516853
* Set to `"bold"` for a chip with vibrant, bold colors or to `"subtle"` for a chip with muted, subtle colors. Defaults to `"subtle"` if both the hue property and theme config are unset.
68526854
*/
6853-
"hue"?: 'bold' | 'subtle';
6855+
"hue"?: IonChipHue;
68546856
/**
68556857
* The mode determines the platform behaviors of the component.
68566858
*/
@@ -6864,11 +6866,11 @@ declare namespace LocalJSX {
68646866
/**
68656867
* Set to `"soft"` for a chip with slightly rounded corners, `"round"` for a chip with fully rounded corners, or `"rectangular"` for a chip without rounded corners. Defaults to `"round"` if both the shape property and theme config are unset.
68666868
*/
6867-
"shape"?: 'soft' | 'round' | 'rectangular';
6869+
"shape"?: IonChipShape;
68686870
/**
68696871
* Set to `"small"` for a chip with less height and padding. Defaults to `"large"` if both the size property and theme config are unset.
68706872
*/
6871-
"size"?: 'small' | 'large';
6873+
"size"?: IonChipSize;
68726874
}
68736875
interface IonCol {
68746876
/**

core/src/components/chip/chip.interfaces.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,17 @@ export type IonChipRecipe = {
1313

1414
// Hues with fills
1515
hue?: {
16-
bold?: IonChipFillDefinition;
17-
subtle?: IonChipFillDefinition;
16+
[K in IonChipHue]?: IonChipFillDefinition;
1817
};
1918

2019
// Sizes
2120
size?: {
22-
small?: IonChipSizeDefinition;
23-
large?: IonChipSizeDefinition;
21+
[K in IonChipSize]?: IonChipSizeDefinition;
2422
};
2523

2624
// Shapes
2725
shape?: {
28-
soft?: IonChipShapeDefinition;
29-
round?: IonChipShapeDefinition;
30-
rectangular?: IonChipShapeDefinition;
26+
[K in IonChipShape]?: IonChipShapeDefinition;
3127
};
3228

3329
// Shared States
@@ -50,11 +46,7 @@ export type IonChipRecipe = {
5046
};
5147

5248
type IonChipFillDefinition = {
53-
solid?: IonChipStateDefinition & {
54-
semantic?: IonChipStateDefinition;
55-
};
56-
57-
outline?: IonChipStateDefinition & {
49+
[K in IonChipFill]?: IonChipStateDefinition & {
5850
semantic?: IonChipStateDefinition;
5951
};
6052
};
@@ -118,8 +110,13 @@ type IonChipAvatarDefinition = IonChipMediaDefinition & {
118110
};
119111

120112
export type IonChipConfig = {
121-
fill?: 'outline' | 'solid';
122-
hue?: 'bold' | 'subtle';
123-
size?: 'small' | 'large';
124-
shape?: 'soft' | 'round' | 'rectangular';
113+
fill?: IonChipFill;
114+
hue?: IonChipHue;
115+
size?: IonChipSize;
116+
shape?: IonChipShape;
125117
};
118+
119+
export type IonChipFill = 'outline' | 'solid';
120+
export type IonChipHue = 'bold' | 'subtle';
121+
export type IonChipSize = 'small' | 'large';
122+
export type IonChipShape = 'soft' | 'round' | 'rectangular';

core/src/components/chip/chip.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { createColorClasses } from '@utils/theme';
66
import { config } from '../../global/config';
77
import type { Color } from '../../interface';
88

9+
import type { IonChipFill, IonChipHue, IonChipSize, IonChipShape } from './chip.interfaces';
10+
911
/**
1012
* @virtualProp {"ios" | "md"} mode - The mode determines the platform behaviors of the component.
1113
*/
@@ -39,7 +41,7 @@ export class Chip implements ComponentInterface {
3941
*
4042
* Defaults to `"solid"` if both the fill property and theme config are unset.
4143
*/
42-
@Prop() fill?: 'outline' | 'solid';
44+
@Prop() fill?: IonChipFill;
4345

4446
/**
4547
* If `true`, the user cannot interact with the chip.
@@ -52,7 +54,7 @@ export class Chip implements ComponentInterface {
5254
*
5355
* Defaults to `"subtle"` if both the hue property and theme config are unset.
5456
*/
55-
@Prop() hue?: 'bold' | 'subtle';
57+
@Prop() hue?: IonChipHue;
5658

5759
/**
5860
* Set to `"soft"` for a chip with slightly rounded corners,
@@ -61,15 +63,15 @@ export class Chip implements ComponentInterface {
6163
*
6264
* Defaults to `"round"` if both the shape property and theme config are unset.
6365
*/
64-
@Prop() shape?: 'soft' | 'round' | 'rectangular';
66+
@Prop() shape?: IonChipShape;
6567

6668
// TODO(FW-6266): Determine if `medium` size is needed.
6769
/**
6870
* Set to `"small"` for a chip with less height and padding.
6971
*
7072
* Defaults to `"large"` if both the size property and theme config are unset.
7173
*/
72-
@Prop() size?: 'small' | 'large';
74+
@Prop() size?: IonChipSize;
7375

7476
componentDidLoad() {
7577
if (this.outline) {
@@ -84,8 +86,8 @@ export class Chip implements ComponentInterface {
8486
* Gets the chip fill. Uses the `fill` property if set, otherwise
8587
* checks the theme config and falls back to 'solid' if neither is provided.
8688
*/
87-
get fillValue(): string {
88-
const fillConfig = config.getObjectValue('IonChip.fill', 'solid') as string;
89+
get fillValue(): IonChipFill {
90+
const fillConfig = config.getObjectValue('IonChip.fill', 'solid') as IonChipFill;
8991
const fill = this.fill || (this.outline ? 'outline' : undefined) || fillConfig;
9092

9193
return fill;
@@ -95,8 +97,8 @@ export class Chip implements ComponentInterface {
9597
* Gets the chip hue. Uses the `hue` property if set, otherwise
9698
* checks the theme config and falls back to 'subtle' if neither is provided.
9799
*/
98-
get hueValue(): string {
99-
const hueConfig = config.getObjectValue('IonChip.hue', 'subtle') as string;
100+
get hueValue(): IonChipHue {
101+
const hueConfig = config.getObjectValue('IonChip.hue', 'subtle') as IonChipHue;
100102
const hue = this.hue || hueConfig;
101103

102104
return hue;
@@ -106,8 +108,8 @@ export class Chip implements ComponentInterface {
106108
* Gets the chip shape. Uses the `shape` property if set, otherwise
107109
* checks the theme config and falls back to 'round' if neither is provided.
108110
*/
109-
get shapeValue(): string {
110-
const shapeConfig = config.getObjectValue('IonChip.shape', 'round') as string;
111+
get shapeValue(): IonChipShape {
112+
const shapeConfig = config.getObjectValue('IonChip.shape', 'round') as IonChipShape;
111113
const shape = this.shape || shapeConfig;
112114

113115
return shape;
@@ -117,8 +119,8 @@ export class Chip implements ComponentInterface {
117119
* Gets the chip size. Uses the `size` property if set, otherwise
118120
* checks the theme config and falls back to 'large' if neither is provided.
119121
*/
120-
get sizeValue(): string {
121-
const sizeConfig = config.getObjectValue('IonChip.size', 'large') as string;
122+
get sizeValue(): IonChipSize {
123+
const sizeConfig = config.getObjectValue('IonChip.size', 'large') as IonChipSize;
122124
const size = this.size || sizeConfig;
123125

124126
return size;

0 commit comments

Comments
 (0)