From b80ab2b1c60061525be18b2fd08ed0264f05338f Mon Sep 17 00:00:00 2001 From: Anya Stasiuk Date: Wed, 7 Oct 2020 13:53:31 +0300 Subject: [PATCH] feat: make dropdown to switch between code samples --- src/common-elements/tabs.ts | 4 +- .../RequestSamples/RequestSamples.tsx | 110 ++++++++++++++---- .../RequestSamples/styled.elements.ts | 26 +++++ 3 files changed, 117 insertions(+), 23 deletions(-) create mode 100644 src/components/RequestSamples/styled.elements.ts diff --git a/src/common-elements/tabs.ts b/src/common-elements/tabs.ts index 1c4ef1b9..a5171dea 100644 --- a/src/common-elements/tabs.ts +++ b/src/common-elements/tabs.ts @@ -6,7 +6,7 @@ import styled from '../styled-components'; export { Tab, TabList, TabPanel } from 'react-tabs'; export const Tabs = styled(ReactTabs)` - > ul { + .react-tabs__tab-list { list-style: none; padding: 0; margin: 0; @@ -101,7 +101,7 @@ export const SmallTabs = styled(Tabs)` > .react-tabs__tab-panel { & > div, & > pre { - padding: ${props => props.theme.spacing.unit * 2}px 0; + padding: ${(props) => props.theme.spacing.unit * 2}px 0; } } `; diff --git a/src/components/RequestSamples/RequestSamples.tsx b/src/components/RequestSamples/RequestSamples.tsx index 89b12d74..6884a99b 100644 --- a/src/components/RequestSamples/RequestSamples.tsx +++ b/src/components/RequestSamples/RequestSamples.tsx @@ -1,52 +1,120 @@ import { observer } from 'mobx-react'; import * as React from 'react'; -import { isPayloadSample, OperationModel, RedocNormalizedOptions } from '../../services'; +import { + isPayloadSample, + OperationModel, + RedocNormalizedOptions, + XPayloadSample, +} from '../../services'; import { PayloadSamples } from '../PayloadSamples/PayloadSamples'; import { SourceCodeWithCopy } from '../SourceCode/SourceCode'; -import { RightPanelHeader, Tab, TabList, TabPanel, Tabs } from '../../common-elements'; +import { + DropdownOption, + RightPanelHeader, + Tab, + TabList, + TabPanel, + Tabs, +} from '../../common-elements'; import { OptionsContext } from '../OptionsProvider'; +import { SamplesDropdown, Flex } from './styled.elements'; export interface RequestSamplesProps { operation: OperationModel; } +interface RequestSamplesState { + codeSampleIdx: number; + tabIndex: number; +} + @observer -export class RequestSamples extends React.Component { +export class RequestSamples extends React.Component { static contextType = OptionsContext; context: RedocNormalizedOptions; operation: OperationModel; + state = { + codeSampleIdx: 0, + tabIndex: 0, + }; + + handleChangeLang = (option: DropdownOption, updatedTabIndex: number) => { + this.setState({ + codeSampleIdx: option.idx, + }); + if (this.state.tabIndex !== updatedTabIndex) { + this.setState({ + tabIndex: updatedTabIndex, + }); + } + }; + + handleChangeTab = (index: number) => { + this.setState({ + tabIndex: index, + }); + }; + render() { const { operation } = this.props; const samples = operation.codeSamples; const hasSamples = samples.length > 0; const hideTabList = samples.length === 1 ? this.context.hideSingleRequestSampleTab : false; + + const payloadSample = samples.find((sample) => isPayloadSample(sample)); + const codeSamples = samples.filter((sample) => !isPayloadSample(sample)); + const isCodeSamples = codeSamples.length > 0; + const options = isCodeSamples + ? codeSamples.map((codeSample, idx) => { + return { + idx, + value: codeSample.label || codeSample.lang, + }; + }) + : []; + return ( (hasSamples && (
Request samples - - - - {samples.map(sample => ( - - {isPayloadSample(sample) ? ( -
- -
- ) : ( - + + + + {isCodeSamples && ( + + this.handleChangeLang(option, payloadSample ? 1 : 0) + } + options={options} + /> + )} + + {payloadSample && ( + +
+ +
- ))} + )} + {isCodeSamples && ( + + + + )}
)) || diff --git a/src/components/RequestSamples/styled.elements.ts b/src/components/RequestSamples/styled.elements.ts new file mode 100644 index 00000000..b63870b1 --- /dev/null +++ b/src/components/RequestSamples/styled.elements.ts @@ -0,0 +1,26 @@ +import styled from '../../styled-components'; +import { InvertedSimpleDropdown } from '../PayloadSamples/styled.elements'; +import { darken } from 'polished'; + +export const Flex = styled.div` + display: flex; + justify-content: space-between; +`; + +export const SamplesDropdown = styled(InvertedSimpleDropdown)` + && { + background-color: ${({ theme }) => theme.codeBlock.backgroundColor}; + height: 33px; + padding: 6px 10px; + border: 1px solid ${({ theme }) => darken(0.05, theme.codeBlock.backgroundColor)}; + border-radius: 5px; + &:hover, + &:focus-within { + border: 1px solid ${({ theme }) => darken(0.05, theme.codeBlock.backgroundColor)}; + box-shadow: none; + } + &:focus-within { + background-color: ${({ theme }) => theme.codeBlock.backgroundColor}; + } + } +`;