Skip to content

Commit cd6e61e

Browse files
authored
Don't show Bucket Name input error until field is touched (#2316)
1 parent a77b56b commit cd6e61e

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

portal-ui/src/screens/Console/Buckets/ListBuckets/AddBucket/AddBucket.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ import { selDistSet, selSiteRep } from "../../../../../systemSlice";
4545
import {
4646
resetForm,
4747
setEnableObjectLocking,
48+
setIsDirty,
49+
setName,
4850
setQuota,
4951
setQuotaSize,
5052
setQuotaUnit,
@@ -119,6 +121,7 @@ const AddBucket = ({ classes }: IsetProps) => {
119121
"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(.|$)){4}$"
120122
);
121123
const bucketName = useSelector((state: AppState) => state.addBucket.name);
124+
const isDirty = useSelector((state: AppState) => state.addBucket.isDirty);
122125
const [validationResult, setValidationResult] = useState<boolean[]>([]);
123126
const errorList = validationResult.filter((v) => !v);
124127
const hasErrors = errorList.length > 0;
@@ -166,7 +169,7 @@ const AddBucket = ({ classes }: IsetProps) => {
166169

167170
useEffect(() => {
168171
const bucketNameErrors = [
169-
!(bucketName.length < 3 || bucketName.length > 63),
172+
!(isDirty && (bucketName.length < 3 || bucketName.length > 63)),
170173
validBucketCharacters.test(bucketName),
171174
!(
172175
bucketName.includes(".-") ||
@@ -180,9 +183,11 @@ const AddBucket = ({ classes }: IsetProps) => {
180183
];
181184
setValidationResult(bucketNameErrors);
182185
// eslint-disable-next-line react-hooks/exhaustive-deps
183-
}, [bucketName]);
186+
}, [bucketName, isDirty]);
184187

185188
useEffect(() => {
189+
dispatch(setName(""));
190+
dispatch(setIsDirty(false));
186191
const fetchRecords = () => {
187192
api
188193
.invoke("GET", `/api/v1/buckets`)
@@ -475,7 +480,12 @@ const AddBucket = ({ classes }: IsetProps) => {
475480
type="submit"
476481
variant="callAction"
477482
color="primary"
478-
disabled={addLoading || invalidFields.length > 0 || hasErrors}
483+
disabled={
484+
addLoading ||
485+
invalidFields.length > 0 ||
486+
!isDirty ||
487+
hasErrors
488+
}
479489
label={"Create Bucket"}
480490
/>
481491
</Grid>

portal-ui/src/screens/Console/Buckets/ListBuckets/AddBucket/AddBucketName.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
import React from "react";
18-
import { setName } from "./addBucketsSlice";
18+
import { setName, setIsDirty } from "./addBucketsSlice";
1919
import InputBoxWrapper from "../../../Common/FormComponents/InputBoxWrapper/InputBoxWrapper";
2020
import { useSelector } from "react-redux";
2121
import { AppState, useAppDispatch } from "../../../../../store";
@@ -28,8 +28,10 @@ const AddBucketName = ({ hasErrors }: { hasErrors: boolean }) => {
2828
<InputBoxWrapper
2929
id="bucket-name"
3030
name="bucket-name"
31-
error={hasErrors ? "Invalid bucket Name" : ""}
32-
autoFocus={true}
31+
error={hasErrors ? "Invalid bucket name" : ""}
32+
onFocus={() => {
33+
dispatch(setIsDirty(true));
34+
}}
3335
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
3436
dispatch(setName(event.target.value));
3537
}}

portal-ui/src/screens/Console/Buckets/ListBuckets/AddBucket/addBucketsSlice.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { addBucketAsync } from "./addBucketThunks";
1919

2020
export interface AddBucketState {
2121
loading: boolean;
22+
isDirty: boolean;
2223
invalidFields: string[];
2324
name: string;
2425
versioningEnabled: boolean;
@@ -36,7 +37,8 @@ export interface AddBucketState {
3637

3738
const initialState: AddBucketState = {
3839
loading: false,
39-
invalidFields: ["name"],
40+
isDirty: false,
41+
invalidFields: [],
4042
name: "",
4143
versioningEnabled: false,
4244
lockingEnabled: false,
@@ -55,6 +57,9 @@ export const addBucketsSlice = createSlice({
5557
name: "addBuckets",
5658
initialState,
5759
reducers: {
60+
setIsDirty: (state, action: PayloadAction<boolean>) => {
61+
state.isDirty = action.payload;
62+
},
5863
setName: (state, action: PayloadAction<string>) => {
5964
state.name = action.payload;
6065

@@ -177,6 +182,7 @@ export const addBucketsSlice = createSlice({
177182

178183
export const {
179184
setName,
185+
setIsDirty,
180186
setVersioning,
181187
setEnableObjectLocking,
182188
setQuota,

portal-ui/src/screens/Console/Common/FormComponents/InputBoxWrapper/InputBoxWrapper.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ interface InputBoxProps {
4141
classes: any;
4242
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
4343
onKeyPress?: (e: any) => void;
44+
onFocus?: () => void;
4445
value: string | boolean;
4546
id: string;
4647
name: string;
@@ -133,6 +134,7 @@ const InputBoxWrapper = ({
133134
classes,
134135
className = "",
135136
onKeyPress,
137+
onFocus,
136138
}: InputBoxProps) => {
137139
let inputProps: any = { "data-index": index, ...extraInputProps };
138140

@@ -198,6 +200,7 @@ const InputBoxWrapper = ({
198200
placeholder={placeholder}
199201
className={classes.inputRebase}
200202
onKeyPress={onKeyPress}
203+
onFocus={onFocus}
201204
/>
202205
{overlayIcon && (
203206
<div

0 commit comments

Comments
 (0)