Radio.svelte 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <script>
  2. import { getContext } from 'svelte';
  3. import Icon from '../icon/Icon.svelte';
  4. // 名称
  5. // name
  6. export let name = '';
  7. // 文本位置
  8. // text position
  9. export let textPosition = 'r';
  10. // 图标
  11. // icon
  12. export let icon = 'default';
  13. // 选中图标
  14. // icon checked
  15. export let iconChecked = 'default';
  16. const STDF_radioValueStore = getContext('STDF_radioValueContext');
  17. const STDF_radioHorizontalStore = getContext('STDF_radioHorizontalContext');
  18. const clickRadioFun = () => {
  19. STDF_radioValueStore.set(name);
  20. };
  21. </script>
  22. <!-- svelte-ignore a11y-click-events-have-key-events -->
  23. <div
  24. class={`flex grow active:opacity-80 ${textPosition === 'l' && !$STDF_radioHorizontalStore ? 'justify-between' : ''} ${
  25. textPosition === 'b' || textPosition === 't'
  26. ? 'flex-col items-center'
  27. : $STDF_radioHorizontalStore
  28. ? 'justify-center'
  29. : 'items-center'
  30. }`}
  31. on:click={clickRadioFun}
  32. >
  33. {#if textPosition === 'l' || textPosition === 't'}
  34. <div class={`${textPosition === 'l' && 'mr-0.5'} ${!$STDF_radioHorizontalStore ? 'grow' : ''}`}>
  35. <slot />
  36. </div>
  37. {/if}
  38. <div class:hidden={$STDF_radioValueStore !== name}>
  39. {#if iconChecked === 'none'}
  40. <!-- none -->
  41. {:else if iconChecked === 'default'}
  42. <Icon name="ri-radio-button-line" theme top={textPosition === 't' || textPosition === 'b' ? 0 : -1} />
  43. {:else}
  44. <Icon {...iconChecked} theme />
  45. {/if}
  46. </div>
  47. <div class:hidden={$STDF_radioValueStore === name}>
  48. {#if icon === 'none'}
  49. <!-- none -->
  50. {:else if icon === 'default'}
  51. <Icon name="ri-checkbox-blank-circle-line" alpha={0.1} top={textPosition === 't' || textPosition === 'b' ? 0 : -1} />
  52. {:else}
  53. <Icon {...icon} alpha={0.1} />
  54. {/if}
  55. </div>
  56. {#if textPosition === 'r' || textPosition === 'b'}
  57. <div class={`${textPosition === 'r' && 'ml-0.5'} ${!$STDF_radioHorizontalStore ? 'grow' : ''}`}>
  58. <slot />
  59. </div>
  60. {/if}
  61. </div>