Skip to content

Commit 2524b8d

Browse files
committed
refactor: split components into smaller files and keep the selected options
1 parent 006eb98 commit 2524b8d

File tree

8 files changed

+113
-119
lines changed

8 files changed

+113
-119
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { MultiValueProps } from 'react-select/lib/components/MultiValue'
2+
import { MultiSelectOption } from './MultiSelect'
3+
4+
export const CustomMultiValue = ({
5+
color,
6+
data,
7+
removeProps,
8+
}: MultiValueProps<MultiSelectOption> & { color?: string }) => {
9+
return (
10+
<div
11+
className='d-flex align-items-center'
12+
style={{
13+
backgroundColor: color,
14+
borderRadius: '4px',
15+
color: 'white',
16+
fontSize: '12px',
17+
maxWidth: '150px',
18+
overflow: 'hidden',
19+
padding: '2px 6px',
20+
textOverflow: 'ellipsis',
21+
whiteSpace: 'nowrap',
22+
maxHeight: '24px'
23+
}}
24+
>
25+
<span
26+
className='mr-1'
27+
style={{
28+
overflow: 'hidden',
29+
textOverflow: 'ellipsis',
30+
whiteSpace: 'nowrap',
31+
}}
32+
>
33+
{data.label}
34+
</span>
35+
<span
36+
onClick={() => removeProps?.onClick?.(data)}
37+
style={{
38+
cursor: 'pointer',
39+
fontSize: '14px',
40+
lineHeight: '1',
41+
}}
42+
>
43+
×
44+
</span>
45+
</div>
46+
)
47+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
import { OptionProps } from 'react-select/lib/components/Option'
3+
import { MultiSelectOption } from './MultiSelect'
4+
5+
export const CustomOption = ({
6+
children,
7+
color,
8+
...props
9+
}: OptionProps<MultiSelectOption> & { color?: string }) => {
10+
return (
11+
<div
12+
{...props.innerProps}
13+
className={`d-flex align-items-center p-2 ${
14+
props.isFocused ? 'bg-light' : ''
15+
}`}
16+
style={{ cursor: 'pointer' }}
17+
>
18+
{color && (
19+
<div
20+
style={{
21+
backgroundColor: color,
22+
borderRadius: '2px',
23+
flexShrink: 0,
24+
height: '12px',
25+
marginRight: '8px',
26+
width: '12px',
27+
}}
28+
/>
29+
)}
30+
<span>{children}</span>
31+
</div>
32+
)
33+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import classNames from 'classnames'
2-
import React from 'react'
2+
import React, { FC } from 'react'
33
import { MultiValueProps } from 'react-select/lib/components/MultiValue'
4+
import { OptionProps } from 'react-select/lib/components/Option'
5+
6+
import { CustomMultiValue } from './CustomMultiValue'
7+
import { CustomOption } from './CustomOption'
48

59
export interface MultiSelectOption {
610
label: string
@@ -17,84 +21,11 @@ export interface MultiSelectProps {
1721
className?: string
1822
disabled?: boolean
1923
size?: 'small' | 'default' | 'large'
24+
hideSelectedOptions?: boolean
25+
inline?: boolean
2026
}
2127

22-
const CustomMultiValue = ({
23-
color,
24-
data,
25-
removeProps,
26-
}: MultiValueProps<MultiSelectOption> & { color?: string }) => {
27-
return (
28-
<div
29-
className='d-flex align-items-center'
30-
style={{
31-
backgroundColor: color,
32-
borderRadius: '4px',
33-
color: 'white',
34-
fontSize: '12px',
35-
maxWidth: '150px',
36-
overflow: 'hidden',
37-
padding: '2px 6px',
38-
textOverflow: 'ellipsis',
39-
whiteSpace: 'nowrap',
40-
maxHeight: '24px'
41-
}}
42-
>
43-
<span
44-
className='mr-1'
45-
style={{
46-
overflow: 'hidden',
47-
textOverflow: 'ellipsis',
48-
whiteSpace: 'nowrap',
49-
}}
50-
>
51-
{data.label}
52-
</span>
53-
<span
54-
onClick={() => removeProps?.onClick?.(data)}
55-
style={{
56-
cursor: 'pointer',
57-
fontSize: '14px',
58-
lineHeight: '1',
59-
}}
60-
>
61-
×
62-
</span>
63-
</div>
64-
)
65-
}
66-
67-
const CustomOption = ({
68-
children,
69-
color,
70-
...props
71-
}: any & { color?: string }) => {
72-
return (
73-
<div
74-
{...props.innerProps}
75-
className={`d-flex align-items-center p-2 ${
76-
props.isFocused ? 'bg-light' : ''
77-
}`}
78-
style={{ cursor: 'pointer' }}
79-
>
80-
{color && (
81-
<div
82-
style={{
83-
backgroundColor: color,
84-
borderRadius: '2px',
85-
flexShrink: 0,
86-
height: '12px',
87-
marginRight: '8px',
88-
width: '12px',
89-
}}
90-
/>
91-
)}
92-
<span>{children}</span>
93-
</div>
94-
)
95-
}
96-
97-
const MultiSelect: React.FC<MultiSelectProps> = ({
28+
export const MultiSelect: FC<MultiSelectProps> = ({
9829
className = '',
9930
colorMap,
10031
disabled = false,
@@ -104,6 +35,7 @@ const MultiSelect: React.FC<MultiSelectProps> = ({
10435
placeholder = 'Select options...',
10536
selectedValues,
10637
size = 'default',
38+
hideSelectedOptions = false,
10739
}) => {
10840
return (
10941
<div className={classNames(
@@ -113,25 +45,25 @@ const MultiSelect: React.FC<MultiSelectProps> = ({
11345
{label && <label>{label}</label>}
11446
<Select
11547
isMulti
48+
hideSelectedOptions={hideSelectedOptions}
11649
closeMenuOnSelect={false}
11750
placeholder={placeholder}
11851
isDisabled={disabled}
11952
size={size}
120-
onChange={(selectedOptions: any) => {
53+
onChange={(selectedOptions: MultiSelectOption[]) => {
12154
const values = selectedOptions
122-
? selectedOptions.map((opt: any) => opt.value)
55+
? selectedOptions.map((opt: MultiSelectOption) => opt.value)
12356
: []
12457
onSelectionChange(values)
12558
}}
12659
components={{
12760
MultiValue: (props: MultiValueProps<MultiSelectOption>) => (
12861
<CustomMultiValue
12962
{...props}
130-
className={classNames(props.className, 'h-[24px]')}
13163
color={colorMap?.get(props.data.value) || '#5D6D7E'}
13264
/>
13365
),
134-
...(colorMap ? {Option: (props: any) => <CustomOption
66+
...(colorMap ? {Option: (props: OptionProps<MultiSelectOption>) => <CustomOption
13567
{...props}
13668
color={colorMap.get(props.data.value)}
13769
/>} : {}),
@@ -154,15 +86,20 @@ const MultiSelect: React.FC<MultiSelectProps> = ({
15486
}),
15587
valueContainer: (base: any) => ({
15688
...base,
157-
flexWrap: 'wrap',
89+
flexWrap: 'nowrap',
15890
gap: '2px',
15991
paddingBottom: '6px',
16092
paddingTop: '6px',
93+
flex: 1
94+
}),
95+
input: (base: any) => ({
96+
...base,
97+
margin: 0,
98+
paddingBottom: 0,
99+
paddingTop: 0,
161100
}),
162101
}}
163102
/>
164103
</div>
165104
)
166105
}
167-
168-
export default MultiSelect
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { MultiSelect } from './MultiSelect'

frontend/web/components/organisation-settings/usage/OrganisationUsageMetrics.container.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useMemo, useState } from 'react'
22
import { Res } from 'common/types/responses'
33
import SingleSDKLabelsChart from './components/SingleSDKLabelsChart'
4-
import MultiSelect from 'components/base/select/MultiSelect'
4+
import { MultiSelect } from 'components/base/select/multi-select'
55

66
export interface OrganisationUsageMetricsProps {
77
data?: Res['organisationUsage']

frontend/web/components/segments/Rule/Rule.tsx

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
splitIfValue,
1717
isInvalidPercentageSplit,
1818
} from './utils'
19-
import { useConditionCache } from './hooks/useConditionCache'
2019

2120
interface RuleProps {
2221
index: number
@@ -42,8 +41,6 @@ const Rule: React.FC<RuleProps> = ({
4241
}) => {
4342
const { conditions: rules } = rule
4443

45-
const { cacheValue, getCachedValue } = useConditionCache()
46-
4744
const updateCondition = (
4845
conditionIndex: number,
4946
updates: Partial<SegmentCondition>,
@@ -59,15 +56,7 @@ const Rule: React.FC<RuleProps> = ({
5956
}
6057

6158
const setConditionProperty = (conditionIndex: number, property: string) => {
62-
const condition = rule.conditions[conditionIndex]
63-
64-
if (condition.property) {
65-
cacheValue(conditionIndex, condition.operator, condition.property, condition.value)
66-
}
67-
68-
const cachedValue = getCachedValue(conditionIndex, condition.operator, property)
69-
70-
updateCondition(conditionIndex, { property, value: cachedValue })
59+
updateCondition(conditionIndex, { property })
7160
}
7261

7362
const setConditionOperator = (
@@ -82,15 +71,18 @@ const Rule: React.FC<RuleProps> = ({
8271
)
8372
const newOperator = operators.find((op) => op.value === operatorValue)
8473

85-
if (condition.operator && condition.property) {
86-
cacheValue(conditionIndex, condition.operator, condition.property, condition.value)
87-
}
88-
8974
// Split the append part of the operator as not handled by backend
9075
const updates: Partial<SegmentCondition> = {
9176
operator: operatorValue?.split(':')[0],
9277
}
9378

79+
// Clear value when changing to or from IN operator
80+
const isChangingToIn = operatorValue === 'IN'
81+
const isChangingFromIn = condition?.operator === 'IN'
82+
if (isChangingToIn || isChangingFromIn) {
83+
updates.value = ''
84+
}
85+
9486
if (newOperator?.hideValue) {
9587
updates.value = null
9688
}
@@ -118,22 +110,6 @@ const Rule: React.FC<RuleProps> = ({
118110
}
119111
}
120112

121-
const finalProperty = updates.property !== undefined ? updates.property : condition.property
122-
const finalOperator = operatorValue?.split(':')[0]
123-
124-
if (finalProperty && !newOperator?.hideValue && operatorValue !== 'PERCENTAGE_SPLIT') {
125-
const cachedValue = getCachedValue(conditionIndex, finalOperator, finalProperty)
126-
127-
if (cachedValue !== '') {
128-
// Use cached value if one exists (restore previous input)
129-
updates.value = cachedValue
130-
} else if (!updates.value) {
131-
// No cache and no value set by other logic - clear the input
132-
updates.value = ''
133-
}
134-
// Otherwise: preserve value set by append logic or existing value
135-
}
136-
137113
updateCondition(conditionIndex, updates)
138114
}
139115

frontend/web/components/segments/Rule/components/RuleConditionValueInput.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getDarkMode } from 'project/darkMode'
77
import EnvironmentSelectDropdown from './EnvironmentSelectDropdown'
88
import TextAreaModal from './TextAreaModal'
99
import { checkWhitespaceIssues } from 'components/segments/Rule/utils'
10-
import MultiSelect from 'components/base/select/MultiSelect'
10+
import { MultiSelect } from 'components/base/select/multi-select'
1111
import { safeParseArray } from 'components/segments/Rule/utils/arrayUtils'
1212
import { useGetEnvironmentsQuery } from 'common/services/useEnvironment'
1313
import { useConditionInputType } from 'components/segments/Rule/hooks/useConditionInputType'

frontend/web/styles/3rdParty/_react-select.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
}
9595
&__control {
9696
background: $input-bg;
97-
min-height: $input-height;
97+
height: $input-height;
9898
border-radius: $border-radius;
9999
border: 1px solid $input-border-color;
100100
&--is-disabled {

0 commit comments

Comments
 (0)