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

Input disabled #29

Merged
merged 1 commit into from
Dec 22, 2023
Merged
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
24 changes: 18 additions & 6 deletions src/components/input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import React, {DetailedHTMLProps, InputHTMLAttributes, ReactElement, ReactNode,
import "./Input.style.scss"
import {TablerIconsProps} from "@tabler/icons-react";


export type InputChildType = InputControlType | InputDescType | InputLabelType

export interface InputType {
children: React.ReactNode | React.ReactNode[]
children: ReactElement<InputControlType> | ReactElement<InputChildType>[]
//defaults to false
disabled?: boolean
//defaults to undefined
Expand All @@ -13,12 +16,20 @@ export interface InputType {

const Input: React.FC<InputType> = (props: InputType) => {

const {disabled, children, valid} = props
const {disabled= false, children, valid} = props
const childNodes = React.Children.toArray(children).map(value => {
if (React.isValidElement(value) && value.type == InputControl)
return React.cloneElement(value as ReactElement, {
"aria-disabled": disabled,
"disabled": disabled
})
return value
})

return <div
className={`input ${disabled ? "input--disabled" : ""} ${valid ? "input--valid" : valid !== undefined ? "input--not-valid" : ""}`}
aria-disabled={disabled ? "true" : "false"}>
{children}
{childNodes}
</div>
}

Expand All @@ -40,7 +51,8 @@ export interface InputControlType extends DetailedHTMLProps<InputHTMLAttributes<
placeholder: string
//TODO: allow both as non required and make sure this does not result in errors inside the component
children?: ReactElement<InputControlMessageType | InputControlIconType>[] | ReactElement<InputControlMessageType | InputControlIconType>

//defaults to false
disabled?: boolean
//default is text type
type?: InputControlTypesType
}
Expand All @@ -57,7 +69,7 @@ export interface InputControlType extends DetailedHTMLProps<InputHTMLAttributes<
*/
const InputControl: React.ForwardRefExoticComponent<Omit<InputControlType, "ref"> & RefAttributes<HTMLInputElement>> = React.forwardRef<HTMLInputElement, InputControlType>((props, ref) => {

const {type, placeholder, children, ...args} = props
const {type, placeholder, children, disabled, ...args} = props
const childNodes = children && !Array.isArray(children) ? Array.of(children) : children;
const icon = childNodes ? childNodes.find(child => child.type == InputControlIcon) : null
const message = childNodes ? childNodes.find(child => child.type == InputControlMessage) : null
Expand All @@ -82,7 +94,7 @@ const InputControl: React.ForwardRefExoticComponent<Omit<InputControlType, "ref"
return <>
<div className={"input__control"}>
{icon ?? null}
<input ref={ref} type={type} placeholder={placeholder} className={"input__field"} {...args} onMouseDown={event => {
<input ref={ref} disabled={disabled} type={type} placeholder={placeholder} className={"input__field"} {...args} onMouseDown={event => {
(event.target as HTMLInputElement).focus()
}}/>
</div>
Expand Down