mirror of
https://github.com/dzeiocom/components.git
synced 2025-06-08 09:09:54 +00:00
feat: handle number input better
Signed-off-by: Florian BOUILLON <f.bouillon@aptatio.com>
This commit is contained in:
parent
d5ba10eb31
commit
4066de1bb0
@ -1,8 +1,8 @@
|
|||||||
import { Meta } from '@storybook/react/types-6-0'
|
|
||||||
import { Story } from "@storybook/react"
|
import { Story } from "@storybook/react"
|
||||||
|
import { Meta } from '@storybook/react/types-6-0'
|
||||||
|
import { X } from 'lucide-react'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import Component from '.'
|
import Component from '.'
|
||||||
import { X } from 'lucide-react'
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
title: 'DZEIO/Input',
|
title: 'DZEIO/Input',
|
||||||
@ -20,6 +20,7 @@ tmp.args = {
|
|||||||
// icon: X,
|
// icon: X,
|
||||||
// transformer: (v: string) => v + 1
|
// transformer: (v: string) => v + 1
|
||||||
// },
|
// },
|
||||||
|
min: 0,
|
||||||
id: 'pouet',
|
id: 'pouet',
|
||||||
type: 'number',
|
type: 'number',
|
||||||
step: 10,
|
step: 10,
|
||||||
|
@ -2,10 +2,10 @@ import React, { FocusEvent } from 'react'
|
|||||||
|
|
||||||
import { objectEqual, objectOmit } from '@dzeio/object-util'
|
import { objectEqual, objectOmit } from '@dzeio/object-util'
|
||||||
import { ChevronDown, MinusSquare, PlusSquare } from 'lucide-react'
|
import { ChevronDown, MinusSquare, PlusSquare } from 'lucide-react'
|
||||||
import { Icon } from '../interfaces'
|
|
||||||
import Menu from '../Menu'
|
import Menu from '../Menu'
|
||||||
import Text from '../Text'
|
import Text from '../Text'
|
||||||
import { buildClassName } from '../Util'
|
import { buildClassName } from '../Util'
|
||||||
|
import { Icon } from '../interfaces'
|
||||||
import css from './Input.module.styl'
|
import css from './Input.module.styl'
|
||||||
|
|
||||||
interface Props extends React.DetailedHTMLProps<React.InputHTMLAttributes<HTMLInputElement>, HTMLInputElement> {
|
interface Props extends React.DetailedHTMLProps<React.InputHTMLAttributes<HTMLInputElement>, HTMLInputElement> {
|
||||||
@ -129,15 +129,10 @@ export default class Input extends React.Component<Props, States> {
|
|||||||
if (this.props.choices) {
|
if (this.props.choices) {
|
||||||
const choice = this.props.choices.find((it) => typeof it === 'string' ? it : it.value === this.props.value?.toString())
|
const choice = this.props.choices.find((it) => typeof it === 'string' ? it : it.value === this.props.value?.toString())
|
||||||
if (choice) {
|
if (choice) {
|
||||||
this.setState({
|
this.onChange(typeof choice === 'string' ? choice : choice.value)
|
||||||
displayedValue: typeof choice === 'string' ? choice : choice.display,
|
|
||||||
value: typeof choice === 'string' ? choice : choice.value,
|
|
||||||
valueUpdate: true
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.setState({ displayedValue: this.props.value?.toString(), value : this.props.value?.toString(), valueUpdate: true })
|
this.onChange(this.props.value?.toString() ?? '')
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (prevStates.value !== this.state.value) {
|
if (prevStates.value !== this.state.value) {
|
||||||
@ -354,7 +349,7 @@ export default class Input extends React.Component<Props, States> {
|
|||||||
return <Icon.icon size={16*2+18} className={buildClassName(css[position], css.iconClickable)} onClick={async () => {
|
return <Icon.icon size={16*2+18} className={buildClassName(css[position], css.iconClickable)} onClick={async () => {
|
||||||
if (this.props.disabled) {return}
|
if (this.props.disabled) {return}
|
||||||
const value = Icon.transformer(this.state.value ?? this.state.displayedValue ?? '')
|
const value = Icon.transformer(this.state.value ?? this.state.displayedValue ?? '')
|
||||||
this.setState({ value: value, displayedValue: value, valueUpdate: true })
|
this.onChange(value)
|
||||||
}} />
|
}} />
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -377,14 +372,23 @@ export default class Input extends React.Component<Props, States> {
|
|||||||
*/
|
*/
|
||||||
private onChange = async (event: React.ChangeEvent<HTMLInputElement>|string) => {
|
private onChange = async (event: React.ChangeEvent<HTMLInputElement>|string) => {
|
||||||
// get the input
|
// get the input
|
||||||
const value = typeof event === 'string' ? event : event.currentTarget.value
|
let value = typeof event === 'string' ? event : event.currentTarget.value
|
||||||
// console.log("onChange", value)
|
|
||||||
|
|
||||||
if (typeof value !== 'string') {
|
if (typeof value !== 'string') {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.props.type === 'number') {
|
||||||
|
const val = parseFloat(value)
|
||||||
|
const min = typeof this.props.min !== 'undefined' ? typeof this.props.min === 'string' ? parseFloat(this.props.min) : this.props.min : -Infinity
|
||||||
|
const max = typeof this.props.max !== 'undefined' ? typeof this.props.max === 'string' ? parseFloat(this.props.max) : this.props.max : Infinity
|
||||||
|
console.log('pouet', val, this.props.min, min, this.props.max, max)
|
||||||
|
value = Math.min(max, Math.max(min, val)).toString()
|
||||||
|
}
|
||||||
|
console.log("onChange", value, this.props.type)
|
||||||
|
|
||||||
if (this.props.onChange && typeof event !== 'string') {
|
if (this.props.onChange && typeof event !== 'string') {
|
||||||
|
event.currentTarget.value = value
|
||||||
this.props.onChange(event)
|
this.props.onChange(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user