Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components/molecule/dataCounter): Allow input to be edited #2784

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions components/molecule/dataCounter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const MoleculeDataCounter = forwardRef(
size = inputSizes.MEDIUM,
substractIcon = '-',
initialValue,
value
value,
allowCustomValue = false
},
ref
) => {
Expand Down Expand Up @@ -65,14 +66,21 @@ const MoleculeDataCounter = forwardRef(
const decrementDisabled = disabled || internalValue <= numMin
const incrementDisabled = disabled || internalValue >= numMax

const sanitize = value => {
if (!value) return 0
if (isNaN(value)) return 0
return value
}

const assignDiff = (event, {diff, lastAction}) => {
setInternalValue(currentValue => {
const finalValue = allowCustomValue && lastAction === 'change' ? sanitize(diff) : currentValue + diff
typeof onChange === 'function' &&
onChange(event, {
value: String(currentValue + diff),
value: String(finalValue),
action: lastAction
})
return value === undefined ? currentValue + diff : currentValue
return value === undefined ? finalValue : currentValue
})
lastAction && setLastActions(lastAction)
}
Expand All @@ -90,6 +98,17 @@ const MoleculeDataCounter = forwardRef(
}

const handleChange = (event, {value}) => {
if (allowCustomValue) {
const parsedIntNewValue = parseInt(value, 10)

assignDiff(event, {
diff: parsedIntNewValue,
lastAction: ACTIONS.CHANGE
})

return
}

const parsedIntNewValue = parseInt(value, 10)

const diffValue = isNaN(parsedIntNewValue) && 0
Expand Down Expand Up @@ -228,7 +247,10 @@ MoleculeDataCounter.propTypes = {
addIcon: PropTypes.node,

/** Icon to show on substract button */
substractIcon: PropTypes.node
substractIcon: PropTypes.node,

/** Flag to allow the input value to be editable */
allowCustomValue: PropTypes.bool
}

export default MoleculeDataCounter
Expand Down
77 changes: 77 additions & 0 deletions components/molecule/dataCounter/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,83 @@ describe(json.name, () => {
expect(input.value).to.equal(`${initialValue}`)
})

it('given allowCustomValue we can directly write the value', () => {
// Given
const spy = sinon.spy()
const props = {
charsSize: 10,
label: 'label',
minValueHelpText: 'minValueHelpText',
minValueErrorText: 'minValueErrorText',
maxValueHelpText: 'maxValueHelpText',
maxValueErrorText: 'maxValueErrorText',
onChange: spy,
min: 0,
max: 2,
initialValue: 2,
allowCustomValue: true
}
const {initialValue} = props

// When
const {getByRole} = setup(props)
const input = getByRole('textbox')
// Then
expect(input.value).to.equal(`${initialValue}`)

// And
// When
fireEvent.change(input, {target: {value: '44'}})
expect(input.value).to.equal('44')

fireEvent.change(input, {target: {value: '66'}})
expect(input.value).to.equal('66')

fireEvent.change(input, {target: {value: '4-'}})
expect(input.value).to.equal('4')

fireEvent.change(input, {target: {value: ''}})
expect(input.value).to.equal('0')

fireEvent.change(input, {target: {value: '-'}})
expect(input.value).to.equal('0')
})

it('given allowCustomValue false we cannot directly write the value', () => {
// Given
const spy = sinon.spy()
const props = {
charsSize: 10,
label: 'label',
minValueHelpText: 'minValueHelpText',
minValueErrorText: 'minValueErrorText',
maxValueHelpText: 'maxValueHelpText',
maxValueErrorText: 'maxValueErrorText',
onChange: spy,
min: 0,
max: 2,
initialValue: 2
}
const {initialValue} = props

// When
const {getByRole} = setup(props)
const input = getByRole('textbox')
// Then
expect(input.value).to.equal(`${initialValue}`)

// And
// When
fireEvent.change(input, {target: {value: '44'}})
expect(input.value).to.equal(`${initialValue}`)

fireEvent.change(input, {target: {value: ''}})
expect(input.value).to.equal(`${initialValue}`)

fireEvent.change(input, {target: {value: '-'}})
expect(input.value).to.equal(`${initialValue}`)
})

it('should not allow to reach NaN values', () => {
// Given
const {getByRole, getByDisplayValue} = setup({value: 10})
Expand Down
Loading