@c2-markdown-ui/svelte 0.0.0-stage → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,60 @@
1
+ <script lang="ts">
2
+ interface Props {
3
+ label?: string;
4
+ /** 默认是否选中 */
5
+ default?: boolean;
6
+ /** 选中时展示的文案(antd checkedChildren) */
7
+ checkedText?: string;
8
+ /** 未选中时展示的文案(antd unCheckedChildren) */
9
+ unCheckedText?: string;
10
+ hint?: string;
11
+ /** 是否必填(switch 始终有值,仅语义标记) */
12
+ required?: boolean;
13
+ /** 是否禁用 */
14
+ disabled?: boolean;
15
+ onchange: (value: boolean) => void;
16
+ }
17
+ let {
18
+ label = '',
19
+ default: initial = false,
20
+ checkedText = '',
21
+ unCheckedText = '',
22
+ hint = '',
23
+ required = false,
24
+ disabled = false,
25
+ onchange
26
+ }: Props = $props();
27
+ let checked = $state(initial);
28
+
29
+ function toggle() {
30
+ if (disabled) return;
31
+ checked = !checked;
32
+ onchange(checked);
33
+ }
34
+ </script>
35
+
36
+ <div class="widget-switch">
37
+ {#if label || hint || required}
38
+ <div class="widget-label-row switch-label-row">
39
+ {#if required}<span class="widget-required-mark" aria-hidden="true">*</span>{/if}
40
+ {#if label}<label>{label}</label>{/if}
41
+ {#if hint}<span class="widget-hint" data-hint={hint}>ⓘ</span>{/if}
42
+ </div>
43
+ {/if}
44
+ <div class="widget-field-content">
45
+ <button
46
+ type="button"
47
+ role="switch"
48
+ aria-checked={checked}
49
+ aria-label={label}
50
+ class="switch-track"
51
+ data-checked={checked}
52
+ onclick={toggle}
53
+ {disabled}
54
+ >
55
+ <span class="switch-thumb"></span>
56
+ {#if checkedText}<span class="switch-text switch-text-checked">{checkedText}</span>{/if}
57
+ {#if unCheckedText}<span class="switch-text switch-text-unchecked">{unCheckedText}</span>{/if}
58
+ </button>
59
+ </div>
60
+ </div>
@@ -0,0 +1,58 @@
1
+ <script lang="ts">
2
+ interface Props {
3
+ /** 文字内容 */
4
+ text?: string;
5
+ /** 字号(12-48),默认 14 */
6
+ size?: number;
7
+ /** 字体颜色(十六进制或 CSS 颜色名) */
8
+ color?: string;
9
+ /** 加粗 */
10
+ bold?: boolean;
11
+ /** 文字对齐:left / center / right */
12
+ align?: 'left' | 'center' | 'right';
13
+ /** 是否启用 Alert 提示风格(参考 antd Alert) */
14
+ alert?: boolean;
15
+ /** Alert 风格:info(默认)/ warning / error */
16
+ alertStyle?: 'info' | 'warning' | 'error';
17
+ /** 列内垂直对齐(row 组件用) */
18
+ onchange?: (value: any) => void;
19
+ /** 允许透传扩展属性(span/when 等 form 修饰符) */
20
+ [key: string]: any;
21
+ }
22
+ let { text = '', size, color, bold, align, alert = false, alertStyle = 'info' }: Props = $props();
23
+
24
+ // Alert 风格图标(antd 风格:info 圆形 i / warning 圆形感叹 / error 圆形叉)
25
+ const alertIcons: Record<string, string> = {
26
+ info: 'ℹ',
27
+ warning: '⚠',
28
+ error: '✕'
29
+ };
30
+ </script>
31
+
32
+ {#if alert}
33
+ <div
34
+ class="widget-alert widget-alert-{alertStyle}"
35
+ style={[align ? `text-align: ${align}` : ''].filter(Boolean).join('; ')}
36
+ role="alert"
37
+ >
38
+ <span class="widget-alert-icon" aria-hidden="true">{alertIcons[alertStyle] ?? alertIcons.info}</span>
39
+ <span
40
+ class="widget-alert-message"
41
+ style={[
42
+ size ? `font-size: ${size}px` : '',
43
+ color ? `color: ${color}` : '',
44
+ bold ? 'font-weight: 600' : ''
45
+ ].filter(Boolean).join('; ')}
46
+ >{text}</span>
47
+ </div>
48
+ {:else}
49
+ <p
50
+ class="widget-text"
51
+ style={[
52
+ size ? `font-size: ${size}px` : '',
53
+ color ? `color: ${color}` : '',
54
+ bold ? 'font-weight: 600' : '',
55
+ align ? `text-align: ${align}` : ''
56
+ ].filter(Boolean).join('; ')}
57
+ >{text}</p>
58
+ {/if}
@@ -0,0 +1,58 @@
1
+ <script lang="ts">
2
+ interface Props {
3
+ placeholder?: string;
4
+ label?: string;
5
+ default?: string;
6
+ hint?: string;
7
+ /** 是否必填:label 前红色 * */
8
+ required?: boolean;
9
+ /** 正则校验规则(JS 正则源字符串) */
10
+ pattern?: string;
11
+ /** 校验失败提示文案 */
12
+ message?: string;
13
+ /** 外部(form 提交时)传入的错误信息,优先于组件内部即时校验 */
14
+ error?: string;
15
+ /** 是否禁用 */
16
+ disabled?: boolean;
17
+ onchange: (value: string) => void;
18
+ }
19
+ let { placeholder = '', label = '', default: initial = '', hint = '', required = false, pattern = '', message = '', error = '', disabled = false, onchange }: Props = $props();
20
+ let value = $state(initial);
21
+ let localError = $state('');
22
+
23
+ /** 校验当前值:返回错误文案或空串 */
24
+ function validate(v: string): string {
25
+ if (required && !v.trim()) return message || (label ? `请填写${label}` : '该项为必填项');
26
+ if (pattern && v && !new RegExp(pattern).test(v)) return message || (label ? `${label}格式不正确` : '格式不正确');
27
+ return '';
28
+ }
29
+
30
+ function commit() {
31
+ localError = validate(value);
32
+ onchange(value);
33
+ }
34
+
35
+ const shownError = $derived(error || localError);
36
+ </script>
37
+
38
+ <div class="widget-text-input" class:widget-field-error={!!shownError}>
39
+ {#if label || hint || required}
40
+ <div class="widget-label-row">
41
+ {#if required}<span class="widget-required-mark" aria-hidden="true">*</span>{/if}
42
+ {#if label}<label>{label}</label>{/if}
43
+ {#if hint}<span class="widget-hint" data-hint={hint}>ⓘ</span>{/if}
44
+ </div>
45
+ {/if}
46
+ <div class="widget-field-content">
47
+ <input
48
+ type="text"
49
+ class:widget-input-error={!!shownError}
50
+ bind:value
51
+ {placeholder}
52
+ {disabled}
53
+ onfocusout={commit}
54
+ oninput={() => { if (localError) localError = validate(value); }}
55
+ />
56
+ {#if shownError}<div class="widget-error-text" role="alert">{shownError}</div>{/if}
57
+ </div>
58
+ </div>