|
| 1 | +import classNames from 'classnames'; |
| 2 | +import { VNode } from 'dom-renderer'; |
| 3 | +import { FC, WebCellProps } from 'web-cell'; |
| 4 | +import { uniqueID } from 'web-utility'; |
| 5 | + |
| 6 | +export type FormGroupProps = WebCellProps<HTMLDivElement>; |
| 7 | + |
| 8 | +export const FormGroup: FC<FormGroupProps> = ({ children, ...props }) => ( |
| 9 | + <div {...props}>{children}</div> |
| 10 | +); |
| 11 | + |
| 12 | +export type FormLabelProps = WebCellProps<HTMLLabelElement>; |
| 13 | + |
| 14 | +export const FormLabel: FC<FormLabelProps> = ({ |
| 15 | + className = '', |
| 16 | + children, |
| 17 | + ...props |
| 18 | +}) => ( |
| 19 | + <label className={`form-label ${className}`} {...props}> |
| 20 | + {children} |
| 21 | + </label> |
| 22 | +); |
| 23 | + |
| 24 | +export interface FloatingLabelProps extends FormLabelProps { |
| 25 | + label: string; |
| 26 | +} |
| 27 | + |
| 28 | +export const FloatingLabel: FC<FloatingLabelProps> = ({ |
| 29 | + className = '', |
| 30 | + style, |
| 31 | + label, |
| 32 | + children, |
| 33 | + ...props |
| 34 | +}) => ( |
| 35 | + <div className={`form-floating ${className}`} style={style}> |
| 36 | + {children} |
| 37 | + <label {...props}>{label}</label> |
| 38 | + </div> |
| 39 | +); |
| 40 | + |
| 41 | +export type FormControlTag = 'input' | 'textarea' | 'select'; |
| 42 | + |
| 43 | +export type FormControlProps<T extends FormControlTag> = WebCellProps & |
| 44 | + Omit<JSX.IntrinsicElements[T], 'size'> & { |
| 45 | + as?: T; |
| 46 | + htmlSize?: number; |
| 47 | + size?: 'sm' | 'lg'; |
| 48 | + plaintext?: boolean; |
| 49 | + }; |
| 50 | + |
| 51 | +export const FormControl = <T extends FormControlTag = 'input'>({ |
| 52 | + as: Tag, |
| 53 | + className = '', |
| 54 | + htmlSize, |
| 55 | + size, |
| 56 | + plaintext, |
| 57 | + ...props |
| 58 | +}: FormControlProps<T>) => ( |
| 59 | + <Tag |
| 60 | + className={classNames( |
| 61 | + 'form-control', |
| 62 | + size && `form-control-${size}`, |
| 63 | + (props as FormControlProps<'input'>).readOnly && |
| 64 | + plaintext && |
| 65 | + `form-control-plaintext`, |
| 66 | + (props as FormControlProps<'input'>).type === 'color' && |
| 67 | + `form-control-color`, |
| 68 | + className |
| 69 | + )} |
| 70 | + {...props} |
| 71 | + size={htmlSize} |
| 72 | + /> |
| 73 | +); |
| 74 | + |
| 75 | +export interface FormCheckProps extends WebCellProps<HTMLInputElement> { |
| 76 | + type: 'radio' | 'checkbox' | 'switch'; |
| 77 | + inline?: boolean; |
| 78 | + reverse?: boolean; |
| 79 | + label?: VNode; |
| 80 | +} |
| 81 | + |
| 82 | +export const FormCheck: FC<FormCheckProps> = ({ |
| 83 | + id = uniqueID(), |
| 84 | + className = '', |
| 85 | + style, |
| 86 | + title, |
| 87 | + type, |
| 88 | + inline, |
| 89 | + reverse, |
| 90 | + label, |
| 91 | + ...props |
| 92 | +}) => ( |
| 93 | + <div |
| 94 | + className={classNames( |
| 95 | + label && 'form-check', |
| 96 | + inline && 'form-check-inline', |
| 97 | + reverse && 'form-check-reverse', |
| 98 | + type === 'switch' && 'form-switch', |
| 99 | + className |
| 100 | + )} |
| 101 | + style={style} |
| 102 | + > |
| 103 | + <input |
| 104 | + className="form-check-input" |
| 105 | + type={type === 'switch' ? 'checkbox' : type} |
| 106 | + role={type === 'switch' ? 'switch' : undefined} |
| 107 | + id={id} |
| 108 | + {...props} |
| 109 | + /> |
| 110 | + {label && ( |
| 111 | + <label className="form-check-label" htmlFor={id} title={title}> |
| 112 | + {label} |
| 113 | + </label> |
| 114 | + )} |
| 115 | + </div> |
| 116 | +); |
| 117 | + |
| 118 | +export type FormFieldProps<T extends FormControlTag> = FormGroupProps & |
| 119 | + FormLabelProps & |
| 120 | + FormControlProps<T> & { |
| 121 | + label?: string; |
| 122 | + labelFloat?: boolean; |
| 123 | + }; |
| 124 | + |
| 125 | +export const FormField = <T extends FormControlTag = 'input'>({ |
| 126 | + className, |
| 127 | + label, |
| 128 | + labelFloat, |
| 129 | + ...props |
| 130 | +}: FormFieldProps<T>) => { |
| 131 | + label ||= props.title || (props as FormControlProps<'input'>).placeholder; |
| 132 | + |
| 133 | + const field = <FormControl {...(props as FormControlProps<T>)} />; |
| 134 | + |
| 135 | + return labelFloat ? ( |
| 136 | + <FloatingLabel {...{ className, label }}>{field}</FloatingLabel> |
| 137 | + ) : ( |
| 138 | + <FormGroup className={className}> |
| 139 | + <FormLabel>{label}</FormLabel> |
| 140 | + {field} |
| 141 | + </FormGroup> |
| 142 | + ); |
| 143 | +}; |
0 commit comments