Skip to content

Commit 84a3d21

Browse files
authored
RI-7050 replace EUI field number with NumericInput (#4607)
* export redis ui numeric input * replace eui field number with numeric input * remove no longer used validators * add better test names and some more tests for the numeric input behavior when strings are provided
1 parent 5fc5800 commit 84a3d21

File tree

17 files changed

+208
-252
lines changed

17 files changed

+208
-252
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { NumericInput } from '@redis-ui/components'
2+
3+
export default NumericInput
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { default as PasswordInput } from './PasswordInput'
22
export { default as SearchInput } from './SearchInput'
3+
export { default as NumericInput } from './NumericInput'

redisinsight/ui/src/components/input-field-sentinel/InputFieldSentinel.spec.tsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('InputFieldSentinel', () => {
6565
expect(screen.getByTestId(inputNumberTestId)).toBeInTheDocument()
6666
})
6767

68-
it('should change Number field properly', () => {
68+
it('should default to 0 when Number field properly is set to string with letters and Number field was not previously set', () => {
6969
render(
7070
<InputFieldSentinel
7171
{...instance(mockedProps)}
@@ -75,6 +75,37 @@ describe('InputFieldSentinel', () => {
7575
fireEvent.change(screen.getByTestId(inputNumberTestId), {
7676
target: { value: 'val13' },
7777
})
78+
expect(screen.getByTestId(inputNumberTestId)).toHaveValue('0')
79+
})
80+
81+
it('should default to previous value when Number field properly is set to string with letters', () => {
82+
render(
83+
<InputFieldSentinel
84+
{...instance(mockedProps)}
85+
inputType={SentinelInputFieldType.Number}
86+
/>,
87+
)
88+
fireEvent.change(screen.getByTestId(inputNumberTestId), {
89+
target: { value: '1' },
90+
})
91+
expect(screen.getByTestId(inputNumberTestId)).toHaveValue('1')
92+
93+
fireEvent.change(screen.getByTestId(inputNumberTestId), {
94+
target: { value: 'val13' },
95+
})
96+
expect(screen.getByTestId(inputNumberTestId)).toHaveValue('1')
97+
})
98+
99+
it('should set Number field properly when is set to string with numbers only', () => {
100+
render(
101+
<InputFieldSentinel
102+
{...instance(mockedProps)}
103+
inputType={SentinelInputFieldType.Number}
104+
/>,
105+
)
106+
fireEvent.change(screen.getByTestId(inputNumberTestId), {
107+
target: { value: '13' },
108+
})
78109
expect(screen.getByTestId(inputNumberTestId)).toHaveValue('13')
79110
})
80111
})

redisinsight/ui/src/components/input-field-sentinel/InputFieldSentinel.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import {
22
EuiFieldText,
33
EuiIcon,
4-
EuiFieldNumber,
54
} from '@elastic/eui'
65
import { omit } from 'lodash'
76
import React, { useState } from 'react'
87
import cx from 'classnames'
98
import { useDebouncedEffect } from 'uiSrc/services'
10-
import { validateNumber } from 'uiSrc/utils'
11-
import { PasswordInput } from 'uiSrc/components/base/inputs'
9+
import { NumericInput, PasswordInput } from 'uiSrc/components/base/inputs'
1210

1311
import styles from './styles.module.scss'
1412

@@ -76,12 +74,11 @@ const InputFieldSentinel = (props: Props) => {
7674
/>
7775
)}
7876
{inputType === SentinelInputFieldType.Number && (
79-
<EuiFieldNumber
77+
<NumericInput
8078
{...clearProp}
81-
compressed
82-
type="text"
83-
value={value}
84-
onChange={(e) => handleChange(validateNumber(e.target?.value))}
79+
autoValidate
80+
value={Number(value)}
81+
onChange={(value) => handleChange(value ? value.toString() : '')}
8582
data-testid="sentinel-input-number"
8683
/>
8784
)}

redisinsight/ui/src/components/instance-header/InstanceHeader.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import React, { useEffect, useState } from 'react'
22
import { useDispatch, useSelector } from 'react-redux'
33
import { useHistory } from 'react-router-dom'
44
import cx from 'classnames'
5-
import { EuiFieldNumber, EuiIcon, EuiToolTip } from '@elastic/eui'
5+
import { EuiIcon, EuiToolTip } from '@elastic/eui'
66

77
import { FeatureFlags, Pages } from 'uiSrc/constants'
8-
import { selectOnFocus, validateNumber } from 'uiSrc/utils'
8+
import { selectOnFocus } from 'uiSrc/utils'
99
import { sendEventTelemetry, TelemetryEvent } from 'uiSrc/telemetry'
1010
import { BuildType } from 'uiSrc/constants/env'
1111
import { ConnectionType } from 'uiSrc/slices/interfaces'
@@ -38,6 +38,7 @@ import { FlexItem, Row } from 'uiSrc/components/base/layout/flex'
3838
import { EmptyButton } from 'uiSrc/components/base/forms/buttons'
3939
import { EditIcon } from 'uiSrc/components/base/icons'
4040
import { Text } from 'uiSrc/components/base/text'
41+
import { NumericInput } from 'uiSrc/components/base/inputs'
4142
import InstancesNavigationPopover from './components/instances-navigation-popover'
4243
import styles from './styles.module.scss'
4344

@@ -221,20 +222,17 @@ const InstanceHeader = ({ onChangeDbIndex }: Props) => {
221222
viewChildrenMode={false}
222223
controlsClassName={styles.controls}
223224
>
224-
<EuiFieldNumber
225+
<NumericInput
226+
autoSize
227+
autoValidate
228+
min={0}
225229
onFocus={selectOnFocus}
226-
onChange={(e) =>
227-
setDbIndex(
228-
validateNumber(e.target.value.trim()),
229-
)
230+
onChange={(value) =>
231+
setDbIndex(value ? value.toString() : '')
230232
}
231-
value={dbIndex}
233+
value={Number(dbIndex)}
232234
placeholder="Database Index"
233-
className={styles.input}
234-
fullWidth={false}
235-
compressed
236-
autoComplete="off"
237-
type="text"
235+
className={styles.dbIndexInput}
238236
data-testid="change-index-input"
239237
/>
240238
</InlineItemEditor>

redisinsight/ui/src/components/instance-header/styles.module.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
height: 32px !important;
6868
}
6969

70-
.input {
70+
.dbIndexInput {
7171
width: 60px !important;
7272
}
7373

redisinsight/ui/src/components/settings-item/SettingItem.tsx

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import React, { ChangeEvent, useEffect, useState } from 'react'
1+
import React, { useEffect, useState } from 'react'
22
import cx from 'classnames'
3-
import { EuiFieldNumber, EuiIcon, EuiTitle } from '@elastic/eui'
3+
import { EuiTitle } from '@elastic/eui'
44

55
import InlineItemEditor from 'uiSrc/components/inline-item-editor/InlineItemEditor'
66

77
import { FlexItem, Row } from 'uiSrc/components/base/layout/flex'
88
import { Spacer } from 'uiSrc/components/base/layout/spacer'
99
import { Text } from 'uiSrc/components/base/text'
10+
import { NumericInput } from 'uiSrc/components/base/inputs'
11+
import { EditIcon } from 'uiSrc/components/base/icons'
1012
import styles from './styles.module.scss'
1113

1214
export interface Props {
@@ -54,28 +56,19 @@ const SettingItem = (props: Props) => {
5456
setHovering(false)
5557
}
5658

57-
const onChange = ({
58-
currentTarget: { value },
59-
}: ChangeEvent<HTMLInputElement>) => {
60-
isEditing && setValue(validation(value))
61-
}
62-
63-
const appendEditing = () =>
64-
!isEditing ? <EuiIcon type="pencil" color="subdued" /> : ''
65-
6659
return (
6760
<>
6861
<EuiTitle className={styles.title} size="xxs">
6962
<span>{title}</span>
7063
</EuiTitle>
7164
<Spacer size="s" />
72-
<Text className={styles.smallText} size="s" color="subdued">
65+
<Text className={styles.smallText} size="s">
7366
{summary}
7467
</Text>
7568
<Spacer size="m" />
7669
<Row align="center" className={styles.container}>
7770
<FlexItem style={{ marginRight: '4px' }}>
78-
<Text size="xs" color="subdued" className={styles.inputLabel}>
71+
<Text size="xs" className={styles.inputLabel}>
7972
{label}
8073
</Text>
8174
</FlexItem>
@@ -84,7 +77,7 @@ const SettingItem = (props: Props) => {
8477
onMouseEnter={() => setHovering(true)}
8578
onMouseLeave={() => setHovering(false)}
8679
onClick={() => setEditing(true)}
87-
style={{ paddingBottom: '1px' }}
80+
style={{ width: '200px' }}
8881
>
8982
{isEditing || isHovering ? (
9083
<InlineItemEditor
@@ -94,22 +87,29 @@ const SettingItem = (props: Props) => {
9487
onDecline={handleDeclineChanges}
9588
declineOnUnmount={false}
9689
>
97-
<EuiFieldNumber
98-
onChange={onChange}
99-
value={value}
100-
placeholder={placeholder}
101-
aria-label={testid?.replaceAll?.('-', ' ')}
102-
className={cx(styles.input, {
103-
[styles.inputEditing]: isEditing,
90+
<div
91+
className={cx({
92+
[styles.inputHover]: isHovering,
10493
})}
105-
append={appendEditing()}
106-
fullWidth={false}
107-
compressed
108-
autoComplete="off"
109-
type="text"
110-
readOnly={!isEditing}
111-
data-testid={`${testid}-input`}
112-
/>
94+
>
95+
<NumericInput
96+
autoValidate
97+
onChange={(value) =>
98+
isEditing &&
99+
setValue(validation(value ? value.toString() : ''))
100+
}
101+
value={Number(value)}
102+
placeholder={placeholder}
103+
aria-label={testid?.replaceAll?.('-', ' ')}
104+
className={cx(styles.input, {
105+
[styles.inputEditing]: isEditing,
106+
})}
107+
readOnly={!isEditing}
108+
data-testid={`${testid}-input`}
109+
style={{ width: '100%' }}
110+
/>
111+
{!isEditing && <EditIcon />}
112+
</div>
113113
</InlineItemEditor>
114114
) : (
115115
<Text className={styles.value} data-testid={`${testid}-value`}>

redisinsight/ui/src/components/settings-item/styles.module.scss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77
height: 32px !important;
88
}
99

10+
.inputHover {
11+
display: flex;
12+
flex-direction: row;
13+
align-items: center;
14+
justify-content: space-between;
15+
padding-left: 10px;
16+
padding-bottom: 5px;
17+
18+
& > * {
19+
line-height: 3.1rem !important;
20+
}
21+
}
22+
1023
.container {
1124
height: 40px;
1225

0 commit comments

Comments
 (0)