|
1 | 1 | import type { ChangeEvent } from 'react'; |
2 | 2 | import type { FieldSlugConfig } from '../types'; |
3 | 3 |
|
4 | | -//function to convert a string to a slug |
5 | | -const slugify = (str: string, allowDash = true) => { |
6 | | - return str.trim() |
7 | | - //replace spaces with dashes (or underscores) |
8 | | - .replace(/\s+/g, allowDash ? '-': '_') |
9 | | - //replace multiple dashes with a single dash |
10 | | - .replace(/-{2,}/g, '-') |
11 | | - //replace multiple underscores with a single underscore |
12 | | - .replace(/_{2,}/g, '_') |
13 | | - //replace anything that is not a letter, number, dash, or underscore |
14 | | - .replace(/[^a-zA-Z0-9-\s_]/g, '') |
15 | | - .toLowerCase(); |
16 | | -}; |
| 4 | +import { slugify } from '../utils'; |
17 | 5 |
|
18 | 6 | export default function useFieldSlug(config: FieldSlugConfig) { |
19 | 7 | const value = config.value |
20 | | - ? slugify(String(config.value)) |
| 8 | + ? slugify(String(config.value), !config.dash, !config.line) |
21 | 9 | : undefined; |
22 | 10 | const defaultValue = config.defaultValue |
23 | | - ? slugify(String(config.defaultValue)) |
| 11 | + ? slugify(String(config.defaultValue), !config.dash, !config.line) |
24 | 12 | : undefined; |
25 | 13 | const change = (e: ChangeEvent<HTMLInputElement>) => { |
26 | | - e.target.value = slugify(e.target.value, config.dash); |
| 14 | + e.target.value = slugify(e.target.value, !config.dash, !config.line); |
27 | 15 | config.onChange && config.onChange(e); |
28 | 16 | }; |
29 | 17 | return { value, defaultValue, change }; |
|
0 commit comments