Skip to content

Commit 6db32f7

Browse files
committed
fixed slugify logic considering dash and line only settings
1 parent 013637c commit 6db32f7

8 files changed

Lines changed: 56 additions & 22 deletions

File tree

build/entry.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,30 @@ const App = () => {
144144
</Control>
145145
</div>
146146
<PrettyCode code={`
147-
<Control label="Input Field">
147+
<Control label="Slug Field">
148148
<FieldSlug defaultValue="value 1" onUpdate={console.log} />
149149
</Control>
150150
`} />
151+
<div style={{ padding: '10px 0', marginTop: '40px' }}>
152+
<Control label="Slug Field (only dashes)">
153+
<FieldSlug defaultValue="value 2" dash onUpdate={console.log} />
154+
</Control>
155+
</div>
156+
<PrettyCode code={`
157+
<Control label="Slug Field (only dashes)">
158+
<FieldSlug defaultValue="value 2" dash onUpdate={console.log} />
159+
</Control>
160+
`} />
161+
<div style={{ padding: '10px 0', marginTop: '40px' }}>
162+
<Control label="Slug Field (only lines)">
163+
<FieldSlug defaultValue="value 3" line onUpdate={console.log} />
164+
</Control>
165+
</div>
166+
<PrettyCode code={`
167+
<Control label="Slug Field (only lines)">
168+
<FieldSlug defaultValue="value 3" line onUpdate={console.log} />
169+
</Control>
170+
`} />
151171
<div style={{ padding: '10px 0', marginTop: '40px' }}>
152172
<Control label="Single File Input">
153173
<FieldFile

docs/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frui",
3-
"version": "0.0.11",
3+
"version": "0.0.12",
44
"license": "MIT",
55
"description": "A collection of vanilla react and tailwind components written in typescript.",
66
"author": "Chris <chris@incept.asia>",

src/hooks/useFieldSlug.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
import type { ChangeEvent } from 'react';
22
import type { FieldSlugConfig } from '../types';
33

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';
175

186
export default function useFieldSlug(config: FieldSlugConfig) {
197
const value = config.value
20-
? slugify(String(config.value))
8+
? slugify(String(config.value), !config.dash, !config.line)
219
: undefined;
2210
const defaultValue = config.defaultValue
23-
? slugify(String(config.defaultValue))
11+
? slugify(String(config.defaultValue), !config.dash, !config.line)
2412
: undefined;
2513
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);
2715
config.onChange && config.onChange(e);
2816
};
2917
return { value, defaultValue, change };

src/react/FieldSlug.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import useFieldSlug from '../hooks/useFieldSlug';
55

66
const FieldSlug: React.FC<FieldSlugProps> = (props) => {
77
const {
8-
dash = true,
8+
dash,
9+
line,
910
value: rawValue,
1011
defaultValue: rawDefaultValue,
1112
onChange,
@@ -14,6 +15,7 @@ const FieldSlug: React.FC<FieldSlugProps> = (props) => {
1415

1516
const { value, defaultValue, change } = useFieldSlug({
1617
dash,
18+
line,
1719
value: rawValue,
1820
defaultValue: rawDefaultValue,
1921
onChange

src/tailwind/FieldSlug.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import useFieldSlug from '../hooks/useFieldSlug';
55

66
const FieldSlug: React.FC<FieldSlugProps> = (props) => {
77
const {
8-
dash = true,
8+
dash,
9+
line,
910
value: rawValue,
1011
defaultValue: rawDefaultValue,
1112
onChange,
@@ -14,6 +15,7 @@ const FieldSlug: React.FC<FieldSlugProps> = (props) => {
1415

1516
const { value, defaultValue, change } = useFieldSlug({
1617
dash,
18+
line,
1719
value: rawValue,
1820
defaultValue: rawDefaultValue,
1921
onChange

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,13 @@ export type FieldsProps<ValueType = any> = {
415415
//Field slug component
416416
export type FieldSlugConfig = FieldInputConfig & {
417417
dash?: boolean,
418+
line?: boolean
418419
value: string|number|readonly string[]|undefined,
419420
defaultValue?: string|number|readonly string[]|undefined
420421
};
421422
export type FieldSlugProps = FieldInputProps & {
422-
dash?: boolean
423+
dash?: boolean,
424+
line?: boolean
423425
};
424426

425427
// Field switch component

src/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,24 @@ export function makeStyles(
8787
: defaultStyles as React.CSSProperties|undefined;
8888

8989
return Object.keys(styles || {}).length > 0 ? styles : undefined;
90+
};
91+
92+
//function to convert a string to a slug
93+
export function slugify(str: string, noDash = false, noLine = false) {
94+
return str.trim()
95+
//replace special characters with dashes (or underscores)
96+
.replace(/[^a-zA-Z0-9\-_]/g, noLine ? '-': '_')
97+
//replace dashes with underscores (or dashes if allowed)
98+
.replace(/-/g, noLine ? '-': '_')
99+
//replace dashes with underscores (or dashes if allowed)
100+
.replace(/_/g, noDash ? '_': '-')
101+
//replace multiple dashes with a single dash
102+
.replace(/-{2,}/g, '-')
103+
//replace multiple underscores with a single underscore
104+
.replace(/_{2,}/g, '_')
105+
//trim dashes and underscores from the beginning and end of the string
106+
.replace(/^-+|-+$/g, '')
107+
.replace(/^_+|_+$/g, '')
108+
//convert to lowercase
109+
.toLowerCase();
90110
};

0 commit comments

Comments
 (0)