Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validações feitas #9

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import JobForm from '../components/molecules/JobForm/index'
import JobForm from "../components/molecules/JobForm/index";

export default function Home() {
return (
<>
<JobForm></JobForm>
<JobForm/>
</>
)
);
}
139 changes: 77 additions & 62 deletions src/components/atoms/InputForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,69 +1,84 @@
import { Label } from './label';
import { ContainerInput , Field } from './style'
import React, { useState } from 'react'
import { useField } from "formik";
import { Label } from "./label";
import { ContainerInput, Field, StyledError } from "./style";

interface InputFormProps {
id: string;
name: string;
type?: string;
placeholder?: string;
label: string
width: string
height?: string
as?: string
options?: Array<string>
id: string;
name: string;
type?: string;
placeholder?: string;
label: string;
width: string;
height?: string;
as?: string;
options?: Array<string>;
minDate?: string;
maxDate?: string;
callback?: (value: string) => void;
}

export default function InputForm({
id,
name,
type,
placeholder,
label,
width,
height,
as,
options,
} : InputFormProps) {
const [selectedValue, setSelectedValue] = useState('');
id,
name,
type,
placeholder,
label,
width,
height,
as,
options,
minDate,
maxDate,
callback,
}: InputFormProps) {
const [field] = useField(name);

const handleSelectChange = (event: any) => {
setSelectedValue(event.target.value);
console.log(event.target.value);
};
const handleSelectChange = (event: any) => {
if (callback) {
callback(event.target.value);
}

return (
<ContainerInput>
<Label
htmlFor={id}
name={label}
/>
{as === 'select' ?
<Field
id={id}
name={name}
width={width}
height={height}
as={as}
onChange={handleSelectChange}
value={selectedValue}
>
{options?.map((op, index) => (
<option key={index} value={index > 0 ? op : ""}>
{op}
</option>
))}
</Field>
:
<Field
id={id}
name={name}
type={type}
placeholder={placeholder}
width={width}
height={height}
/>
}
</ContainerInput>
);
}
field.onChange(event);
};

return (
<ContainerInput>
<Label htmlFor={id} name={label} />
{as === "select" ? (
<>
<Field
id={id}
name={name}
width={width}
height={height}
as={as}
onChange={handleSelectChange}
value={field.value}
>
{options?.map((op, index) => (
<option key={index} value={index > 0 ? op : ""}>
{op}
Comment on lines +58 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ao fazer listas, é interessante utilizar outra opção ao invés de Index. Vou deixar um link que explica com mais detalhes sobre isso:
https://dev.to/shiv1998/why-not-to-use-index-as-key-in-react-lists-practical-example-3e66

</option>
))}
</Field>
<StyledError name={name} component="span" />
</>
) : (
<>
<Field
id={id}
name={name}
type={type}
placeholder={placeholder}
width={width}
height={height}
min={minDate}
max={maxDate}
/>

<StyledError name={name} component="span" />
</>
)}
</ContainerInput>
);
}
10 changes: 8 additions & 2 deletions src/components/atoms/InputForm/style.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Field as FieldComponent } from "formik";
import { ErrorMessage, Field as FieldComponent } from "formik";
import styled from "styled-components";

export const ContainerInput = styled.div`
Expand Down Expand Up @@ -33,7 +33,13 @@ export const StyledLabel = styled.label`
height: 15px;
`;

export const StyledError = styled(ErrorMessage)`
margin-left: 5px;
height: 15px;
color: #ff0000;
font-size: 12px;
`;
export const StyledLabelError = styled(StyledLabel)`
margin-left: 5px;
height: 15px;
`;
`;
Loading