@cleartrip/ct-design-chip-dropdown 4.0.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +80 -0
  2. package/dist/ChipDropdown.d.ts +3 -3
  3. package/dist/ChipDropdown.d.ts.map +1 -1
  4. package/dist/ChipDropdown.native.d.ts +5 -0
  5. package/dist/ChipDropdown.native.d.ts.map +1 -0
  6. package/dist/ct-design-chip-dropdown.browser.cjs.js +1 -1
  7. package/dist/ct-design-chip-dropdown.browser.cjs.js.map +1 -1
  8. package/dist/ct-design-chip-dropdown.browser.esm.js +1 -1
  9. package/dist/ct-design-chip-dropdown.browser.esm.js.map +1 -1
  10. package/dist/ct-design-chip-dropdown.cjs.js +52 -38
  11. package/dist/ct-design-chip-dropdown.cjs.js.map +1 -1
  12. package/dist/ct-design-chip-dropdown.esm.js +54 -36
  13. package/dist/ct-design-chip-dropdown.esm.js.map +1 -1
  14. package/dist/ct-design-chip-dropdown.umd.js +63 -76
  15. package/dist/ct-design-chip-dropdown.umd.js.map +1 -1
  16. package/dist/index.d.ts +1 -1
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.native.d.ts +3 -0
  19. package/dist/index.native.d.ts.map +1 -0
  20. package/dist/style.d.ts +27 -0
  21. package/dist/style.d.ts.map +1 -0
  22. package/dist/type.d.ts +18 -14
  23. package/dist/type.d.ts.map +1 -1
  24. package/package.json +29 -12
  25. package/src/ChipDropdown.native.tsx +119 -0
  26. package/src/ChipDropdown.tsx +119 -0
  27. package/src/index.native.ts +2 -0
  28. package/src/index.ts +2 -0
  29. package/src/style.ts +49 -0
  30. package/src/type.ts +59 -0
  31. package/dist/StyledChipArrow/StyledChipArrow.d.ts +0 -9
  32. package/dist/StyledChipArrow/StyledChipArrow.d.ts.map +0 -1
  33. package/dist/StyledChipArrow/index.d.ts +0 -2
  34. package/dist/StyledChipArrow/index.d.ts.map +0 -1
  35. package/dist/StyledChipArrow/type.d.ts +0 -4
  36. package/dist/StyledChipArrow/type.d.ts.map +0 -1
