@naptics/vue-collection 0.2.14 → 0.3.0
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.
- package/.github/workflows/build.yml +26 -0
- package/.github/workflows/deploy-demo.yml +46 -0
- package/.github/workflows/deploy-lib.yml +65 -0
- package/.gitlab-ci.yml +57 -0
- package/.nvmrc +1 -0
- package/.prettierrc +8 -0
- package/.vscode/extensions.json +10 -0
- package/.vscode/launch.json +23 -0
- package/.vscode/settings.json +13 -0
- package/babel.config.json +3 -0
- package/components/NAlert.d.ts +1 -44
- package/components/NBadge.d.ts +1 -133
- package/components/NBreadcrub.d.ts +2 -106
- package/components/NBreadcrub.js +1 -1
- package/components/NButton.d.ts +2 -118
- package/components/NCheckbox.d.ts +1 -32
- package/components/NCheckboxLabel.d.ts +1 -45
- package/components/NCheckboxLabel.js +1 -1
- package/components/NCrudModal.d.ts +9 -221
- package/components/NCrudModal.js +1 -1
- package/components/NDialog.d.ts +1 -110
- package/components/NDialog.js +1 -1
- package/components/NDropdown.d.ts +1 -69
- package/components/NDropdown.js +1 -1
- package/components/NDropzone.d.ts +1 -115
- package/components/NDropzone.js +1 -1
- package/components/NForm.d.ts +1 -23
- package/components/NFormModal.d.ts +9 -127
- package/components/NIconButton.d.ts +3 -159
- package/components/NIconButton.js +1 -1
- package/components/NIconCircle.d.ts +1 -87
- package/components/NInput.d.ts +1 -164
- package/components/NInput.js +1 -1
- package/components/NInputPhone.d.ts +2 -114
- package/components/NInputPhone.js +1 -1
- package/components/NInputSelect.d.ts +2 -187
- package/components/NInputSelect.js +1 -1
- package/components/NInputSuggestion.d.ts +2 -155
- package/components/NInputSuggestion.js +1 -1
- package/components/NLink.d.ts +1 -70
- package/components/NList.d.ts +1 -43
- package/components/NList.js +1 -1
- package/components/NLoadingIndicator.d.ts +1 -49
- package/components/NModal.d.ts +15 -227
- package/components/NModal.js +16 -2
- package/components/NPagination.d.ts +1 -63
- package/components/NSearchbar.d.ts +1 -56
- package/components/NSearchbarList.d.ts +3 -63
- package/components/NSearchbarList.js +1 -1
- package/components/NSelect.d.ts +2 -148
- package/components/NSelect.js +1 -1
- package/components/NSuggestionList.d.ts +3 -126
- package/components/NSuggestionList.js +5 -2
- package/components/NTable.d.ts +1 -85
- package/components/NTable.js +12 -6
- package/components/NTableAction.d.ts +2 -46
- package/components/NTableAction.js +1 -1
- package/components/NTextArea.d.ts +2 -181
- package/components/NTextArea.js +1 -1
- package/components/NTooltip.d.ts +1 -105
- package/components/NTooltip.js +1 -1
- package/components/NValInput.d.ts +7 -182
- package/components/NValInput.js +1 -1
- package/env.d.ts +15 -0
- package/eslint.config.cjs +29 -0
- package/index.html +13 -0
- package/package.json +21 -19
- package/postcss.config.js +6 -0
- package/public/favicon.ico +0 -0
- package/scripts/build-lib.sh +52 -0
- package/scripts/sync-node-types.js +70 -0
- package/src/demo/App.css +9 -0
- package/src/demo/App.tsx +5 -0
- package/src/demo/components/ColorGrid.tsx +26 -0
- package/src/demo/components/ComponentGrid.tsx +26 -0
- package/src/demo/components/ComponentSection.tsx +30 -0
- package/src/demo/components/VariantSection.tsx +18 -0
- package/src/demo/i18n/de.ts +7 -0
- package/src/demo/i18n/en.ts +7 -0
- package/src/demo/i18n/index.ts +24 -0
- package/src/demo/main.ts +13 -0
- package/src/demo/router/index.ts +21 -0
- package/src/demo/views/HomeView.tsx +94 -0
- package/src/demo/views/NavigationView.tsx +43 -0
- package/src/demo/views/presentation/AlertView.tsx +40 -0
- package/src/demo/views/presentation/BadgeView.tsx +61 -0
- package/src/demo/views/presentation/BreadcrumbView.tsx +52 -0
- package/src/demo/views/presentation/ButtonView.tsx +49 -0
- package/src/demo/views/presentation/CheckboxView.tsx +59 -0
- package/src/demo/views/presentation/DropdownView.tsx +59 -0
- package/src/demo/views/presentation/DropzoneView.tsx +39 -0
- package/src/demo/views/presentation/IconButtonView.tsx +47 -0
- package/src/demo/views/presentation/IconCircleView.tsx +38 -0
- package/src/demo/views/presentation/InputView.tsx +179 -0
- package/src/demo/views/presentation/LinkView.tsx +50 -0
- package/src/demo/views/presentation/ListView.tsx +29 -0
- package/src/demo/views/presentation/LoadingIndicatorView.tsx +38 -0
- package/src/demo/views/presentation/ModalView.tsx +210 -0
- package/src/demo/views/presentation/PaginationView.tsx +25 -0
- package/src/demo/views/presentation/SearchbarView.tsx +80 -0
- package/src/demo/views/presentation/TableView.tsx +146 -0
- package/src/demo/views/presentation/TooltipView.tsx +86 -0
- package/src/lib/components/NAlert.tsx +85 -0
- package/src/lib/components/NBadge.tsx +75 -0
- package/src/lib/components/NBreadcrub.tsx +97 -0
- package/src/lib/components/NButton.tsx +80 -0
- package/src/lib/components/NCheckbox.tsx +55 -0
- package/src/lib/components/NCheckboxLabel.tsx +51 -0
- package/src/lib/components/NCrudModal.tsx +133 -0
- package/src/lib/components/NDialog.tsx +182 -0
- package/src/lib/components/NDropdown.tsx +167 -0
- package/src/lib/components/NDropzone.tsx +265 -0
- package/src/lib/components/NForm.tsx +32 -0
- package/src/lib/components/NFormModal.tsx +66 -0
- package/src/lib/components/NIconButton.tsx +92 -0
- package/src/lib/components/NIconCircle.tsx +78 -0
- package/src/lib/components/NInput.css +11 -0
- package/src/lib/components/NInput.tsx +139 -0
- package/src/lib/components/NInputPhone.tsx +53 -0
- package/src/lib/components/NInputSelect.tsx +126 -0
- package/src/lib/components/NInputSuggestion.tsx +80 -0
- package/src/lib/components/NLink.tsx +68 -0
- package/src/lib/components/NList.tsx +67 -0
- package/src/lib/components/NLoadingIndicator.css +46 -0
- package/src/lib/components/NLoadingIndicator.tsx +63 -0
- package/src/lib/components/NModal.tsx +243 -0
- package/src/lib/components/NPagination.css +15 -0
- package/src/lib/components/NPagination.tsx +131 -0
- package/src/lib/components/NSearchbar.tsx +78 -0
- package/src/lib/components/NSearchbarList.tsx +47 -0
- package/src/lib/components/NSelect.tsx +128 -0
- package/src/lib/components/NSuggestionList.tsx +216 -0
- package/src/lib/components/NTable.css +3 -0
- package/src/lib/components/NTable.tsx +247 -0
- package/src/lib/components/NTableAction.tsx +49 -0
- package/src/lib/components/NTextArea.tsx +159 -0
- package/src/lib/components/NTooltip.css +37 -0
- package/src/lib/components/NTooltip.tsx +250 -0
- package/src/lib/components/NValInput.tsx +163 -0
- package/src/lib/components/ValidatedForm.ts +71 -0
- package/src/lib/components/__tests__/NButton.spec.tsx +26 -0
- package/src/lib/components/__tests__/NCheckbox.spec.tsx +39 -0
- package/src/lib/i18n/de/vue-collection.json +58 -0
- package/src/lib/i18n/en/vue-collection.json +58 -0
- package/src/lib/i18n/index.ts +54 -0
- package/src/lib/index.ts +2 -0
- package/src/lib/jsx.d.ts +13 -0
- package/src/lib/utils/__tests__/identifiable.spec.ts +72 -0
- package/src/lib/utils/__tests__/validation.spec.ts +92 -0
- package/src/lib/utils/breakpoints.ts +47 -0
- package/src/lib/utils/component.tsx +131 -0
- package/src/lib/utils/deferred.ts +28 -0
- package/src/lib/utils/identifiable.ts +87 -0
- package/src/lib/utils/stringMaxLength.ts +25 -0
- package/src/lib/utils/tailwind.ts +41 -0
- package/src/lib/utils/utils.ts +90 -0
- package/src/lib/utils/vModel.ts +260 -0
- package/src/lib/utils/validation.ts +189 -0
- package/src/lib/utils/vue.ts +25 -0
- package/tailwind.config.js +38 -0
- package/tsconfig.config.json +9 -0
- package/tsconfig.demo.json +19 -0
- package/tsconfig.json +16 -0
- package/tsconfig.lib.json +18 -0
- package/tsconfig.vitest.json +8 -0
- package/utils/breakpoints.d.ts +1 -1
- package/utils/component.d.ts +3 -7
- package/utils/component.js +5 -2
- package/utils/identifiable.js +5 -1
- package/vite.config.ts +28 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { createComponent, type ExtractedProps } from '../utils/component'
|
|
2
|
+
import { uniqueId } from '../utils/utils'
|
|
3
|
+
import type { TWMaxWidth } from '../utils/tailwind'
|
|
4
|
+
import { computed, onMounted, ref, watch, type PropType, onUnmounted, Transition } from 'vue'
|
|
5
|
+
import { createPopper, type Instance as PopperInstance } from '@popperjs/core'
|
|
6
|
+
import { watchRef } from '../utils/vue'
|
|
7
|
+
import './NTooltip.css'
|
|
8
|
+
|
|
9
|
+
export const nTooltipProps = {
|
|
10
|
+
/**
|
|
11
|
+
* The text content of the tooltip.
|
|
12
|
+
*/
|
|
13
|
+
text: String,
|
|
14
|
+
/**
|
|
15
|
+
* A slot to replace the content of the tooltip. This will override the `text` prop.
|
|
16
|
+
*/
|
|
17
|
+
content: Function as PropType<() => JSX.Element>,
|
|
18
|
+
/**
|
|
19
|
+
* If set to `true` the tooltip is shown constantly.
|
|
20
|
+
*/
|
|
21
|
+
show: Boolean,
|
|
22
|
+
/**
|
|
23
|
+
* If set to `true` the tooltip is hidden constantly.
|
|
24
|
+
*/
|
|
25
|
+
hide: Boolean,
|
|
26
|
+
/**
|
|
27
|
+
* If set to `true` the `block` class is applied to the tooltip.
|
|
28
|
+
* This should be set if the content in the default slot is also block.
|
|
29
|
+
*/
|
|
30
|
+
block: Boolean,
|
|
31
|
+
/**
|
|
32
|
+
* The placement of the tooltip.
|
|
33
|
+
*/
|
|
34
|
+
placement: {
|
|
35
|
+
type: String as PropType<TooltipPlacement>,
|
|
36
|
+
default: 'auto',
|
|
37
|
+
},
|
|
38
|
+
/**
|
|
39
|
+
* The maximum width of the tooltip.
|
|
40
|
+
*/
|
|
41
|
+
maxWidth: {
|
|
42
|
+
type: String as PropType<TWMaxWidth>,
|
|
43
|
+
default: 'max-w-xs',
|
|
44
|
+
},
|
|
45
|
+
/**
|
|
46
|
+
* Adds the classes to the (invisible) wrapper element.
|
|
47
|
+
*/
|
|
48
|
+
wrapperClass: String,
|
|
49
|
+
/**
|
|
50
|
+
* Adds the classes to the container of the tooltips content.
|
|
51
|
+
*/
|
|
52
|
+
contentClass: String,
|
|
53
|
+
/**
|
|
54
|
+
* Adds the classes to the tooltip arrow. Make sure to use `before:` classes
|
|
55
|
+
* to target the arrow (which is the before element).
|
|
56
|
+
*/
|
|
57
|
+
arrowClass: String,
|
|
58
|
+
} as const
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* These props are made to use on a component which implements the tooltip
|
|
62
|
+
* and wants it to be controllable via the own props.
|
|
63
|
+
* e.g. `text` is now called `tooltipText`.
|
|
64
|
+
* They can be mapped to the normal tooltip props with {@link mapTooltipProps}
|
|
65
|
+
*/
|
|
66
|
+
export const nToolTipPropsForImplementor = {
|
|
67
|
+
/**
|
|
68
|
+
* Adds a tooltip to the component with the specified text.
|
|
69
|
+
* @see {@link nTooltipProps.text}
|
|
70
|
+
*/
|
|
71
|
+
tooltipText: nTooltipProps.text,
|
|
72
|
+
/**
|
|
73
|
+
* A slot for the tooltip of this component.
|
|
74
|
+
* If the slot is set, the tooltip with the specified content is added to the component.
|
|
75
|
+
* @see {@link nTooltipProps.content}
|
|
76
|
+
*/
|
|
77
|
+
tooltipContent: nTooltipProps.content,
|
|
78
|
+
/**
|
|
79
|
+
* @see {@link nTooltipProps.hide}
|
|
80
|
+
*/
|
|
81
|
+
tooltipHide: nTooltipProps.hide,
|
|
82
|
+
/**
|
|
83
|
+
* @see {@link nTooltipProps.show}
|
|
84
|
+
*/
|
|
85
|
+
tooltipShow: nTooltipProps.show,
|
|
86
|
+
/**
|
|
87
|
+
* @see {@link nTooltipProps.placement}
|
|
88
|
+
*/
|
|
89
|
+
tooltipPlacement: nTooltipProps.placement,
|
|
90
|
+
/**
|
|
91
|
+
* @see {@link nTooltipProps.maxWidth}
|
|
92
|
+
*/
|
|
93
|
+
tooltipMaxWidth: nTooltipProps.maxWidth,
|
|
94
|
+
/**
|
|
95
|
+
* @see {@link nTooltipProps.wrapperClass}
|
|
96
|
+
*/
|
|
97
|
+
tooltipWrapperClass: nTooltipProps.wrapperClass,
|
|
98
|
+
/**
|
|
99
|
+
* @see {@link nTooltipProps.contentClass}
|
|
100
|
+
*/
|
|
101
|
+
tooltipContentClass: nTooltipProps.contentClass,
|
|
102
|
+
/**
|
|
103
|
+
* @see {@link nTooltipProps.arrowClass}
|
|
104
|
+
*/
|
|
105
|
+
tooltipArrowClass: nTooltipProps.arrowClass,
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Maps the {@link nToolTipPropsForImplementor} props to normal tooltip props
|
|
110
|
+
* @returns an object containing the normal tooltip props.
|
|
111
|
+
*/
|
|
112
|
+
export function mapTooltipProps(props: ExtractedProps<typeof nToolTipPropsForImplementor>) {
|
|
113
|
+
return {
|
|
114
|
+
text: props.tooltipText,
|
|
115
|
+
content: props.tooltipContent,
|
|
116
|
+
hide: props.tooltipHide,
|
|
117
|
+
show: props.tooltipShow,
|
|
118
|
+
placement: props.tooltipPlacement,
|
|
119
|
+
maxWidth: props.tooltipMaxWidth,
|
|
120
|
+
wrapperClass: props.tooltipWrapperClass,
|
|
121
|
+
contentClass: props.tooltipContentClass,
|
|
122
|
+
arrowClass: props.tooltipArrowClass,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The `NTooltip` is a wrapper for any component which adds a tooltip to it.
|
|
128
|
+
* Any component can just be passed in the default slot and a tooltip will be added to it.
|
|
129
|
+
* Note that this component disappears when neither the `text` nor the `content`
|
|
130
|
+
* prop is passed as the tooltip would then be empty.
|
|
131
|
+
* If this is the case, the default slot will just be rendered inside a div.
|
|
132
|
+
* @example
|
|
133
|
+
* <NTooltip text="Hello">
|
|
134
|
+
* <NButton />
|
|
135
|
+
* </NTooltip>
|
|
136
|
+
*/
|
|
137
|
+
const Component = createComponent('NTooltip', nTooltipProps, (props, { slots }) => {
|
|
138
|
+
return () => (
|
|
139
|
+
<div class={[props.block ? 'block' : 'inline-block', props.wrapperClass]}>
|
|
140
|
+
{props.content || props.text ? (
|
|
141
|
+
<NTooltipBase {...props}>{slots.default?.()}</NTooltipBase>
|
|
142
|
+
) : (
|
|
143
|
+
slots.default?.()
|
|
144
|
+
)}
|
|
145
|
+
</div>
|
|
146
|
+
)
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
export { Component as NTooltip, Component as default }
|
|
150
|
+
|
|
151
|
+
const NTooltipBase = createComponent('NTooltipBase', nTooltipProps, (props, { slots }) => {
|
|
152
|
+
let popperInstance: PopperInstance | null = null
|
|
153
|
+
const contentId = `content-${uniqueId()}`
|
|
154
|
+
const tooltipId = `tooltip-${uniqueId()}`
|
|
155
|
+
|
|
156
|
+
function createTooltip() {
|
|
157
|
+
const content = document.getElementById(contentId)
|
|
158
|
+
const tooltip = document.getElementById(tooltipId)
|
|
159
|
+
if (content && tooltip) {
|
|
160
|
+
popperInstance = createPopper(content, tooltip, {
|
|
161
|
+
placement: props.placement,
|
|
162
|
+
modifiers: [
|
|
163
|
+
{
|
|
164
|
+
name: 'offset',
|
|
165
|
+
options: {
|
|
166
|
+
offset: [0, 8],
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
})
|
|
171
|
+
} else {
|
|
172
|
+
console.error('Could not create tooltip. HTML elements for content or tooltip were not found.')
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function destroyTooltip() {
|
|
177
|
+
popperInstance?.destroy()
|
|
178
|
+
popperInstance = null
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
onMounted(createTooltip)
|
|
182
|
+
onUnmounted(destroyTooltip)
|
|
183
|
+
|
|
184
|
+
watch(
|
|
185
|
+
() => props.placement,
|
|
186
|
+
newPlacement => popperInstance?.setOptions({ placement: newPlacement })
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
const isHoveringContent = ref(false)
|
|
190
|
+
const isHoveringTooltip = ref(false)
|
|
191
|
+
const isHovering = computed(() => isHoveringContent.value || isHoveringTooltip.value)
|
|
192
|
+
const showTooltip = computed(() => props.show || (!props.hide && isHovering.value))
|
|
193
|
+
|
|
194
|
+
watchRef(showTooltip, () => popperInstance?.update())
|
|
195
|
+
|
|
196
|
+
return () => (
|
|
197
|
+
<>
|
|
198
|
+
<div
|
|
199
|
+
class="p-[10px] -m-[10px]"
|
|
200
|
+
onMouseleave={() => setTimeout(() => (isHoveringContent.value = false), 10)}
|
|
201
|
+
>
|
|
202
|
+
<div id={contentId} onMouseenter={() => (isHoveringContent.value = true)}>
|
|
203
|
+
{slots.default?.()}
|
|
204
|
+
</div>
|
|
205
|
+
</div>
|
|
206
|
+
|
|
207
|
+
<Transition
|
|
208
|
+
enterActiveClass="transition-opacity ease-out duration-100"
|
|
209
|
+
enterFromClass="opacity-0"
|
|
210
|
+
enterToClass="opacity-100"
|
|
211
|
+
leaveActiveClass="transition-opacity ease-in duration-75"
|
|
212
|
+
leaveFromClass="opacity-100"
|
|
213
|
+
leaveToClass="opacity-0"
|
|
214
|
+
>
|
|
215
|
+
<div
|
|
216
|
+
id={tooltipId}
|
|
217
|
+
role="tooltip"
|
|
218
|
+
onMouseenter={() => (isHoveringTooltip.value = true)}
|
|
219
|
+
onMouseleave={() => (isHoveringTooltip.value = false)}
|
|
220
|
+
v-show={showTooltip.value}
|
|
221
|
+
class={[isHovering.value ? 'z-20' : 'z-10', props.maxWidth, 'tooltip']}
|
|
222
|
+
>
|
|
223
|
+
<div
|
|
224
|
+
class={`bg-white rounded-md py-2 px-4 shadow-lg border-default-200 border text-sm whitespace-normal font-normal text-default-700 ${props.contentClass}`}
|
|
225
|
+
>
|
|
226
|
+
{props.content?.() || props.text}
|
|
227
|
+
</div>
|
|
228
|
+
<div data-popper-arrow class={`arrow ${props.arrowClass}`} />
|
|
229
|
+
</div>
|
|
230
|
+
</Transition>
|
|
231
|
+
</>
|
|
232
|
+
)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
export type TooltipPlacement =
|
|
236
|
+
| 'auto'
|
|
237
|
+
| 'auto-start'
|
|
238
|
+
| 'auto-end'
|
|
239
|
+
| 'top'
|
|
240
|
+
| 'top-start'
|
|
241
|
+
| 'top-end'
|
|
242
|
+
| 'bottom'
|
|
243
|
+
| 'bottom-start'
|
|
244
|
+
| 'bottom-end'
|
|
245
|
+
| 'right'
|
|
246
|
+
| 'right-start'
|
|
247
|
+
| 'right-end'
|
|
248
|
+
| 'left'
|
|
249
|
+
| 'left-start'
|
|
250
|
+
| 'left-end'
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { createComponentWithSlots } from '../utils/component'
|
|
2
|
+
import { computed, onMounted, onUnmounted } from 'vue'
|
|
3
|
+
import { ref, reactive, type PropType, watch } from 'vue'
|
|
4
|
+
import NInput, { nInputProps, type NInputExposed } from './NInput'
|
|
5
|
+
import {
|
|
6
|
+
type ValidationRule,
|
|
7
|
+
type ValidationResult,
|
|
8
|
+
validate,
|
|
9
|
+
type InputValue,
|
|
10
|
+
required,
|
|
11
|
+
validResult,
|
|
12
|
+
} from '../utils/validation'
|
|
13
|
+
import type { ValidatedForm } from './ValidatedForm'
|
|
14
|
+
|
|
15
|
+
export const validationProps = {
|
|
16
|
+
/**
|
|
17
|
+
* If set to `true` this inputs validation will always succeed and all validation
|
|
18
|
+
* rules are ignored. Default is `false`.
|
|
19
|
+
*/
|
|
20
|
+
disableValidation: Boolean,
|
|
21
|
+
/**
|
|
22
|
+
* If set to `true` this input is always valid when its value is empty.
|
|
23
|
+
* If set to `false` the input receives the {@link required} rule. Default is `false`.
|
|
24
|
+
*/
|
|
25
|
+
optional: Boolean,
|
|
26
|
+
/**
|
|
27
|
+
* The rules which this input is checked with.
|
|
28
|
+
* The rules are checked sequentially and the error of the first failed rule is displayed.
|
|
29
|
+
* If `optional` is set to false, the rule {@link required} will be checked first.
|
|
30
|
+
*/
|
|
31
|
+
rules: {
|
|
32
|
+
type: [Function, Array] as PropType<ValidationRule[] | ValidationRule>,
|
|
33
|
+
default: () => [],
|
|
34
|
+
},
|
|
35
|
+
/**
|
|
36
|
+
* The form, which this input will be added to.
|
|
37
|
+
* On initialization, this input will call {@link ValidatedForm.addInput} passing itself to the form.
|
|
38
|
+
*/
|
|
39
|
+
form: Object as PropType<ValidatedForm>,
|
|
40
|
+
/**
|
|
41
|
+
* Overrides the internal error state. If set to true, it will always display an error.
|
|
42
|
+
*/
|
|
43
|
+
error: Boolean,
|
|
44
|
+
/**
|
|
45
|
+
* Overrides the internal error message. If set, this message is always displayed.
|
|
46
|
+
*/
|
|
47
|
+
errorMessage: String,
|
|
48
|
+
/**
|
|
49
|
+
* If set to `true` the error message is not shown.
|
|
50
|
+
* However, the input is still marked red if it is in an error state.
|
|
51
|
+
*/
|
|
52
|
+
hideErrorMessage: Boolean,
|
|
53
|
+
/**
|
|
54
|
+
* Disables the validation on blur. Should only be used in special occasions.
|
|
55
|
+
*/
|
|
56
|
+
disableBlurValidation: Boolean,
|
|
57
|
+
} as const
|
|
58
|
+
|
|
59
|
+
export const nValInputProps = {
|
|
60
|
+
...nInputProps,
|
|
61
|
+
...validationProps,
|
|
62
|
+
/**
|
|
63
|
+
* A slot to replace the input.
|
|
64
|
+
*/
|
|
65
|
+
input: Function as PropType<(props: InputSlotProps) => JSX.Element>,
|
|
66
|
+
} as const
|
|
67
|
+
|
|
68
|
+
export type InputSlotProps = {
|
|
69
|
+
onBlur(): void
|
|
70
|
+
onUpdateValue(newValue: string): void
|
|
71
|
+
error: boolean
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The exposed functions of NValInput
|
|
76
|
+
*/
|
|
77
|
+
export type NValInputExposed = {
|
|
78
|
+
/**
|
|
79
|
+
* Validates the input and returns the validation result
|
|
80
|
+
*/
|
|
81
|
+
validate: () => ValidationResult
|
|
82
|
+
/**
|
|
83
|
+
* Resets the validation state of the input.
|
|
84
|
+
*/
|
|
85
|
+
reset: () => void
|
|
86
|
+
} & NInputExposed
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The `NValInput` is a `NInput` with custom validation.
|
|
90
|
+
*/
|
|
91
|
+
const Component = createComponentWithSlots('NValInput', nValInputProps, ['input'], (props, context) => {
|
|
92
|
+
const rules = computed(() => {
|
|
93
|
+
const otherRules = Array.isArray(props.rules) ? props.rules : [props.rules]
|
|
94
|
+
return props.optional ? otherRules : [required, ...otherRules]
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
const validationResult = ref<ValidationResult>()
|
|
98
|
+
const validateRules = (input: InputValue) => {
|
|
99
|
+
const result = props.disableValidation ? validResult() : validate(input, rules.value)
|
|
100
|
+
validationResult.value = result
|
|
101
|
+
return result
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const showError = computed(() => props.error || (validationResult.value != null && !validationResult.value.isValid))
|
|
105
|
+
const showErrorMessage = computed(() => !props.hideErrorMessage && showError.value)
|
|
106
|
+
const errorMessage = computed(() => props.errorMessage || validationResult.value?.errorMessage)
|
|
107
|
+
|
|
108
|
+
const validateIfError = (value = props.value) => {
|
|
109
|
+
if (showError.value) validateRules(value)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
watch(
|
|
113
|
+
() => props.value,
|
|
114
|
+
() => validateIfError()
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
watch(
|
|
118
|
+
() => rules.value,
|
|
119
|
+
() => validateIfError()
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
watch(
|
|
123
|
+
() => props.disableValidation,
|
|
124
|
+
() => validateIfError()
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
const onBlur = () => {
|
|
128
|
+
if (!props.disableBlurValidation) validateRules(props.value)
|
|
129
|
+
props.onBlur?.()
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const onUpdateValue = (newValue: string) => {
|
|
133
|
+
validateIfError(newValue)
|
|
134
|
+
props.onUpdateValue?.(newValue)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const inputSlotProps: InputSlotProps = reactive({
|
|
138
|
+
onBlur,
|
|
139
|
+
onUpdateValue,
|
|
140
|
+
error: showError,
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
const inputRef = ref<NInputExposed>()
|
|
144
|
+
const expose: NValInputExposed = {
|
|
145
|
+
validate: () => validateRules(props.value),
|
|
146
|
+
reset: () => (validationResult.value = undefined),
|
|
147
|
+
focus: () => inputRef.value?.focus(),
|
|
148
|
+
}
|
|
149
|
+
context.expose(expose)
|
|
150
|
+
|
|
151
|
+
let assignedId: string | undefined = undefined
|
|
152
|
+
onMounted(() => (assignedId = props.form?.addInput(expose)))
|
|
153
|
+
onUnmounted(() => assignedId && props.form?.removeInput(assignedId))
|
|
154
|
+
|
|
155
|
+
return () => (
|
|
156
|
+
<div>
|
|
157
|
+
{props.input?.(inputSlotProps) || <NInput ref={inputRef} {...{ ...props, ...inputSlotProps }} />}
|
|
158
|
+
{showErrorMessage.value && <p class="text-red-500 text-xs mt-1">{errorMessage.value}</p>}
|
|
159
|
+
</div>
|
|
160
|
+
)
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
export { Component as NValInput, Component as default }
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Id, type Identifiable } from '../utils/identifiable'
|
|
2
|
+
import { uniqueId } from '../utils/utils'
|
|
3
|
+
import type { ValidationResult } from '../utils/validation'
|
|
4
|
+
import type { NValInputExposed } from './NValInput'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A ValidatedForm groups different inputs together and provides functions, which operate on all inputs at the same time.
|
|
8
|
+
* With a form multiple inputs can be validated together by calling the validate function.
|
|
9
|
+
* @example
|
|
10
|
+
* const form = createValidatedForm()
|
|
11
|
+
* <NValInput ... form={form} />
|
|
12
|
+
* <NValInput ... form={form} />
|
|
13
|
+
* const onClick = () => form.validate()
|
|
14
|
+
*/
|
|
15
|
+
export type ValidatedForm = {
|
|
16
|
+
/**
|
|
17
|
+
* Adds the input to the list of this form and returns the assigned `id`.
|
|
18
|
+
* If this form is passed to a `<NValInput />` via the props, will add itself to this form.
|
|
19
|
+
* @returns the newly assigned `id` of the added input.
|
|
20
|
+
* @example
|
|
21
|
+
* <NValInput ... form={form} />
|
|
22
|
+
*/
|
|
23
|
+
addInput(input: NValInputExposed): string
|
|
24
|
+
/**
|
|
25
|
+
* Removes the input with the given `id` from this form.
|
|
26
|
+
*/
|
|
27
|
+
removeInput(id: string): void
|
|
28
|
+
/**
|
|
29
|
+
* Validates all inputs of the form. If inputs are invalid they will show it visually.
|
|
30
|
+
* The first invalid validation result is returned.
|
|
31
|
+
*/
|
|
32
|
+
validate(): ValidationResult
|
|
33
|
+
/**
|
|
34
|
+
* Resets the validation state of all inputs.
|
|
35
|
+
*/
|
|
36
|
+
reset(): void
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new ValidatedForm.
|
|
41
|
+
* @returns the instance of the new form.
|
|
42
|
+
*/
|
|
43
|
+
export function createValidatedForm(): ValidatedForm {
|
|
44
|
+
return new ValidatedFormImpl()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class ValidatedFormImpl implements ValidatedForm {
|
|
48
|
+
inputs: (NValInputExposed & Identifiable)[] = []
|
|
49
|
+
|
|
50
|
+
addInput(input: NValInputExposed): string {
|
|
51
|
+
const id = `input-${uniqueId()}`
|
|
52
|
+
this.inputs.push({ id, ...input })
|
|
53
|
+
return id
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
removeInput(id: string): void {
|
|
57
|
+
Id.remove(this.inputs, id)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
validate(): ValidationResult {
|
|
61
|
+
const results = this.inputs.map(input => input.validate())
|
|
62
|
+
// return first invalid result
|
|
63
|
+
for (const result of results) if (result && !result.isValid) return result
|
|
64
|
+
// else return valid result
|
|
65
|
+
return { isValid: true }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
reset(): void {
|
|
69
|
+
this.inputs.forEach(input => input.reset())
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest'
|
|
2
|
+
import { mount } from '@vue/test-utils'
|
|
3
|
+
import NButton from '../NButton'
|
|
4
|
+
|
|
5
|
+
describe('<NButton>', () => {
|
|
6
|
+
it('shows text', () => {
|
|
7
|
+
const button = mount(() => <NButton>Click me!</NButton>)
|
|
8
|
+
expect(button.text()).toMatch('Click me!')
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('calls onClick when clicked', async () => {
|
|
12
|
+
const onClick = vi.fn()
|
|
13
|
+
const button = mount(() => <NButton onClick={onClick} />).get('button')
|
|
14
|
+
await button.trigger('click')
|
|
15
|
+
expect(onClick).toHaveBeenCalledOnce()
|
|
16
|
+
await button.trigger('click')
|
|
17
|
+
expect(onClick).toHaveBeenCalledTimes(2)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('does not call onClick when disabled', async () => {
|
|
21
|
+
const onClick = vi.fn()
|
|
22
|
+
const button = mount(() => <NButton onClick={onClick} disabled={true} />).get('button')
|
|
23
|
+
await button.trigger('click')
|
|
24
|
+
expect(onClick).not.toHaveBeenCalled()
|
|
25
|
+
})
|
|
26
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { mount } from '@vue/test-utils'
|
|
2
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
3
|
+
import NCheckbox from '../NCheckbox'
|
|
4
|
+
|
|
5
|
+
describe('<NCheckbox>', () => {
|
|
6
|
+
test('handles value and calls onUpdateValue correctly', async () => {
|
|
7
|
+
const onUpdateValue = vi.fn()
|
|
8
|
+
|
|
9
|
+
let checkbox = mount(() => <NCheckbox value={false} onUpdateValue={onUpdateValue} />).get('input')
|
|
10
|
+
await checkbox.trigger('click')
|
|
11
|
+
expect(onUpdateValue).toHaveBeenLastCalledWith(true)
|
|
12
|
+
expect(onUpdateValue).toHaveBeenCalledTimes(1)
|
|
13
|
+
|
|
14
|
+
// test twice (should not have changed because we did not change the value)
|
|
15
|
+
await checkbox.trigger('click')
|
|
16
|
+
expect(onUpdateValue).toHaveBeenLastCalledWith(true)
|
|
17
|
+
expect(onUpdateValue).toHaveBeenCalledTimes(2)
|
|
18
|
+
|
|
19
|
+
onUpdateValue.mockReset()
|
|
20
|
+
checkbox = mount(() => <NCheckbox value={true} onUpdateValue={onUpdateValue} />).get('input')
|
|
21
|
+
|
|
22
|
+
await checkbox.trigger('click')
|
|
23
|
+
expect(onUpdateValue).toHaveBeenLastCalledWith(false)
|
|
24
|
+
expect(onUpdateValue).toHaveBeenCalledTimes(1)
|
|
25
|
+
|
|
26
|
+
// test twice (should not have changed because we did not change the value)
|
|
27
|
+
await checkbox.trigger('click')
|
|
28
|
+
expect(onUpdateValue).toHaveBeenLastCalledWith(false)
|
|
29
|
+
expect(onUpdateValue).toHaveBeenCalledTimes(2)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('does not call onUpdateValue when disabled', async () => {
|
|
33
|
+
const onUpdateValue = vi.fn()
|
|
34
|
+
|
|
35
|
+
const checkbox = mount(() => <NCheckbox disabled onUpdateValue={onUpdateValue} />).get('input')
|
|
36
|
+
await checkbox.trigger('click')
|
|
37
|
+
expect(onUpdateValue).not.toHaveBeenCalled()
|
|
38
|
+
})
|
|
39
|
+
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"title": {},
|
|
3
|
+
"subtitle": {},
|
|
4
|
+
"text": {
|
|
5
|
+
"loading-search-results": "Ergebnisse werden geladen...",
|
|
6
|
+
"no-search-results": "Keine Ergebnisse für «{input}».",
|
|
7
|
+
"drag-n-drop-files": "Datei auswählen oder per Drag & Drop hinzufügen. | Bis zu {n} Dateien auswählen oder per Drag & Drop hinzufügen.",
|
|
8
|
+
"files-selected": "Keine Datei ausgewählt. | Eine Datei ausgewählt. | {n} Dateien ausgewählt."
|
|
9
|
+
},
|
|
10
|
+
"property": {},
|
|
11
|
+
"term": {},
|
|
12
|
+
"action": {
|
|
13
|
+
"search": "Suchen",
|
|
14
|
+
"select": "Auswählen",
|
|
15
|
+
"remove": "Löschen",
|
|
16
|
+
"cancel": "Abbrechen",
|
|
17
|
+
"all-right": "Alles klar",
|
|
18
|
+
"proceed": "Fortfahren",
|
|
19
|
+
"save": "Speichern",
|
|
20
|
+
"clear-files": "Auswahl löschen."
|
|
21
|
+
},
|
|
22
|
+
"enum": {},
|
|
23
|
+
"error": {
|
|
24
|
+
"file-type": "Eine Datei wurde nicht hinzugefügt, da sie im falschen Format ist. | {n} Dateien wurden nicht hinzugefügt, da sie im falschen Format sind.",
|
|
25
|
+
"file-size": "Eine Datei wurde nicht hinzugefügt, da sie zu gross ist. | {n} Dateien wurden nicht hinzugefügt, da sie zu gross sind.",
|
|
26
|
+
"too-many-files": "Es kann nur eine Datei hinzugefügt werden. | Es können nicht mehr als {n} Dateien hinzugefügt werden."
|
|
27
|
+
},
|
|
28
|
+
"validation": {
|
|
29
|
+
"rules": {
|
|
30
|
+
"email": "Dieses Feld muss eine gültige Email-Adresse sein.",
|
|
31
|
+
"integer": "Dieses Feld muss eine Ganzzahl sein.",
|
|
32
|
+
"length": {
|
|
33
|
+
"min": "Der Text darf nicht weniger als {min} Zeichen enthalten.",
|
|
34
|
+
"max": "Der Text darf nicht mehr als {max} Zeichen enthalten.",
|
|
35
|
+
"min-max": "Der Text muss zwischen {min} und {max} Zeichen enthalten."
|
|
36
|
+
},
|
|
37
|
+
"matches": "Die Felder stimmen nicht überein.",
|
|
38
|
+
"number-range": {
|
|
39
|
+
"nan": "Dieses Feld muss eine Zahl sein.",
|
|
40
|
+
"min": "Die Zahl darf nicht kleiner als {min} sein.",
|
|
41
|
+
"max": "Die Zahl darf nicht grösser als {max} sein.",
|
|
42
|
+
"min-max": "Die Zahl muss im Bereich von {min} bis {max} liegen."
|
|
43
|
+
},
|
|
44
|
+
"option": "Dieses Feld muss eine Option aus der Liste sein.",
|
|
45
|
+
"password": {
|
|
46
|
+
"to-short": "Das Passwort muss aus mindestens 8 Zeichen bestehen.",
|
|
47
|
+
"no-lowercase": "Das Passwort muss einen Kleinbuchstaben enthalten.",
|
|
48
|
+
"no-uppercase": "Das Passwort muss einen Grossbuchstaben enthalten.",
|
|
49
|
+
"no-digits": "Das Passwort muss eine Ziffer enthalten.",
|
|
50
|
+
"no-special-chars": "Das Passwort muss ein Sonderzeichen enthalten.",
|
|
51
|
+
"unknown": "Dieses Feld erfüllt nicht die Anforderungen für ein Passwort."
|
|
52
|
+
},
|
|
53
|
+
"phone": "Dieses Feld muss eine gültige Telefonnummer sein.",
|
|
54
|
+
"required": "Dieses Feld ist ein Pflichtfeld.",
|
|
55
|
+
"regex": "Dieses Feld entspricht nicht dem geforderten Format."
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"title": {},
|
|
3
|
+
"subtitle": {},
|
|
4
|
+
"text": {
|
|
5
|
+
"loading-search-results": "Loading results...",
|
|
6
|
+
"no-search-results": "No results found for «{input}».",
|
|
7
|
+
"drag-n-drop-files": "Select a file or add it by drag & drop. | Select up to {n} files or add them by drag & drop.",
|
|
8
|
+
"files-selected": "No file selected. | One file selected. | {n} files selected."
|
|
9
|
+
},
|
|
10
|
+
"property": {},
|
|
11
|
+
"term": {},
|
|
12
|
+
"action": {
|
|
13
|
+
"search": "Search",
|
|
14
|
+
"select": "Select",
|
|
15
|
+
"remove": "Delete",
|
|
16
|
+
"cancel": "Cancel",
|
|
17
|
+
"all-right": "All right",
|
|
18
|
+
"proceed": "Continue",
|
|
19
|
+
"save": "Save",
|
|
20
|
+
"clear-files": "Clear. | Clear all."
|
|
21
|
+
},
|
|
22
|
+
"enum": {},
|
|
23
|
+
"error": {
|
|
24
|
+
"file-type": "One file has not been added as it is in the wrong format. | {n} files have not been added as they are in the wrong format.",
|
|
25
|
+
"file-size": "One file has not been added as it exceeds the maximum file size. | {n} files have not been added as they exceed the maximum file size.",
|
|
26
|
+
"too-many-files": "Only one file can be added. | Can not add more than {max} files."
|
|
27
|
+
},
|
|
28
|
+
"validation": {
|
|
29
|
+
"rules": {
|
|
30
|
+
"email": "This field must be a valid email address.",
|
|
31
|
+
"integer": "This field must be an integer.",
|
|
32
|
+
"length": {
|
|
33
|
+
"min": "The text must not have less than {min} characters.",
|
|
34
|
+
"max": "The text must not have more than {max} characters.",
|
|
35
|
+
"min-max": "The text must have a length between {min} and {max} characters."
|
|
36
|
+
},
|
|
37
|
+
"matches": "The fields don't match.",
|
|
38
|
+
"number-range": {
|
|
39
|
+
"nan": "This filed must be a number.",
|
|
40
|
+
"min": "The number must not be smaller than {min}.",
|
|
41
|
+
"max": "The number must not be bigger than {max}.",
|
|
42
|
+
"min-max": "The number must be in the range from {min} to {max}."
|
|
43
|
+
},
|
|
44
|
+
"option": "This field has to be an option from the list.",
|
|
45
|
+
"password": {
|
|
46
|
+
"to-short": "The password must have at least 8 characters.",
|
|
47
|
+
"no-lowercase": "The password must contain a lowercase letter.",
|
|
48
|
+
"no-uppercase": "The password must contain an uppercase letter.",
|
|
49
|
+
"no-digits": "The password must contain a digit.",
|
|
50
|
+
"no-special-chars": "The password must contain a special character.",
|
|
51
|
+
"unknown": "This field does not match the required password format."
|
|
52
|
+
},
|
|
53
|
+
"phone": "This field must be a valid phone number.",
|
|
54
|
+
"required": "This field is required.",
|
|
55
|
+
"regex": "This field does not conform to the required format."
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|