|
| 1 | +import { Slot, component$, useSignal } from '@builder.io/qwik'; |
| 2 | +import { SfSelectSize } from '../../shared/SfSelect'; |
| 3 | +import { SfIconExpandMore } from '../SfIcons'; |
| 4 | +import { SfSelectProps } from './types'; |
| 5 | + |
| 6 | +export const SfSelect = component$<SfSelectProps>( |
| 7 | + ({ |
| 8 | + size = SfSelectSize.base, |
| 9 | + children, |
| 10 | + invalid, |
| 11 | + wrapperClass, |
| 12 | + showSlotChevron, |
| 13 | + required, |
| 14 | + disabled, |
| 15 | + class: _class, |
| 16 | + placeholder, |
| 17 | + // onBlur, |
| 18 | + onChange$, |
| 19 | + // onClick, |
| 20 | + // onKeyDown, |
| 21 | + ...attributes |
| 22 | + }) => { |
| 23 | + // TODO |
| 24 | + // const { isFocusVisible } = useFocusVisible(); |
| 25 | + const isFocusVisible = false; |
| 26 | + |
| 27 | + const chevronRotatedSignal = useSignal(false); |
| 28 | + |
| 29 | + // const rotateUp = $(() => {chevronRotatedSignal.value = (true)}); |
| 30 | + |
| 31 | + // const rotateDown = $(() => {chevronRotatedSignal.value =(false)}); |
| 32 | + |
| 33 | + // const keydownHandler = $((event: QwikKeyboardEvent<HTMLSelectElement>) => { |
| 34 | + // if (event.key === 'Space') { |
| 35 | + // rotateUp(); |
| 36 | + // } |
| 37 | + // }); |
| 38 | + |
| 39 | + return ( |
| 40 | + <div |
| 41 | + class={`relative flex flex-col rounded-md ${ |
| 42 | + isFocusVisible |
| 43 | + ? 'focus-within:outline focus-within:outline-offset' |
| 44 | + : '' |
| 45 | + } ${wrapperClass}`} |
| 46 | + data-testid="select" |
| 47 | + > |
| 48 | + <select |
| 49 | + required={required} |
| 50 | + disabled={disabled} |
| 51 | + class={` |
| 52 | + appearance-none disabled:cursor-not-allowed cursor-pointer pl-4 pr-3.5 text-neutral-900 focus:ring-primary-700 focus:ring-2 outline-none bg-transparent rounded-md ring-1 ring-inset ring-neutral-300 hover:ring-primary-700 active:ring-2 active:ring-primary-700 disabled:bg-disabled-100 disabled:text-disabled-900 disabled:ring-disabled-200 |
| 53 | + ${size === SfSelectSize.sm ? 'py-1.5' : ''} |
| 54 | + ${size === SfSelectSize.base ? 'py-2' : ''} |
| 55 | + ${size === SfSelectSize.lg ? 'py-3 text-base' : ''} |
| 56 | + ${invalid && !disabled ? '!ring-negative-600 ring-2' : ''} |
| 57 | + ${_class}`} |
| 58 | + data-testid="select-input" |
| 59 | + onChange$={onChange$} |
| 60 | + // TODO |
| 61 | + // onBlur={composeHandlers(rotateDown, onBlur)} |
| 62 | + // onChange={composeHandlers(rotateDown, onChange)} |
| 63 | + // onClick={composeHandlers(rotateUp, onClick)} |
| 64 | + // onKeyDown={composeHandlers(keydownHandler, onKeyDown)} |
| 65 | + {...attributes} |
| 66 | + > |
| 67 | + {placeholder && ( |
| 68 | + <option |
| 69 | + value="" |
| 70 | + class={`bg-neutral-300 text-sm ${ |
| 71 | + size === SfSelectSize.lg ? 'text-base' : '' |
| 72 | + }`} |
| 73 | + data-testid="select-placeholder" |
| 74 | + > |
| 75 | + {placeholder} |
| 76 | + </option> |
| 77 | + )} |
| 78 | + <Slot /> |
| 79 | + </select> |
| 80 | + {showSlotChevron ? ( |
| 81 | + <Slot name="chevron" /> |
| 82 | + ) : ( |
| 83 | + <SfIconExpandMore |
| 84 | + class={` |
| 85 | + box-border absolute -translate-y-1 pointer-events-none top-1/3 right-4 transition easy-in-out duration-0.5 |
| 86 | + ${disabled ? 'text-disabled-500' : 'text-neutral-500'} |
| 87 | + ${chevronRotatedSignal.value ? 'rotate-180' : ''}`} |
| 88 | + /> |
| 89 | + )} |
| 90 | + </div> |
| 91 | + ); |
| 92 | + } |
| 93 | +); |
0 commit comments