Skip to content

Commit

Permalink
feat(FormRow): add direction prop
Browse files Browse the repository at this point in the history
  • Loading branch information
mournfulCoroner committed Oct 3, 2023
1 parent 56dce92 commit e3eaade
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
31 changes: 24 additions & 7 deletions src/components/FormRow/FormRow.scss
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
@use '../variables';

.#{variables.$ns}form-row {
$block: '.#{variables.$ns}form-row';

#{$block} {
--gc-form-row-label-width: 172px;
--gc-form-row-field-height: 28px;

margin-bottom: 20px;
align-items: flex-start;
display: flex;
margin-bottom: 20px;

&_direction_row {
align-items: flex-start;
}

&_direction_column {
flex-direction: column;
}

&__left {
display: flex;
flex-flow: row;
min-height: var(--gc-form-row-field-height);
box-sizing: border-box;
flex-shrink: 0;
width: var(--gc-form-row-label-width);
padding-right: 8px;

#{$block}_direction_row & {
min-height: var(--gc-form-row-field-height);
flex-shrink: 0;
width: var(--gc-form-row-label-width);
padding-right: 8px;
}

#{$block}_direction_column & {
width: 100%;
margin-bottom: 10px;
}
}

&__field-name {
Expand Down
3 changes: 2 additions & 1 deletion src/components/FormRow/FormRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ const FormRowComponent: FC<FormRowProps> = ({
fieldId,
required = false,
children,
direction = 'row',
}) => {
const LabelComponent = fieldId ? 'label' : 'span';

return (
<div className={b(null, className)}>
<div className={b({direction}, className)}>
<div className={b('left')}>
<LabelComponent className={b('field-name')} htmlFor={fieldId ? fieldId : undefined}>
<span className={b('field-name-text')}>{label}</span>
Expand Down
6 changes: 6 additions & 0 deletions src/components/FormRow/__stories__/FormRow.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default {
label: 'Enter your name',
fieldId,
children: <TextInput id={fieldId} />,
direction: 'row',
},
argTypes: {
children: argTypeReactNode,
Expand Down Expand Up @@ -88,3 +89,8 @@ WithHelpPopoverAndLongLabel.args = {
...WithLongLabel.args,
...WithHelpPopover.args,
};

export const ColumnDirection = Template.bind({});
ColumnDirection.args = {
direction: 'column',
};
3 changes: 3 additions & 0 deletions src/components/FormRow/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {HTMLAttributes, PropsWithChildren, ReactNode} from 'react';

export type FormRowDirection = 'row' | 'column';
export interface FormRowProps {
className?: string;
/** Field label */
Expand All @@ -13,6 +14,8 @@ export interface FormRowProps {
/** Field component itself. `<FormRow.FieldDescription/>` could be used here
* next to field component itself */
children?: ReactNode;
/** Direction in which the elements are placed */
direction?: FormRowDirection;
}

export type FormRowFieldDescriptionProps = PropsWithChildren<{}> &
Expand Down

0 comments on commit e3eaade

Please sign in to comment.