Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Megha-Dev-19 committed Mar 4, 2024
1 parent 68c3070 commit 6790ec2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
40 changes: 32 additions & 8 deletions src/devhub/components/molecule/Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ const TextInput = ({
style,
...otherProps
}) => {
State.init({
data: value,
});

useEffect(() => {
const handler = setTimeout(() => {
onChange({ target: { value: state.data } });
}, 30);

return () => {
clearTimeout(handler);
};
}, [state.data]);

useEffect(() => {
if (value !== state.data) {
State.update({ data: value });
}
}, [value]);

const typeAttribute =
type === "text" ||
type === "password" ||
Expand All @@ -22,21 +42,21 @@ const TextInput = ({
: "text";

const isValid = () => {
if (!value || value.length === 0) {
if (!state.data || state.data.length === 0) {
return !inputProps.required;
} else if (inputProps.min && inputProps.min > value?.length) {
} else if (inputProps.min && inputProps.min > state.data?.length) {
return false;
} else if (inputProps.max && inputProps.max < value?.length) {
} else if (inputProps.max && inputProps.max < state.data?.length) {
return false;
} else if (
inputProps.allowCommaAndSpace === false &&
/^[^,\s]*$/.test(value) === false
/^[^,\s]*$/.test(state.data) === false
) {
return false;
} else if (
inputProps.validUrl === true &&
/^(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$/.test(
value
state.data
) === false
) {
return false;
Expand Down Expand Up @@ -72,7 +92,7 @@ const TextInput = ({
<span
className={`d-inline-flex ${isValid() ? "text-muted" : "text-danger"}`}
style={{ fontSize: 12 }}
>{`${value?.length ?? 0} / ${inputProps.max}`}</span>
>{`${state.data?.length ?? 0} / ${inputProps.max}`}</span>
) : null,
].filter((label) => label !== null);

Expand Down Expand Up @@ -109,7 +129,9 @@ const TextInput = ({
)}
type={typeAttribute}
maxLength={inputProps.max}
{...{ onChange, placeholder, value, ...inputProps }}
value={state.data}
onChange={(e) => State.update({ data: e.target.value })}
{...{ placeholder, ...inputProps }}
/>
</div>
) : (
Expand All @@ -124,7 +146,9 @@ const TextInput = ({
style={{ resize: inputProps.resize ?? "vertical" }}
type={typeAttribute}
maxLength={inputProps.max}
{...{ onChange, placeholder, value, ...inputProps }}
value={state.data}
onChange={(e) => State.update({ data: e.target.value })}
{...{ placeholder, ...inputProps }}
/>
)}
</div>
Expand Down
23 changes: 19 additions & 4 deletions src/devhub/entity/proposal/AccountInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@ const placeholder = props.placeholder;
const onUpdate = props.onUpdate;

const [showAccountAutocomplete, setAutoComplete] = useState(false);
const [isValidAccount, setValidAccount] = useState(true);
const AutoComplete = styled.div`
max-width: 400px;
margin-top: 1rem;
`;

useEffect(() => {
const handler = setTimeout(() => {
const valid = value.length === 64 || value.includes(".near");
setValidAccount(valid);
setAutoComplete(!valid);
}, 100);

return () => {
clearTimeout(handler);
};
}, [value]);

return (
<div>
<Widget
Expand All @@ -17,18 +30,20 @@ return (
value: value,
onChange: (e) => {
onUpdate(e.target.value);
if (e.target.value.includes(".near")) {
return;
}
setAutoComplete(true);
},
skipPaddingGap: true,
placeholder: placeholder,
inputProps: {
max: 64,
prefix: "@",
},
}}
/>
{value && !isValidAccount && (
<div style={{ color: "red" }} className="text-sm">
Please enter valid account ID
</div>
)}
{showAccountAutocomplete && (
<AutoComplete>
<Widget
Expand Down
14 changes: 7 additions & 7 deletions src/devhub/entity/proposal/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ useEffect(() => {
const token = tokensOptions.find(
(item) => item.value === snapshot.requested_sponsorship_paid_in_currency
);
setRequestedSponsorshipToken(token ?? tokensOptions);
setRequestedSponsorshipToken(token ?? tokensOptions[2]);
}
setLoading(false);
}
Expand Down Expand Up @@ -418,7 +418,7 @@ const [isReviewModalOpen, setReviewModal] = useState(false);
const [amountError, setAmountError] = useState(null);
const [isCancelModalOpen, setCancelModal] = useState(false);

const DraftBtn = () => {
const SubmitBtn = () => {
const btnOptions = [
{
iconColor: "grey",
Expand Down Expand Up @@ -859,7 +859,7 @@ return (
}}
/>
</Link>
<DraftBtn />
<SubmitBtn />
</div>
</div>
</div>
Expand Down Expand Up @@ -957,11 +957,11 @@ return (
value: requestedSponsorshipAmount,
onChange: (e) => {
const inputValue = e.target.value;
// Check if the input value is a whole number
if (!Number.isInteger(Number(inputValue))) {
setAmountError("Please enter a whole number.");
const isValidInput = /^\d+$/.test(inputValue);
if (!isValidInput || Number(inputValue) < 0) {
setAmountError("Please enter a positive whole number.");
} else {
setRequestedSponsorshipAmount(e.target.value);
setRequestedSponsorshipAmount(inputValue);
setAmountError("");
}
},
Expand Down

0 comments on commit 6790ec2

Please sign in to comment.