CheckboxItem.svelte 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <script>
  2. import Icon from '../icon/Icon.svelte';
  3. /** @typedef {import('../..').CheckboxItem} CheckboxItemProps */
  4. /** @type {CheckboxItemProps} */
  5. let {
  6. name = '',
  7. layout = 'v',
  8. checked = false,
  9. textPosition = 'r',
  10. icon = 'default',
  11. iconChecked = 'default',
  12. children,
  13. onclick,
  14. } = $props();
  15. // 点击选项事件
  16. // Click option event
  17. const clickRadioFun = () => {
  18. onclick && onclick(name);
  19. };
  20. </script>
  21. <button
  22. onclick={() => clickRadioFun()}
  23. class="{layout === 'inline' ? 'inline-block' : 'flex'} grow active:opacity-80 {textPosition === 'l' && layout === 'v'
  24. ? 'justify-between'
  25. : ''} {textPosition === 'b' || textPosition === 't' ? 'flex-col items-center' : layout === 'h' ? 'justify-center' : 'items-center'}"
  26. >
  27. {#if textPosition === 'l' || textPosition === 't'}
  28. <div class="{textPosition === 'l' && icon !== null ? 'mr-0.5 text-left' : ''} {layout === 'v' ? 'grow' : ''}">
  29. {@render children?.()}
  30. {#if layout === 'v'}
  31. <div class="h-px mt-1 bg-black/10 dark:bg-white/10"></div>
  32. {/if}
  33. </div>
  34. {/if}
  35. <div class:hidden={!checked}>
  36. {#if iconChecked === null}{:else if iconChecked === 'default'}
  37. <Icon name="ri-checkbox-fill" theme top={textPosition === 't' || textPosition === 'b' ? 0 : -1} />
  38. {:else}
  39. <Icon {...iconChecked} theme />
  40. {/if}
  41. </div>
  42. <div class:hidden={checked}>
  43. {#if icon === null}{:else if icon === 'default'}
  44. <Icon name="ri-checkbox-line" alpha={0.2} top={textPosition === 't' || textPosition === 'b' ? 0 : -1} />
  45. {:else}
  46. <Icon alpha={0.2} {...icon} />
  47. {/if}
  48. </div>
  49. {#if textPosition === 'r' || textPosition === 'b'}
  50. <div class={textPosition === 'r' && icon !== null ? 'ml-0.5 text-left' : ''}>
  51. {@render children?.()}
  52. </div>
  53. {/if}
  54. </button>