@@ -0,0 +1,119 @@
1
+ import { FC, useEffect, useRef, useState } from 'react';
2
+
3
+ import { Container } from '@cleartrip/ct-design-container';
4
+ import { Chip } from '@cleartrip/ct-design-chip';
5
+ import { Dropdown, Item } from '@cleartrip/ct-design-dropdown';
6
+ import { OutsideClickHandler } from '@cleartrip/ct-design-outside-click-handler';
7
+ import { useStyles } from '@cleartrip/ct-design-style-manager';
8
+ import { useTheme } from '@cleartrip/ct-design-theme';
9
+ import { ChipArrowDownSmall } from '@cleartrip/ct-design-icons';
10
+ import { IconPosition } from '@cleartrip/ct-design-types';
11
+
12
+ import { IChipDropdownProps } from './type';
13
+ import staticStyles, { getChipArrowRotationWeb } from './style';
14
+
15
+ const ChipDropdown: FC<IChipDropdownProps> = ({
16
+ optionList,
17
+ isDefaultOptionSelected = true,
18
+ defaultChipLabel = '',
19
+ selectedOption = '',
20
+ showDropdown = false,
21
+ onClick,
22
+ dropdownHeight = 500,
23
+ chipArrowDownIconProps,
24
+ styleConfig,
25
+ }) => {
26
+ const chipRef = useRef(false);
27
+ const [isActiveChip, setIsActiveChip] = useState(true);
28
+ const [isDropdownOpen, setIsDropdownOpen] = useState(showDropdown);
29
+ const theme = useTheme();
30
+
31
+ const {
32
+ root: rootStyles = [],
33
+ chip: chipStyleConfig,
34
+ dropdownContainer: dropdownContainerStyles = [],
35
+ dropdown: dropdownStyleConfig,
36
+ chipArrowWrapper: chipArrowWrapperStyles = [],
37
+ } = styleConfig || {};
38
+
39
+ const dynamicStyles = useStyles(
40
+ () => ({
41
+ chipArrowWrapper: getChipArrowRotationWeb(isDropdownOpen),
42
+ }),
43
+ [isDropdownOpen],
44
+ );
45
+
46
+ const dropdownHandler = () => {
47
+ setIsDropdownOpen((open) => !open);
48
+ };
49
+ const closeHandler = () => {
50
+ setIsDropdownOpen(false);
51
+ };
52
+
53
+ const getChipLabel = () => {
54
+ for (const optionItem of optionList) {
55
+ const { key, label } = optionItem;
56
+ if (key === selectedOption) return label;
57
+ }
58
+ return '';
59
+ };
60
+
61
+ const chipLabel = getChipLabel();
62
+ const isChipActive = isDefaultOptionSelected || isDropdownOpen || chipLabel !== '';
63
+
64
+ const onClickOfDropdown = (item: Item) => {
65
+ onClick?.(item.label);
66
+ setIsDropdownOpen((open) => !open);
67
+ };
68
+
69
+ useEffect(() => {
70
+ if (chipRef.current) {
71
+ setIsActiveChip(isChipActive);
72
+ } else {
73
+ setIsActiveChip(isDefaultOptionSelected);
74
+ chipRef.current = true;
75
+ }
76
+ // eslint-disable-next-line react-hooks/exhaustive-deps
77
+ }, [isDefaultOptionSelected, isDropdownOpen, chipLabel]);
78
+
79
+ return (
80
+ <Container styleConfig={{ root: [staticStyles.root, ...rootStyles] }}>
81
+ <OutsideClickHandler onOutsideClick={closeHandler}>
82
+ <Chip
83
+ showIcon
84
+ Icon={
85
+ <Container
86
+ styleConfig={{
87
+ root: [staticStyles.chipArrowWrapper, dynamicStyles.chipArrowWrapper, ...chipArrowWrapperStyles],
88
+ }}
89
+ >
90
+ <ChipArrowDownSmall
91
+ color={isActiveChip ? theme.color.background.link2 : theme.color.background.defaultDarkest}
92
+ {...(chipArrowDownIconProps || {})}
93
+ />
94
+ </Container>
95
+ }
96
+ iconPosition={IconPosition.RIGHT}
97
+ label={chipLabel || defaultChipLabel}
98
+ isSelected={isActiveChip}
99
+ onClick={dropdownHandler}
100
+ styleConfig={chipStyleConfig}
101
+ />
102
+ {isDropdownOpen && (
103
+ <Container styleConfig={{ root: [staticStyles.dropdownContainer, ...dropdownContainerStyles] }}>
104
+ <Dropdown
105
+ itemList={optionList}
106
+ dropDownValue={selectedOption}
107
+ onItemSelect={onClickOfDropdown}
108
+ height={dropdownHeight}
109
+ styleConfig={dropdownStyleConfig}
110
+ />
111
+ </Container>
112
+ )}
113
+ </OutsideClickHandler>
114
+ </Container>
115
+ );
116
+ };
117
+
118
+ ChipDropdown.displayName = 'ChipDropdown';
119
+ export default ChipDropdown;
@@ -0,0 +1,2 @@
1
+ export { default as ChipDropdown } from './ChipDropdown.native';
2
+ export type * from './type';
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { default as ChipDropdown } from './ChipDropdown';
2
+ export type * from './type';
package/src/style.ts ADDED
@@ -0,0 +1,49 @@
1
+ import { makeStyles } from '@cleartrip/ct-design-style-manager';
2
+
3
+ /**
4
+ * Static, platform-agnostic styles for ChipDropdown's composable slots. The
5
+ * arrow-wrapper rotation depends on the open/closed state of the dropdown and
6
+ * is therefore computed at render time via `useStyles` rather than being
7
+ * baked into these static slots.
8
+ */
9
+ const staticStyles = makeStyles(() => ({
10
+ root: {
11
+ position: 'relative',
12
+ //todo: add width
13
+ // width: 'fit-content',
14
+ alignSelf: 'flex-start',
15
+ },
16
+ dropdownContainer: {
17
+ position: 'absolute',
18
+ left: 0,
19
+ top: 40,
20
+ zIndex: 1,
21
+ },
22
+ chipArrowWrapper: {
23
+ display: 'flex',
24
+ alignItems: 'center',
25
+ justifyContent: 'center',
26
+ },
27
+ }));
28
+
29
+ /**
30
+ * Web rotation style for the arrow wrapper. Using a CSS transform string so
31
+ * the existing rotate(0deg)/rotate(180deg) visual is preserved.
32
+ */
33
+ export function getChipArrowRotationWeb(isOpen: boolean) {
34
+ return {
35
+ transform: isOpen ? 'rotate(180deg)' : 'rotate(0deg)',
36
+ };
37
+ }
38
+
39
+ /**
40
+ * Native rotation style for the arrow wrapper. React Native requires
41
+ * transforms to be expressed as an array of individual transform objects.
42
+ */
43
+ export function getChipArrowRotationNative(isOpen: boolean) {
44
+ return {
45
+ transform: [{ rotate: isOpen ? '180deg' : '0deg' }] as Array<{ rotate: string }>,
46
+ };
47
+ }
48
+
49
+ export default staticStyles;
package/src/type.ts ADDED
@@ -0,0 +1,59 @@
1
+ import type { SVGProps } from 'react';
2
+
3
+ import type { Styles } from '@cleartrip/ct-design-types';
4
+ import type { IChipStyleConfig } from '@cleartrip/ct-design-chip';
5
+ import type { DropdownProps } from '@cleartrip/ct-design-dropdown';
6
+
7
+ export interface ChipDropdownOption {
8
+ key: string;
9
+ label: string;
10
+ }
11
+
12
+ /**
13
+ * Style configuration for the ChipDropdown's composable slots. Slot names
14
+ * mirror the previous surface; values are now `Styles[]` arrays aligned with
15
+ * the rest of the migrated components.
16
+ */
17
+ export interface IChipDropdownStyleConfig {
18
+ /** Styles for the outer relative-positioning wrapper Container. */
19
+ root?: Styles[];
20
+ /** Style configuration forwarded to the inner `Chip`. */
21
+ chip?: IChipStyleConfig;
22
+ /** Styles for the absolutely-positioned dropdown wrapper Container. */
23
+ dropdownContainer?: Styles[];
24
+ /** Style configuration forwarded to the inner `Dropdown`. */
25
+ dropdown?: DropdownProps['styleConfig'];
26
+ /** Styles for the arrow icon wrapper (rotation is handled internally). */
27
+ chipArrowWrapper?: Styles[];
28
+ }
29
+
30
+ export interface IChipDropdownProps {
31
+ /** Width forwarded to the inner Dropdown. */
32
+ width: number;
33
+ /** If true, the chip is highlighted as the default-selected option. */
34
+ isDefaultOptionSelected?: boolean;
35
+ /** Fallback label rendered on the chip when no option is selected. */
36
+ defaultChipLabel?: string;
37
+ /** Options displayed inside the dropdown. */
38
+ optionList: DropdownProps['itemList'];
39
+ /** Currently selected option key. */
40
+ selectedOption?: string;
41
+ /** Initial dropdown open state. */
42
+ showDropdown?: boolean;
43
+ /** Callback fired when an option is selected. */
44
+ onClick?: (key: string) => void;
45
+ /** Height forwarded to the inner Dropdown. */
46
+ dropdownHeight?: number;
47
+ /** Props spread onto the chip arrow SVG. */
48
+ chipArrowDownIconProps?: SVGProps<SVGSVGElement>;
49
+ /** Style configuration for different parts of the ChipDropdown. */
50
+ styleConfig?: IChipDropdownStyleConfig;
51
+ }
52
+
53
+ /**
54
+ * @deprecated Use `IChipDropdownProps`. Kept as an alias for legacy consumers
55
+ * that imported `ChipDropdownProps` from `@cleartrip/ct-design-chip-dropdown`.
56
+ * The `styleConfig` slots have been migrated from `{ css, className }`
57
+ * objects to `Styles[]` arrays; legacy call sites will need to migrate.
58
+ */
59
+ export type ChipDropdownProps = IChipDropdownProps;
@@ -1,9 +0,0 @@
1
- import { CSSObject, CSSProperties } from 'styled-components';
2
- import { StyledChipArrowProps } from './type';
3
- export declare const getStyledChipArrowStyles: ({ transform, ...rest }: CSSProperties) => CSSObject;
4
- declare const StyledChipArrow: import("styled-components").StyledComponent<"div", import("styled-components").DefaultTheme, StyledChipArrowProps & {
5
- css?: CSSProperties | undefined;
6
- transform: CSSObject['transform'];
7
- }, never>;
8
- export default StyledChipArrow;
9
- //# sourceMappingURL=StyledChipArrow.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"StyledChipArrow.d.ts","sourceRoot":"","sources":["../../packages/components/ChipDropdown/src/StyledChipArrow/StyledChipArrow.tsx"],"names":[],"mappings":"AAAA,OAAe,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAE,MAAM,QAAQ,CAAC;AAE9C,eAAO,MAAM,wBAAwB,2BAA4B,aAAa,KAAG,SAMhF,CAAC;AAEF,QAAA,MAAM,eAAe;;eAAuE,SAAS,CAAC,WAAW,CAAC;SAMjH,CAAC;AAEF,eAAe,eAAe,CAAC"}
@@ -1,2 +0,0 @@
1
- export { default as StyledChipArrow } from './StyledChipArrow';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../packages/components/ChipDropdown/src/StyledChipArrow/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,mBAAmB,CAAC"}
@@ -1,4 +0,0 @@
1
- export interface StyledChipArrowProps {
2
- transform: string;
3
- }
4
- //# sourceMappingURL=type.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../packages/components/ChipDropdown/src/StyledChipArrow/type.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,oBAAoB;IACnC,SAAS,EAAE,MAAM,CAAC;CACnB"}