|
7 | 7 |
|
8 | 8 | <script lang="ts"> |
9 | 9 | import { twMerge } from 'tailwind-merge'; |
10 | | - import { getContext, type Snippet } from 'svelte'; |
11 | | - import type { HTMLInputAttributes } from 'svelte/elements'; |
12 | | -
|
13 | | - interface Props extends HTMLInputAttributes { |
14 | | - children?: Snippet; |
15 | | - left?: Snippet; |
16 | | - right?: Snippet; |
17 | | - inputSize?: FormSizeType; |
18 | | - value?: any; |
19 | | - defaultClass?: string | undefined | null; |
20 | | - color?: 'base' | 'green' | 'red'; |
21 | | - floatClass?: string | undefined | null; |
22 | | - classLeft?: string | undefined | null; |
23 | | - classRight?: string | undefined | null; |
24 | | - class?: string | undefined | null; |
25 | | - show?: boolean; |
26 | | - } |
| 10 | + import { getContext } from 'svelte'; |
| 11 | + import { type InputProps as Props, input} from '.' |
27 | 12 |
|
28 | 13 | let { |
29 | 14 | children, |
30 | 15 | left, |
31 | 16 | right, |
32 | 17 | value = $bindable(), |
33 | | - inputSize, |
| 18 | + size, |
34 | 19 | defaultClass = 'block w-full disabled:cursor-not-allowed disabled:opacity-50 rtl:text-right', |
35 | 20 | color = 'base', |
36 | 21 | floatClass = 'flex absolute inset-y-0 items-center text-gray-500 dark:text-gray-400', |
|
41 | 26 | ...attributes |
42 | 27 | }: Props = $props(); |
43 | 28 |
|
44 | | - const borderClasses = { |
45 | | - base: 'border border-gray-300 dark:border-gray-600', |
46 | | - tinted: 'border border-gray-300 dark:border-gray-500', |
47 | | - green: 'border border-green-500 dark:border-green-400', |
48 | | - red: 'border border-red-500 dark:border-red-400' |
49 | | - }; |
| 29 | + // const borderClasses = { |
| 30 | + // base: 'border border-gray-300 dark:border-gray-600', |
| 31 | + // tinted: 'border border-gray-300 dark:border-gray-500', |
| 32 | + // green: 'border border-green-500 dark:border-green-400', |
| 33 | + // red: 'border border-red-500 dark:border-red-400' |
| 34 | + // }; |
50 | 35 |
|
51 | | - const ringClasses = { |
52 | | - base: 'focus:border-primary-500 focus:ring-primary-500 dark:focus:border-primary-500 dark:focus:ring-primary-500', |
53 | | - green: |
54 | | - 'focus:ring-green-500 focus:border-green-500 dark:focus:border-green-500 dark:focus:ring-green-500', |
55 | | - red: 'focus:ring-red-500 focus:border-red-500 dark:focus:ring-red-500 dark:focus:border-red-500' |
56 | | - }; |
| 36 | + // const ringClasses = { |
| 37 | + // base: 'focus:border-primary-500 focus:ring-primary-500 dark:focus:border-primary-500 dark:focus:ring-primary-500', |
| 38 | + // green: |
| 39 | + // 'focus:ring-green-500 focus:border-green-500 dark:focus:border-green-500 dark:focus:ring-green-500', |
| 40 | + // red: 'focus:ring-red-500 focus:border-red-500 dark:focus:ring-red-500 dark:focus:border-red-500' |
| 41 | + // }; |
57 | 42 |
|
58 | | - const colorClasses = { |
59 | | - base: 'bg-gray-50 text-gray-900 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400', |
60 | | - tinted: |
61 | | - 'bg-gray-50 text-gray-900 dark:bg-gray-600 dark:text-white dark:placeholder-gray-400', |
62 | | - green: |
63 | | - 'bg-green-50 text-green-900 placeholder-green-700 dark:text-green-400 dark:placeholder-green-500 dark:bg-gray-700', |
64 | | - red: 'bg-red-50 text-red-900 placeholder-red-700 dark:text-red-500 dark:placeholder-red-500 dark:bg-gray-700' |
65 | | - }; |
| 43 | + // const colorClasses = { |
| 44 | + // base: 'bg-gray-50 text-gray-900 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400', |
| 45 | + // tinted: |
| 46 | + // 'bg-gray-50 text-gray-900 dark:bg-gray-600 dark:text-white dark:placeholder-gray-400', |
| 47 | + // green: |
| 48 | + // 'bg-green-50 text-green-900 placeholder-green-700 dark:text-green-400 dark:placeholder-green-500 dark:bg-gray-700', |
| 49 | + // red: 'bg-red-50 text-red-900 placeholder-red-700 dark:text-red-500 dark:placeholder-red-500 dark:bg-gray-700' |
| 50 | + // }; |
66 | 51 |
|
67 | 52 | // tinted if put in component having its own background |
68 | 53 | let background: boolean = getContext('background'); |
69 | 54 |
|
70 | 55 | let group: { size: SizeType } = getContext('group'); |
71 | | - // console.log('group', group); |
| 56 | + let isGroup = !!group; |
| 57 | + // console.log('isGroup', isGroup); |
72 | 58 |
|
73 | | - const textSizes = { sm: 'sm:text-xs', md: 'text-sm', lg: 'sm:text-base' }; |
74 | | - const leftPadding = { sm: 'ps-9', md: 'ps-10', lg: 'ps-11' }; |
75 | | - const rightPadding = { sm: 'pe-9', md: 'pe-10', lg: 'pe-11' }; |
76 | | - const inputPadding = { sm: 'p-2', md: 'p-2.5', lg: 'p-3' }; |
| 59 | + // const textSizes = { sm: 'sm:text-xs', md: 'text-sm', lg: 'sm:text-base' }; |
| 60 | + // const leftPadding = { sm: 'ps-9', md: 'ps-10', lg: 'ps-11' }; |
| 61 | + // const rightPadding = { sm: 'pe-9', md: 'pe-10', lg: 'pe-11' }; |
| 62 | + // const inputPadding = { sm: 'p-2', md: 'p-2.5', lg: 'p-3' }; |
77 | 63 |
|
78 | | - let _size = inputSize || clampSize(group?.size) || 'md'; |
| 64 | + let _size = $derived(size || clampSize(group?.size) || 'md'); |
79 | 65 | const _color = $derived(color === 'base' && background ? 'tinted' : color); |
80 | | - let inputClass = $derived( |
81 | | - twMerge( |
82 | | - defaultClass, |
83 | | - inputPadding[_size], |
84 | | - (left && leftPadding[_size]) || (right && rightPadding[_size]), |
85 | | - ringClasses[color], |
86 | | - colorClasses[_color], |
87 | | - borderClasses[_color], |
88 | | - textSizes[_size], |
89 | | - group ? '' : 'rounded-lg', |
90 | | - group && 'first:rounded-s-lg last:rounded-e-lg', |
91 | | - group && '[&:not(:first-child)]:-ms-px', |
92 | | - className |
93 | | - ) |
94 | | - ); |
| 66 | +
|
| 67 | + const { input:inputCls, left: leftCls, right: rightCls } = $derived(input({ size:_size, color:_color, group:isGroup, class: className })); |
| 68 | + // let inputClass = $derived( |
| 69 | + // twMerge( |
| 70 | + // defaultClass, |
| 71 | + // inputPadding[_size], |
| 72 | + // (left && leftPadding[_size]) || (right && rightPadding[_size]), |
| 73 | + // ringClasses[color], |
| 74 | + // colorClasses[_color], |
| 75 | + // borderClasses[_color], |
| 76 | + // textSizes[_size], |
| 77 | + // group ? '' : 'rounded-lg', |
| 78 | + // group && 'first:rounded-s-lg last:rounded-e-lg', |
| 79 | + // group && '[&:not(:first-child)]:-ms-px', |
| 80 | + // className |
| 81 | + // ) |
| 82 | + // ); |
95 | 83 | </script> |
96 | 84 |
|
97 | | -<div class="relative w-full"> |
| 85 | +{#snippet inputContent()} |
98 | 86 | {#if left} |
99 | 87 | <div |
100 | | - class="{twMerge( |
101 | | - floatClass, |
102 | | - classLeft |
103 | | - )} pointer-events-none start-0 ps-2.5" |
| 88 | + class={leftCls({ class: classLeft})} |
104 | 89 | > |
105 | 90 | {@render left()} |
106 | 91 | </div> |
107 | 92 | {/if} |
108 | | - <input {...attributes} bind:value class={inputClass} /> |
| 93 | + <input {...attributes} bind:value class={inputCls({ class: className })} /> |
109 | 94 | {#if children} |
110 | 95 | {@render children()} |
111 | 96 | {/if} |
112 | 97 | {#if right} |
113 | | - <div class="{twMerge(floatClass, classRight)} end-0 pe-2.5"> |
| 98 | + <div class={rightCls({ class: classRight })}> |
114 | 99 | {@render right()} |
115 | 100 | </div> |
116 | 101 | {/if} |
117 | | -</div> |
| 102 | +{/snippet} |
| 103 | + |
| 104 | +{#if group} |
| 105 | + {@render inputContent()} |
| 106 | +{:else} |
| 107 | + <div class="relative w-full"> |
| 108 | + {@render inputContent()} |
| 109 | + </div> |
| 110 | +{/if} |
118 | 111 |
|
119 | 112 | <!-- |
120 | 113 | @component |
|
124 | 117 | @prop left |
125 | 118 | @prop right |
126 | 119 | @prop value = $bindable() |
127 | | -@prop inputSize |
| 120 | +@prop size |
128 | 121 | @prop defaultClass = 'block w-full disabled:cursor-not-allowed disabled:opacity-50 rtl:text-right' |
129 | 122 | @prop color = 'base' |
130 | 123 | @prop floatClass = 'flex absolute inset-y-0 items-center text-gray-500 dark:text-gray-400' |
|
0 commit comments