|
| 1 | +{{! partial of |
| 2 | + https://raw.githubusercontent.com/josemarluedke/frontile/a9680f2681a8b1193feba0fdc27871566bcde120/packages/forms/src/components/input.gts |
| 3 | +}} |
| 4 | +import Component from '@glimmer/component'; |
| 5 | +import { tracked } from '@glimmer/tracking'; |
| 6 | +import { action } from '@ember/object'; |
| 7 | +import { on } from '@ember/modifier'; |
| 8 | +import { |
| 9 | + useStyles, |
| 10 | + type InputSlots, |
| 11 | + type InputVariants, |
| 12 | + type SlotsToClasses |
| 13 | +} from '@frontile/theme'; |
| 14 | +import { FormControl, type FormControlSharedArgs } from './form-control'; |
| 15 | +import { triggerFormInputEvent } from '../utils'; |
| 16 | +import { ref } from '@frontile/utilities'; |
| 17 | +import { CloseButton } from '@frontile/buttons'; |
| 18 | + |
| 19 | +interface Args extends FormControlSharedArgs { |
| 20 | + type?: string; |
| 21 | + value?: string; |
| 22 | + name?: string; |
| 23 | + size?: InputVariants['size']; |
| 24 | + classes?: SlotsToClasses<InputSlots>; |
| 25 | + |
| 26 | + /** |
| 27 | + * Whether to include a clear button |
| 28 | + */ |
| 29 | + isClearable?: boolean; |
| 30 | + |
| 31 | + /** |
| 32 | + * Controls pointer-events property of startContent. |
| 33 | + * If you want to pass the click event to the input, set it to `none`. |
| 34 | + * |
| 35 | + * @defaultValue 'auto' |
| 36 | + */ |
| 37 | + startContentPointerEvents?: 'none' | 'auto'; |
| 38 | + |
| 39 | + /** |
| 40 | + * Controls pointer-events property of endContent. |
| 41 | + * If you want to pass the click event to the input, set it to `none`. |
| 42 | + * |
| 43 | + * @defaultValue 'auto' |
| 44 | + */ |
| 45 | + endContentPointerEvents?: 'none' | 'auto'; |
| 46 | + |
| 47 | + /** |
| 48 | + * Callback when oninput is triggered |
| 49 | + */ |
| 50 | + onInput?: (value: string, event?: InputEvent) => void; |
| 51 | + |
| 52 | + /** |
| 53 | + * Callback when onchange is triggered |
| 54 | + */ |
| 55 | + onChange?: (value: string, event?: InputEvent) => void; |
| 56 | +} |
| 57 | + |
| 58 | +interface InputSignature { |
| 59 | + Args: Args; |
| 60 | + Blocks: { |
| 61 | + startContent: []; |
| 62 | + endContent: []; |
| 63 | + }; |
| 64 | + Element: HTMLInputElement; |
| 65 | +} |
| 66 | + |
| 67 | +function or(arg1: unknown, arg2: unknown): boolean { |
| 68 | + return !!(arg1 || arg2); |
| 69 | +} |
| 70 | + |
| 71 | +class Input extends Component<InputSignature> { |
| 72 | + @tracked uncontrolledValue: string = ''; |
| 73 | + |
| 74 | + inputRef = ref<HTMLInputElement>(); |
| 75 | + |
| 76 | + if (this.isControlled) { |
| 77 | + this.args.onInput?.(value, event as InputEvent); |
| 78 | + } else { |
| 79 | + this.uncontrolledValue = value; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @action handleOnChange(event: Event): void { |
| 84 | + const value = (event.target as HTMLInputElement).value; |
| 85 | + |
| 86 | + if (this.isControlled) { |
| 87 | + this.args.onChange?.(value, event as InputEvent); |
| 88 | + } else { |
| 89 | + this.uncontrolledValue = value; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + this.inputRef.element?.focus(); |
| 94 | + triggerFormInputEvent(this.inputRef.element); |
| 95 | + } |
| 96 | + |
| 97 | + <template> |
| 98 | + <FormControl |
| 99 | + @size={{@size}} |
| 100 | + @label={{@label}} |
| 101 | + @isRequired={{@isRequired}} |
| 102 | + @description={{@description}} |
| 103 | + @errors={{@errors}} |
| 104 | + @isInvalid={{@isInvalid}} |
| 105 | + @class={{this.classes.base class=@classes.base}} |
| 106 | + as |c| |
| 107 | + > |
| 108 | + <div class={{this.classes.innerContainer class=@classes.innerContainer}}> |
| 109 | + {{#if (has-block "startContent")}} |
| 110 | + <div |
| 111 | + data-test-id="input-start-content" |
| 112 | + class={{this.classes.startContent |
| 113 | + class=@classes.startContent |
| 114 | + startContentPointerEvents=(if |
| 115 | + @startContentPointerEvents @startContentPointerEvents "auto" |
| 116 | + ) |
| 117 | + }} |
| 118 | + > |
| 119 | + {{yield to="startContent"}} |
| 120 | + </div> |
| 121 | + {{/if}} |
| 122 | + <input |
| 123 | + {{this.inputRef.setup}} |
| 124 | + {{on "input" this.handleOnInput}} |
| 125 | + {{on "change" this.handleOnChange}} |
| 126 | + id={{c.id}} |
| 127 | + }} |
| 128 | + data-component="input" |
| 129 | + aria-invalid={{if c.isInvalid "true"}} |
| 130 | + aria-describedby={{c.describedBy @description c.isInvalid}} |
| 131 | + ...attributes |
| 132 | + /> |
| 133 | + {{#if (or (has-block "endContent") this.isClearable)}} |
| 134 | + <div |
| 135 | + > |
| 136 | + {{yield to="endContent"}} |
| 137 | + |
| 138 | + {{#if this.isClearable}} |
| 139 | + <CloseButton |
| 140 | + /> |
| 141 | + {{/if}} |
| 142 | + </div> |
| 143 | + {{/if}} |
| 144 | + </div> |
| 145 | + </FormControl> |
| 146 | + </template> |
| 147 | +} |
| 148 | + |
| 149 | +export { Input, type InputSignature }; |
| 150 | +export default Input; |
0 commit comments