Skip to content

Commit

Permalink
feat(NumberInput): Replace deprecated implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
m7kvqbe1 committed Feb 21, 2022
1 parent cd7d589 commit bb26195
Show file tree
Hide file tree
Showing 50 changed files with 738 additions and 2,473 deletions.
4 changes: 2 additions & 2 deletions packages/react-component-library/cypress/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import contextMenu from './ContextMenu'
import datePicker from './DatePicker'
import dismissibleBanner from './DismissibleBanner'
import form from './form'
import numberInputE from './NumberInputE'
import NumberInput from './NumberInput'
import rangeSliderE from './RangeSliderE'
import selectE from './SelectE'
import timeline from './timeline'
Expand All @@ -18,7 +18,7 @@ export default {
datePicker,
dismissibleBanner,
form,
numberInputE,
NumberInput,
rangeSliderE,
selectE,
timeline,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@ import { before, cy, describe, it } from 'local-cypress'

import selectors from '../../selectors'

describe('NumberInputE', () => {
describe('NumberInput', () => {
beforeEach(() => {
cy.visit(
'/iframe.html?id=number-input-experimental--default&viewMode=story'
)
cy.visit('/iframe.html?id=number-input--default&viewMode=story')
})

it('renders the number input without a value', () => {
cy.get(selectors.numberInputE.input).should('have.value', '')
cy.get(selectors.NumberInput.input).should('have.value', '')
})

describe('when the increase button is clicked', () => {
beforeEach(() => {
cy.get(selectors.numberInputE.increase).click()
cy.get(selectors.NumberInput.increase).click()
})

it('should increment the value', () => {
cy.get(selectors.numberInputE.input).should('have.value', '1')
cy.get(selectors.NumberInput.input).should('have.value', '1')
})
})

describe('when the decrease button is clicked', () => {
beforeEach(() => {
cy.get(selectors.numberInputE.decrease).click()
cy.get(selectors.NumberInput.decrease).click()
})

it('should decrement the value', () => {
cy.get(selectors.numberInputE.input).should('have.value', '-1')
cy.get(selectors.NumberInput.input).should('have.value', '-1')
})
})

Expand All @@ -48,11 +46,11 @@ describe('NumberInputE', () => {
TYPED_VALUE_CASES.forEach(([textToType, expectedValue]) => {
describe(`when typing "${textToType}"`, () => {
beforeEach(() => {
cy.get(selectors.numberInputE.input).type(textToType)
cy.get(selectors.NumberInput.input).type(textToType)
})

it(`the value is "${expectedValue}"`, () => {
cy.get(selectors.numberInputE.input).should('have.value', expectedValue)
cy.get(selectors.NumberInput.input).should('have.value', expectedValue)
})
})
})
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,85 +1,58 @@
import React from 'react'
import { isFinite, isNil } from 'lodash'
import { selectors } from '@defencedigital/design-tokens'
import { isNil } from 'lodash'

import { NumberInputUnit } from './NumberInputUnit'
import { UnitPosition } from './NumberInput'
import { useInputText } from './useInputText'
import { InputValidationProps } from '../../common/InputValidationProps'
import { StyledInput } from './partials/StyledInput'
import { StyledLabel } from './partials/StyledLabel'
import { ComponentSizeType } from '../Forms'
import { StyledInput } from '../TextInputE/partials/StyledInput'
import { StyledInputWrapper } from './partials/StyledInputWrapper'
import { StyledLabel } from '../TextInputE/partials/StyledLabel'

export interface InputProps extends InputValidationProps {
export interface InputProps {
hasFocus: boolean
isDisabled?: boolean
id?: string
isCondensed: boolean
label?: string
name: string
onBeforeInput: (event: React.FormEvent<HTMLInputElement>) => void
onBlur: (event: React.FormEvent<HTMLInputElement>) => void
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void
onFocus: (event: React.FormEvent<HTMLInputElement>) => void
onPaste: (event: React.ClipboardEvent<HTMLInputElement>) => void
placeholder?: string
unit: string
unitPosition: UnitPosition
value?: number
size: ComponentSizeType
value?: string | null
}

const { spacing } = selectors

export const Input: React.FC<InputProps> = ({
hasFocus,
isDisabled = false,
isDisabled,
id,
isCondensed,
label,
placeholder = '',
unit,
unitPosition,
size,
value,
...rest
}) => {
const hasLabel = !!(label && label.length)
const { canShow, inputOffset, inputRef, unitOffset } = useInputText(
value,
unitPosition
)

return (
<StyledInputWrapper>
{hasLabel && (
<StyledLabel
$hasContent={!isNil(value)}
$hasFocus={hasFocus}
$hasUnit={!isNil(unit)}
data-testid="number-input-label"
$size={size}
htmlFor={id}
data-testid="number-input-label"
>
{label}
</StyledLabel>
)}

{canShow && (
<NumberInputUnit
left={`${unitOffset}px`}
top={hasLabel && spacing('4')}
>
{unit}
</NumberInputUnit>
)}

<StyledInput
$hasLabel={hasLabel}
$isCondensed={isCondensed}
$offset={inputOffset}
$size={size}
data-testid="number-input-input"
disabled={isDisabled}
id={id}
placeholder={placeholder}
ref={inputRef}
type="text"
value={isFinite(value) && canShow ? value : ''}
value={value || ''}
{...rest}
/>
</StyledInputWrapper>
Expand Down
Loading

0 comments on commit bb26195

Please sign in to comment.