Skip to content

Commit b351701

Browse files
fix(ModalInput): only check filename if requested (#259)
1 parent d175a12 commit b351701

File tree

5 files changed

+50
-19
lines changed

5 files changed

+50
-19
lines changed

src/components/ModalInput.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type ModalInputProps = {
3333
bottomSlot?: JSXElement
3434
footerSlot?: JSXElement
3535
onDrop?: (e: DragEvent, setValue: (value: string) => void) => void
36+
validateFilename?: boolean
3637
}
3738
export const ModalInput = (props: ModalInputProps) => {
3839
const [value, setValue] = createSignal(props.defaultValue ?? "")
@@ -78,18 +79,29 @@ export const ModalInput = (props: ModalInputProps) => {
7879
})
7980

8081
const submit = () => {
81-
const validation = validateFilename(value())
82-
if (!validation.valid) {
83-
notify.warning(t(`global.${validation.error}`))
84-
return
82+
if (props.validateFilename) {
83+
const validation = validateFilename(value())
84+
if (!validation.valid) {
85+
notify.warning(t(`global.${validation.error}`))
86+
return
87+
}
88+
} else {
89+
if (!value() || value().trim().length === 0) {
90+
notify.warning(t("global.empty_input"))
91+
return
92+
}
8593
}
8694
props.onSubmit?.(value())
8795
}
8896

8997
const handleInput = (newValue: string) => {
9098
setValue(newValue)
91-
const validation = validateFilename(newValue)
92-
setValidationError(validation.valid ? "" : validation.error || "")
99+
if (props.validateFilename) {
100+
const validation = validateFilename(newValue)
101+
setValidationError(validation.valid ? "" : validation.error || "")
102+
} else {
103+
setValidationError("")
104+
}
93105
}
94106

95107
return (

src/components/ModalTwoInput.tsx

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type ModalTwoInputProps = {
2525
loading?: boolean
2626
tips?: string
2727
topSlot?: JSXElement
28+
validateFilename?: boolean
2829
}
2930
export const ModalTwoInput = (props: ModalTwoInputProps) => {
3031
const [value1, setValue1] = createSignal(props.defaultValue1 ?? "") // Update value and setValue to value1 and setValue1
@@ -35,27 +36,42 @@ export const ModalTwoInput = (props: ModalTwoInputProps) => {
3536

3637
const handleInput1 = (newValue: string) => {
3738
setValue1(newValue)
38-
const validation = validateFilename(newValue)
39-
setValidationError1(validation.valid ? "" : validation.error || "")
39+
if (props.validateFilename) {
40+
const validation = validateFilename(newValue)
41+
setValidationError1(validation.valid ? "" : validation.error || "")
42+
} else {
43+
setValidationError1("")
44+
}
4045
}
4146

4247
const handleInput2 = (newValue: string) => {
4348
setValue2(newValue)
44-
const validation = validateFilename(newValue)
45-
setValidationError2(validation.valid ? "" : validation.error || "")
49+
if (props.validateFilename) {
50+
const validation = validateFilename(newValue)
51+
setValidationError2(validation.valid ? "" : validation.error || "")
52+
} else {
53+
setValidationError2("")
54+
}
4655
}
4756

4857
const submit = () => {
49-
const validation1 = validateFilename(value1())
50-
const validation2 = validateFilename(value2())
58+
if (props.validateFilename) {
59+
const validation1 = validateFilename(value1())
60+
const validation2 = validateFilename(value2())
5161

52-
if (!validation1.valid) {
53-
notify.warning(t(`global.${validation1.error}`))
54-
return
55-
}
56-
if (!validation2.valid) {
57-
notify.warning(t(`global.${validation2.error}`))
58-
return
62+
if (!validation1.valid) {
63+
notify.warning(t(`global.${validation1.error}`))
64+
return
65+
}
66+
if (!validation2.valid) {
67+
notify.warning(t(`global.${validation2.error}`))
68+
return
69+
}
70+
} else {
71+
if (!value1() || !value2()) {
72+
notify.warning(t("global.empty_input"))
73+
return
74+
}
5975
}
6076
props.onSubmit?.(value1(), value2()) // Update onSubmit to pass both input values
6177
}

src/pages/home/toolbar/Mkdir.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const Mkdir = () => {
2121
return (
2222
<ModalInput
2323
title="home.toolbar.input_dir_name"
24+
validateFilename={true}
2425
opened={isOpen()}
2526
onClose={onClose}
2627
loading={loading()}

src/pages/home/toolbar/NewFile.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const NewFile = () => {
2525
return (
2626
<ModalInput
2727
title="home.toolbar.input_filename"
28+
validateFilename={true}
2829
footerSlot={
2930
<Checkbox
3031
mr="auto"

src/pages/home/toolbar/Rename.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const Rename = () => {
3030
<Show when={isOpen()}>
3131
<ModalInput
3232
title="home.toolbar.input_new_name"
33+
validateFilename={true}
3334
footerSlot={
3435
<Checkbox
3536
mr="auto"

0 commit comments

Comments
 (0)