feat: QInput 컴포넌트에 number type 추가
This commit is contained in:
parent
231255bf0e
commit
708f6c8ea1
@ -39,6 +39,7 @@ export default function Playground() {
|
|||||||
const [color, setColor] = useState('#ff0000')
|
const [color, setColor] = useState('#ff0000')
|
||||||
|
|
||||||
const [textInput, setTextInput] = useState('')
|
const [textInput, setTextInput] = useState('')
|
||||||
|
const [numberInput, setNumberInput] = useState(null)
|
||||||
const [radioInput, setRadioInput] = useState('')
|
const [radioInput, setRadioInput] = useState('')
|
||||||
const [checkboxInput, setCheckboxInput] = useState([])
|
const [checkboxInput, setCheckboxInput] = useState([])
|
||||||
const [selectedValue, setSelectedValue] = useState('')
|
const [selectedValue, setSelectedValue] = useState('')
|
||||||
@ -48,6 +49,9 @@ export default function Playground() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log('textInput:', textInput)
|
console.log('textInput:', textInput)
|
||||||
}, [textInput])
|
}, [textInput])
|
||||||
|
useEffect(() => {
|
||||||
|
console.log('numberInput:', numberInput)
|
||||||
|
}, [numberInput])
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log('radioInput:', radioInput)
|
console.log('radioInput:', radioInput)
|
||||||
}, [radioInput])
|
}, [radioInput])
|
||||||
@ -161,8 +165,19 @@ export default function Playground() {
|
|||||||
>
|
>
|
||||||
QInput TextInput DATA RESET
|
QInput TextInput DATA RESET
|
||||||
</button>
|
</button>
|
||||||
<QInput type="text" value={textInput} onChange={setTextInput} />
|
<QInput type="text" placeholder="placeholder" value={textInput} onChange={setTextInput} />
|
||||||
<QInput type="text" value={textInput} onChange={setTextInput} readOnly="true" />
|
<QInput type="text" placeholder="read only" value={textInput} onChange={setTextInput} readOnly="true" />
|
||||||
|
<br />
|
||||||
|
<button
|
||||||
|
className="btn-frame deepgray"
|
||||||
|
onClick={() => {
|
||||||
|
setNumberInput(null)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
QInput NumberInput DATA RESET
|
||||||
|
</button>
|
||||||
|
<QInput type="number" placeholder="placeholder" value={numberInput} onChange={setNumberInput} />
|
||||||
|
<QInput type="number" placeholder="read only" value={numberInput} onChange={setNumberInput} readOnly="true" />
|
||||||
<br />
|
<br />
|
||||||
<button
|
<button
|
||||||
className="btn-frame deepgray"
|
className="btn-frame deepgray"
|
||||||
@ -185,7 +200,7 @@ export default function Playground() {
|
|||||||
<button
|
<button
|
||||||
className="btn-frame deepgray"
|
className="btn-frame deepgray"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setCheckboxInput('')
|
setCheckboxInput([])
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
QInput Checkbox DATA RESET
|
QInput Checkbox DATA RESET
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
|
|
||||||
export default function QInput({ type, readOnly = false, options = [], value, onChange }) {
|
export default function QInput({ type, className, ref, id, readOnly = false, options = [], placeholder, value, onChange }) {
|
||||||
// options = options || [
|
// options = options || [
|
||||||
// {
|
// {
|
||||||
// id: 'one',
|
// id: 'one',
|
||||||
@ -21,11 +21,11 @@ export default function QInput({ type, readOnly = false, options = [], value, on
|
|||||||
// },
|
// },
|
||||||
// ]
|
// ]
|
||||||
|
|
||||||
const handleChange = useCallback(
|
const handleRadioCheckboxChange = useCallback(
|
||||||
(e, optionValue) => {
|
(e, optionValue) => {
|
||||||
if (type === 'radio') {
|
if (type === 'radio') {
|
||||||
onChange(e.target.value)
|
onChange(e.target.value)
|
||||||
} else {
|
} else if (type === 'checkbox') {
|
||||||
const newValue = value.includes(optionValue) ? value.filter((v) => v !== optionValue) : [...value, optionValue]
|
const newValue = value.includes(optionValue) ? value.filter((v) => v !== optionValue) : [...value, optionValue]
|
||||||
onChange(newValue)
|
onChange(newValue)
|
||||||
}
|
}
|
||||||
@ -33,21 +33,61 @@ export default function QInput({ type, readOnly = false, options = [], value, on
|
|||||||
[type, value, onChange],
|
[type, value, onChange],
|
||||||
)
|
)
|
||||||
|
|
||||||
const handleTextChange = useCallback(
|
const handleTextNumberChange = useCallback(
|
||||||
(e) => {
|
(e) => {
|
||||||
|
if (type === 'text') {
|
||||||
onChange(e.target.value)
|
onChange(e.target.value)
|
||||||
|
} else if (type === 'number') {
|
||||||
|
onChange(Number(e.target.value))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[onChange],
|
[type, onChange],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// type=number 정수 부호, 소수점 검사, 일부 키 허용
|
||||||
|
const checkInputNumber = (e) => {
|
||||||
|
const value = e.target.value
|
||||||
|
const key = e.key
|
||||||
|
const allowKeys = ['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'Home', 'End', 'Tab', 'Enter', 'Control'] // 'ArrowUp', 'ArrowDown',
|
||||||
|
if (key >= '0' && key <= '9') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (key === '.' && !value.includes('.') && value.length > 0 && !isNaN(Number(value))) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (key === '-' && !value.includes('-') && value.length > 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (allowKeys.includes(key)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (key === 'a' || key === 'c' || key === 'v' || key === 'x' || key === 'z') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
e.preventDefault()
|
||||||
|
}
|
||||||
|
|
||||||
|
// input type : text, number
|
||||||
|
const inputTextNumber = () => {
|
||||||
return (
|
return (
|
||||||
<>
|
<input
|
||||||
{type === 'text' ? (
|
type={type}
|
||||||
<div className="mb5">
|
className={`input-light ${className ? className : ''}`}
|
||||||
<input type={type} className="input-light" readOnly={readOnly ? true : false} value={value} onChange={handleTextChange} />
|
ref={ref}
|
||||||
</div>
|
id={id}
|
||||||
) : type === 'radio' || type === 'checkbox' ? (
|
readOnly={readOnly ? true : false}
|
||||||
<div className="flx mb5">
|
placeholder={placeholder}
|
||||||
|
value={value === 0 ? 0 : value || ''}
|
||||||
|
onChange={handleTextNumberChange}
|
||||||
|
onKeyDown={type === 'number' ? checkInputNumber : undefined}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// input type : radio, checkbox
|
||||||
|
const inputRadioCheckbox = () => {
|
||||||
|
return (
|
||||||
|
<div className="flx">
|
||||||
{options?.map((option) => (
|
{options?.map((option) => (
|
||||||
<div key={option.name} className={`d-${type}-radio light mr5`}>
|
<div key={option.name} className={`d-${type}-radio light mr5`}>
|
||||||
<input
|
<input
|
||||||
@ -56,13 +96,14 @@ export default function QInput({ type, readOnly = false, options = [], value, on
|
|||||||
value={option.value}
|
value={option.value}
|
||||||
id={option.id}
|
id={option.id}
|
||||||
checked={type === 'radio' ? value === option.value : value.includes(option.value)}
|
checked={type === 'radio' ? value === option.value : value.includes(option.value)}
|
||||||
onChange={(e) => handleChange(e, option.value)}
|
onChange={(e) => handleRadioCheckboxChange(e, option.value)}
|
||||||
/>
|
/>
|
||||||
<label htmlFor={option.id}>{option.name}</label>
|
<label htmlFor={option.id}>{option.name}</label>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return <>{type === 'text' || type === 'number' ? inputTextNumber() : type === 'radio' || type === 'checkbox' ? inputRadioCheckbox() : null}</>
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user