degmods.com/src/components/Inputs.tsx

122 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-07-12 09:22:31 +00:00
import '../styles/styles.css'
2024-07-21 16:30:03 +00:00
import ReactQuill from 'react-quill'
import '../styles/customQuillStyles.css'
import React from 'react'
const editorFormats = [
'header',
'font',
'size',
'bold',
'italic',
'underline',
'strike',
'blockquote',
'list',
'bullet',
'indent',
'link'
]
const editorModules = {
toolbar: [
[{ header: '1' }, { header: '2' }, { font: [] }],
[{ size: [] }],
['bold', 'italic', 'underline', 'strike', 'blockquote'],
[
{ list: 'ordered' },
{ list: 'bullet' },
{ indent: '-1' },
{ indent: '+1' }
],
['link']
]
}
2024-07-12 09:22:31 +00:00
interface InputFieldProps {
label: string
description?: string
2024-07-21 16:30:03 +00:00
type?: 'text' | 'textarea' | 'richtext'
2024-07-12 09:22:31 +00:00
placeholder: string
name: string
inputMode?: 'url'
2024-07-21 16:30:03 +00:00
value: string
onChange: (name: string, value: string) => void
2024-07-12 09:22:31 +00:00
}
2024-07-21 16:30:03 +00:00
export const InputField = React.memo(
({
label,
description,
type = 'text',
placeholder,
name,
inputMode,
value,
onChange
}: InputFieldProps) => {
const handleChange = (
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
) => {
onChange(name, e.target.value)
}
return (
<div className='inputLabelWrapperMain'>
<label className='form-label labelMain'>{label}</label>
{description && <p className='labelDescriptionMain'>{description}</p>}
{type === 'textarea' ? (
<textarea
className='inputMain'
placeholder={placeholder}
name={name}
value={value}
onChange={handleChange}
></textarea>
) : type === 'richtext' ? (
<ReactQuill
className='inputMain'
formats={editorFormats}
modules={editorModules}
placeholder={placeholder}
value={value}
onChange={(content) => onChange(name, content)}
/>
) : (
<input
type={type}
className='inputMain'
placeholder={placeholder}
name={name}
inputMode={inputMode}
value={value}
onChange={handleChange}
/>
)}
</div>
)
}
2024-07-12 09:22:31 +00:00
)
interface CheckboxFieldProps {
label: string
name: string
2024-07-21 16:30:03 +00:00
isChecked: boolean
handleChange: (e: React.ChangeEvent<HTMLInputElement>) => void
2024-07-12 09:22:31 +00:00
}
2024-07-21 16:30:03 +00:00
export const CheckboxField = React.memo(
({ label, name, isChecked, handleChange }: CheckboxFieldProps) => (
<div className='inputLabelWrapperMain inputLabelWrapperMainAlt inputLabelWrapperMainAltStylized'>
<label className='form-label labelMain'>{label}</label>
2024-07-12 09:22:31 +00:00
<input
2024-07-21 16:30:03 +00:00
type='checkbox'
className='CheckboxMain'
name={name}
checked={isChecked}
onChange={handleChange}
2024-07-12 09:22:31 +00:00
/>
</div>
2024-07-21 16:30:03 +00:00
)
2024-07-12 09:22:31 +00:00
)