Skip to content

Instantly share code, notes, and snippets.

@arthurborgesdev
Created March 16, 2020 20:34
Show Gist options
  • Save arthurborgesdev/27258a29b70d5e9631de81e9d876cbe7 to your computer and use it in GitHub Desktop.
Save arthurborgesdev/27258a29b70d5e9631de81e9d876cbe7 to your computer and use it in GitHub Desktop.
How to delete each Section without only the last one?
import React, { useState } from 'react';
import styled from 'styled-components';
import { Flex } from './Flex';
const Layout = styled.div`
margin: 0 auto;
`;
const Paragraph = styled.p`
margin-bottom: 5px;
margin-top: 5px;
`;
const FlightInput = () => (
<>
<input className="block-input"/>
<div style={{marginLeft: "4em"}}>
<Paragraph>Origem/Destino</Paragraph>
<input style={{height: "1.6em"}}/><span> - </span><input style={{height: "1.6em"}} />
<span style={{ display: "block" }}>Saída:</span>
<input></input>
<span> / Chegada: </span>
<input></input>
</div>
</>
);
const Section = ({sections, deleteSection}) => (
<>
{
sections.map((idx, _) => (
<div key={(idx + 1).toString()}>
<Flex container justifyContent="space-between">
<input className="block-input"/>
<button onClick={() => deleteSection(idx)}>Remover Trajeto</button>
{console.log(idx)}
</Flex>
<div style={{marginLeft: "4em"}}>
<Paragraph>Origem/Destino</Paragraph>
<input style={{height: "1.6em"}}/><span> - </span><input style={{height: "1.6em"}} />
<span style={{ display: "block" }}>Saída:</span>
<input></input>
<span> / Chegada: </span>
<input></input>
</div>
</div>
))
}
</>
);
const Cotacao = () => {
const [sections, setSection] = useState([]);
const addDepartureSection = () => {
let lastElement = sections.length + 1
setSection([...sections, ...[lastElement]]);
}
return (
<>
<Flex container flexDirection="column">
<Layout>
<img></img>
<div>
<img></img>
<div className="button-ida">
<span>IDA</span>
<button onClick={addDepartureSection}>Adcionar Trajeto</button>
</div>
<input className="block-input"/>
<FlightInput />
<Section
sections={sections}
deleteSection={sectionIdx => {
const newSections = sections.filter((_, idx) => idx !== sectionIdx);
console.log(`sections é ${sections}`);
console.log(`sectionIdx é ${sectionIdx}`);
console.log(newSections);
setSection(newSections);
}}
/>
</div>
<div>
<img></img>
<span style={{ display: "block" }}>VOLTA</span>
<input className="block-input"/>
<FlightInput />
<input className="block-input" />
</div>
<div style={{ marginTop: "40px"}}>
<span>VALOR: </span>
<input style={{height: "1.6em"}} />
</div>
<img></img>
</Layout>
</Flex>
</>
)
}
export default Cotacao;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment