-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/steps #11
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
Open
KamiSpo
wants to merge
8
commits into
develop
Choose a base branch
from
feature/steps
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/steps #11
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a1ffab9
Created basic Step and Steps structure.
KamiSpo 807af41
Added color and size customization to a Step component.
KamiSpo 9736ac6
Add conditional rendering besed on step state. Create Steps component…
KamiSpo 34ada0a
Added step dividers. Work in progress.
KamiSpo 3f56d33
Add step labels, better positioning, divide Step component into Step …
980f259
Set Separator top offset, remove separator width. Code cleanup.
3003442
merge
4df24d6
Fix merge issues
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,261 @@ | ||
import React, { HTMLAttributes, ReactNode, ReactElement, LabelHTMLAttributes } from 'react'; | ||
import classnames from 'classnames'; | ||
import styled from '@emotion/styled'; | ||
import { ThemeColorsKeys } from '../..'; | ||
|
||
// SINGLE STEP | ||
|
||
export type StepState = 'success' | 'error' | 'active' | 'pending'; | ||
|
||
export interface StepProps extends HTMLAttributes<HTMLDivElement> { | ||
state?: StepState; | ||
labelLayout?: LabelLayout; | ||
children?: ReactNode; | ||
} | ||
|
||
interface StepContentProps extends StepProps { | ||
size?: number; | ||
color?: ThemeColorsKeys; | ||
fontColor?: ThemeColorsKeys; | ||
children?: ReactNode; | ||
} | ||
|
||
export const StepContentBase = styled.div<StepContentProps>(props => { | ||
const { colors } = props.theme; | ||
const color = colors[props.color!]; | ||
const fontColor = colors[props.fontColor!]; | ||
|
||
return { | ||
display: 'flex', | ||
borderRadius: '100%', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
border: `1px solid ${color}`, | ||
backgroundColor: color, | ||
color: fontColor, | ||
width: props.size, | ||
height: props.size, | ||
}; | ||
}); | ||
|
||
const StepContentSuccess = styled(StepContentBase)(props => { | ||
const { colors } = props.theme; | ||
|
||
return { | ||
backgroundColor: colors.primary, | ||
borderColor: colors.primary, | ||
color: colors.white, | ||
}; | ||
}); | ||
|
||
const StepContentError = styled(StepContentBase)(props => { | ||
const { colors } = props.theme; | ||
|
||
return { | ||
backgroundColor: colors.error, | ||
borderColor: colors.error, | ||
color: colors.white, | ||
}; | ||
}); | ||
|
||
const StepContentActive = styled(StepContentBase)(props => { | ||
const { colors } = props.theme; | ||
|
||
return { | ||
backgroundColor: colors.white, | ||
borderColor: colors.primary, | ||
color: colors.primary, | ||
}; | ||
}); | ||
|
||
const StepContentPending = styled(StepContentBase)(props => { | ||
const { colors } = props.theme; | ||
|
||
return { | ||
backgroundColor: colors.white, | ||
borderColor: colors.fontDisabled, | ||
color: colors.fontDisabled, | ||
}; | ||
}); | ||
|
||
const StepVertical = styled.div<StepProps>(() => { | ||
return { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
margin: '10px', | ||
}; | ||
}); | ||
|
||
const StepHorizontal = styled.div<StepProps>(() => { | ||
return { | ||
display: 'flex', | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
margin: '10px', | ||
|
||
'.ck-step-content': { | ||
marginRight: 8, | ||
}, | ||
}; | ||
}); | ||
|
||
export const StepContent = (props: StepContentProps) => { | ||
const StepContent = | ||
props.state === 'success' | ||
? StepContentSuccess | ||
: props.state === 'error' | ||
? StepContentError | ||
: props.state === 'active' | ||
? StepContentActive | ||
: props.state === 'pending' | ||
? StepContentPending | ||
: StepContentBase; | ||
|
||
return ( | ||
<StepContent {...props} className={classnames(props.className, 'ck-step-content')}> | ||
{props.children} | ||
</StepContent> | ||
); | ||
}; | ||
|
||
export const Step = (props: StepProps) => { | ||
const StepWrapper = props.labelLayout === 'vertical' ? StepVertical : StepHorizontal; | ||
|
||
return <StepWrapper className={classnames(props.className, 'ck-step')}>{props.children}</StepWrapper>; | ||
}; | ||
|
||
// STEP LABEL | ||
|
||
export type LabelLayout = 'vertical' | 'horizontal'; | ||
|
||
export interface StepLabelProps extends LabelHTMLAttributes<HTMLLabelElement> { | ||
state?: StepState; | ||
children?: ReactNode; | ||
} | ||
|
||
const StepLabelWrapper = styled.label<StepLabelProps>(props => { | ||
const { theme, state } = props; | ||
const { fontSizes, fontWeights, lineHeights, colors } = theme; | ||
|
||
return { | ||
width: 'fit-content', | ||
whiteSpace: 'nowrap', | ||
fontSize: fontSizes.body2, | ||
lineHeight: lineHeights.body2, | ||
fontWeight: fontWeights.regular, | ||
color: colors[state === 'active' ? 'primary' : 'fontDisabled'], | ||
}; | ||
}); | ||
|
||
export const StepLabel = ({ children, ...props }: StepLabelProps) => { | ||
return ( | ||
<StepLabelWrapper {...props} className={classnames(props.className, 'ck-step-label')}> | ||
<span>{children}</span> | ||
</StepLabelWrapper> | ||
); | ||
}; | ||
|
||
// SEPARATOR | ||
|
||
export interface SeparatorProps extends StepContentProps { | ||
topOffset?: string; | ||
} | ||
|
||
const Separator = styled.div<SeparatorProps>(props => { | ||
const { colors } = props.theme; | ||
|
||
return { | ||
height: '1px', | ||
width: '100%', | ||
marginTop: props.topOffset, | ||
backgroundColor: colors.fontDisabled, | ||
}; | ||
}); | ||
|
||
// STEP GROUP | ||
|
||
export interface StepsProps extends HTMLAttributes<HTMLDivElement> { | ||
children?: ReactNode; | ||
activeStep?: number; | ||
labelLayout?: LabelLayout; | ||
} | ||
|
||
const StepsWrapper = styled.div<StepsProps>(() => { | ||
return { | ||
display: 'flex', | ||
justifyContent: 'space-between', | ||
}; | ||
}); | ||
|
||
const calculateSeparatorTopOffset = (StepWrapper: ReactElement) => { | ||
const stepContent = StepWrapper!.props.children[0]; | ||
return `${stepContent.props.size / 2 + 10}px`; | ||
}; | ||
|
||
export const Steps = ({ children, ...props }: StepsProps) => { | ||
const className = classnames(props.className, 'ck-steps'); | ||
|
||
// Determine child's state and pass number as a child (only if it's active or pending) | ||
const childrenWithState = React.Children.map(children as ReactElement[], (child, index) => { | ||
const state = index === props.activeStep ? 'active' : index > props.activeStep! ? 'pending' : undefined; | ||
|
||
if (index >= props.activeStep!) { | ||
let number = index + 1; | ||
let updatedChildren = React.Children.map(child.props.children as ReactElement[], (innerChild, index) => { | ||
if (index === 0) { | ||
return React.cloneElement(innerChild as ReactElement, { state: state, children: number }); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Skoro w if powyżej znajduje się return, to else nie jest konieczne. Wystarczy, że zostawisz samą jego zawartość. |
||
return React.cloneElement(innerChild as ReactElement, { state: state }); | ||
} | ||
}); | ||
return React.cloneElement(child, {}, updatedChildren); | ||
} | ||
|
||
return child; | ||
}); | ||
|
||
// Pass label layout to children | ||
const calcChildren = childrenWithState.length; | ||
const childrenWithLabels: ReactNode[] = []; | ||
const labelLayout = props.labelLayout; | ||
|
||
childrenWithState.forEach((element: ReactNode) => { | ||
childrenWithLabels.push(React.cloneElement(element as ReactElement, { labelLayout: labelLayout })); | ||
}); | ||
|
||
// Add separators between steps | ||
const topOffset = calculateSeparatorTopOffset(children![0]); | ||
|
||
const childrenWithSeparators: ReactNode[] = []; | ||
childrenWithLabels.forEach((element: ReactNode, index) => { | ||
childrenWithSeparators.push(element); | ||
if (index < calcChildren - 1) { | ||
childrenWithSeparators.push(<Separator topOffset={topOffset} />); | ||
} | ||
}); | ||
|
||
return ( | ||
<StepsWrapper {...props} className={className}> | ||
{childrenWithSeparators} | ||
</StepsWrapper> | ||
); | ||
}; | ||
|
||
StepContent.defaultProps = { | ||
fontColor: 'white', | ||
color: 'primary', | ||
size: 32, | ||
children: '', | ||
}; | ||
|
||
Step.defaultProps = { | ||
labelLeyout: 'vertical', | ||
}; | ||
|
||
Steps.defaultProps = { | ||
activeStep: 2, | ||
}; | ||
|
||
Step.Label = StepLabel; | ||
Step.Content = StepContent; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Steps'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React, { SVGProps } from 'react'; | ||
|
||
const SvgTimesSolid = (props: SVGProps<SVGSVGElement>) => ( | ||
<svg | ||
aria-hidden="true" | ||
data-prefix="fas" | ||
data-icon="times" | ||
className="svg-inline--fa fa-times fa-w-11" | ||
viewBox="0 0 352 512" | ||
{...props} | ||
> | ||
<path | ||
fill="currentColor" | ||
d="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z" | ||
/> | ||
</svg> | ||
); | ||
|
||
export default SvgTimesSolid; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tę logikę lepiej wyrzuć do jakiejś funkcji, to łatwiej będzie to czytać. Najlepiej zrobić switch