|
| 1 | +import { onMount, Show, useMetadata, useRef, useStore } from "@builder.io/mitosis"; |
| 2 | +import { DBInputState, DBInputProps, iconVariants } from "./model"; |
| 3 | +import { DBIcon } from '../icon'; |
| 4 | +import "./input.scss"; |
| 5 | + |
| 6 | +useMetadata({ |
| 7 | + isAttachedToShadowDom: false, |
| 8 | + component: { |
| 9 | + includeIcon: false, |
| 10 | + properties: [], |
| 11 | + }, |
| 12 | +}); |
| 13 | + |
| 14 | +export default function DBInput(props: DBInputProps) { |
| 15 | + const textInputRef = useRef(null); |
| 16 | + const state = useStore<DBInputState>({ |
| 17 | + _isValid: undefined, |
| 18 | + handleChange: (event) => { |
| 19 | + if (props.onChange) { |
| 20 | + props.onChange(event); |
| 21 | + } |
| 22 | + if (props.change) { |
| 23 | + props.change(event); |
| 24 | + } |
| 25 | + |
| 26 | + if (textInputRef?.validity?.valid != state._isValid ) { |
| 27 | + state._isValid = textInputRef?.validity?.valid; |
| 28 | + if( props.validityChange ) { |
| 29 | + props.validityChange(textInputRef?.validity?.valid); |
| 30 | + } |
| 31 | + } |
| 32 | + }, |
| 33 | + handleBlur: (event) => { |
| 34 | + if (props.onBlur) { |
| 35 | + props.onBlur(event); |
| 36 | + } |
| 37 | + if (props.blur) { |
| 38 | + props.blur(event); |
| 39 | + } |
| 40 | + }, |
| 41 | + handleFocus: (event) => { |
| 42 | + if (props.onFocus) { |
| 43 | + props.onFocus(event); |
| 44 | + } |
| 45 | + if (props.focus) { |
| 46 | + props.focus(event); |
| 47 | + } |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + onMount(() => { |
| 52 | + if (props.stylePath) { |
| 53 | + state.stylePath = props.stylePath; |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + return ( |
| 58 | + <div class={ 'db-input ' + (props.className || '') } data-variant={props.variant}> |
| 59 | + <Show when={state.stylePath}> |
| 60 | + <link rel="stylesheet" href={state.stylePath} /> |
| 61 | + </Show> |
| 62 | + <Show when={props.iconBefore}> |
| 63 | + <DBIcon icon={props.iconBefore} className="icon-before" /> |
| 64 | + </Show> |
| 65 | + <input |
| 66 | + ref={textInputRef} |
| 67 | + id={props.id} |
| 68 | + name={props.name} |
| 69 | + type={props.type || 'text'} |
| 70 | + placeholder={props.placeholder} |
| 71 | + aria-labelledby={props.id + '-label'} |
| 72 | + disabled={props.disabled} |
| 73 | + required={props.required} |
| 74 | + value={props.value} |
| 75 | + maxLength={props.maxLength} |
| 76 | + minLength={props.minLength} |
| 77 | + pattern={props.pattern} |
| 78 | + onChange={(event) => state.handleChange(event)} |
| 79 | + onBlur={(event) => state.handleBlur(event)} |
| 80 | + onFocus={(event) => state.handleFocus(event)} |
| 81 | + /> |
| 82 | + <label for={props.id} aria-hidden="true" id={props.id + '-label'}> |
| 83 | + <span>{props.label}</span> |
| 84 | + </label> |
| 85 | + <Show when={props.description}> |
| 86 | + <p className="description">{props.description}</p> |
| 87 | + </Show> |
| 88 | + <Show when={props.variant || props.required || props.pattern}> |
| 89 | + <DBIcon |
| 90 | + icon={props.variant && iconVariants[props.variant]} |
| 91 | + className="icon-input-state" |
| 92 | + /> |
| 93 | + </Show> |
| 94 | + <Show when={props.iconAfter}> |
| 95 | + <DBIcon icon={props.iconAfter} className="icon-after" /> |
| 96 | + </Show> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +} |
0 commit comments