Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(validation): format numbers to make them more readable #563

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/validation/rules/maxLength.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { createRule } from '../Rule'
import { maxLength as baseMaxLength } from '../validators'

export const message = {
en: (length: number) => `The value must be less than or equal to ${length} characters.`,
ja: (length: number) => `この値は最大${length}文字までです。`
en: (length: string) => `The value must be less than or equal to ${length} characters.`,
ja: (length: string) => `この値は最大${length}文字までです。`
}

export function maxLength(length: number, msg?: string) {
const formatted = Intl.NumberFormat().format(length)
brc-dd marked this conversation as resolved.
Show resolved Hide resolved

return createRule({
message: ({ lang }) => msg ?? message[lang](length),
message: ({ lang }) => msg ?? message[lang](formatted),
optional: true,
validation: (value) => baseMaxLength(value, length)
})
Expand Down
8 changes: 5 additions & 3 deletions lib/validation/rules/maxValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { createRule } from '../Rule'
import { maxValue as baseMaxValue } from '../validators'

export const message = {
en: (max: number) => `The value must be less than or equal to ${max}.`,
ja: (max: number) => `この値は最大${max}です。`
en: (max: string) => `The value must be less than or equal to ${max}.`,
ja: (max: string) => `この値は最大${max}です。`
}

export function maxValue(max: number, msg?: string) {
const formatted = Intl.NumberFormat().format(max)

return createRule({
message: ({ lang }) => msg ?? message[lang](max),
message: ({ lang }) => msg ?? message[lang](formatted),
optional: true,
validation: (value) => baseMaxValue(value, max)
})
Expand Down
8 changes: 5 additions & 3 deletions lib/validation/rules/minLength.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { createRule } from '../Rule'
import { minLength as baseMinLength } from '../validators'

export const message = {
en: (min: number) => `The value must be greater than or equal to ${min} characters.`,
ja: (min: number) => `この値は最小${min}文字です。`
en: (min: string) => `The value must be greater than or equal to ${min} characters.`,
ja: (min: string) => `この値は最小${min}文字です。`
}

export function minLength(length: number, msg?: string) {
const formatted = Intl.NumberFormat().format(length)

return createRule({
message: ({ lang }) => msg ?? message[lang](length),
message: ({ lang }) => msg ?? message[lang](formatted),
optional: true,
validation: (value) => baseMinLength(value, length)
})
Expand Down
8 changes: 5 additions & 3 deletions lib/validation/rules/minValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { createRule } from '../Rule'
import { minValue as baseMinValue } from '../validators'

export const message = {
en: (min: number) => `The value must be greater than or equal to ${min}.`,
ja: (min: number) => `この値は最大${min}です。`
en: (min: string) => `The value must be greater than or equal to ${min}.`,
ja: (min: string) => `この値は最大${min}です。`
}

export function minValue(min: number, msg?: string) {
const formatted = Intl.NumberFormat().format(min)

return createRule({
message: ({ lang }) => msg ?? message[lang](min),
message: ({ lang }) => msg ?? message[lang](formatted),
optional: true,
validation: (value) => baseMinValue(value, min)
})
Expand Down
17 changes: 14 additions & 3 deletions stories/components/SInputTextarea.01_Playground.story.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
<script setup lang="ts">
import SInputTextarea from 'sefirot/components/SInputTextarea.vue'
import { ref } from 'vue'
import { useData } from 'sefirot/composables/Data'
import { useValidation } from 'sefirot/composables/Validation'
import { maxLength } from 'sefirot/validation/rules'

const title = 'Components / SInputTextarea / 01. Playground'
const docs = '/components/input-textarea'

const text = ref('')
const { data } = useData({
text: null as string | null
})

const { validation } = useValidation(data, {
text: {
maxLength: maxLength(1000)
}
})

function state() {
return {
Expand Down Expand Up @@ -86,7 +96,8 @@ function state() {
:rows="state.rows"
:auto-resize="state.autoResize"
:disabled="state.disabled"
v-model="text"
v-model="data.text"
:validation="validation.text"
/>
</Board>
</template>
Expand Down