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

11736 implement component for grid property setting #11851

Merged
merged 18 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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 frontend/language/src/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@
"policy_editor.verification_modal_close_button": "No, go back",
"policy_editor.verification_modal_heading": "Delete rule?",
"policy_editor.verification_modal_text": "Are you sure want to delete this rule?",
"popover.popover_button_helptext": "Click here for help text popover",
"popover.popover_button_help_text": "Click here for help text popover",
"popover.popover_open": "Popover open",
"preview.iframe_title": "Preview",
"process_editor-view_mode": "View mode",
Expand Down Expand Up @@ -1090,8 +1090,8 @@
"ux_editor.modal_properties_group_table_headers_error": "The group must containt atleast one header in the table view.",
"ux_editor.modal_properties_header": "Edit properties",
"ux_editor.modal_properties_header_helper": "Write or search for header",
"ux_editor.modal_properties_helptext": "Help text:",
"ux_editor.modal_properties_helptext_add": "Add help text",
"ux_editor.modal_properties_help_text": "Help text:",
"ux_editor.modal_properties_help_text_add": "Add help text",
"ux_editor.modal_properties_image_alt_text_label": "Alternative text",
"ux_editor.modal_properties_image_placement_center": "Center",
"ux_editor.modal_properties_image_placement_label": "Placement",
Expand Down
86 changes: 47 additions & 39 deletions frontend/language/src/nb.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
.sliderContainer {
position: relative;
--outline: 1px solid var(--fds-semantic-border-action-active);
--outline-offset: 1px;
--border-radius: var(--fds-border_radius-small);
--selected-square-colour: var(--fds-semantic-surface-action-second-default);
--unselected-square-colour: var(--fds-semantic-surface-action-second-no_fill-active);
--thumb-width: calc(100% / 12);
}

.sliderContainer.disabled {
opacity: var(--fds-opacity-disabled);
}

.range {
-webkit-appearance: none;
appearance: none;
aspect-ratio: 12;
border-radius: var(--border-radius);
box-sizing: border-box;
cursor: pointer;
margin: 0;
outline-offset: var(--outline-offset);
outline: var(--outline);
padding: 0;
width: 100%;
}

.range:disabled {
--thumb-background-colour: var(--fds-semantic-border-neutral-default);
cursor: not-allowed;
}

.range::-webkit-slider-runnable-track {
aspect-ratio: 12;
background: var(--background);
border-radius: var(--border-radius);
}

.range::-moz-range-track {
aspect-ratio: 12;
background: var(--background);
border-radius: var(--border-radius);
}

.range::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: var(--thumb-width);
}

.range::-moz-range-thumb {
width: var(--thumb-width);
}

datalist {
align-items: center;
aspect-ratio: 12;
border-radius: var(--border-radius);
color: white;
display: flex;
justify-content: space-around;
pointer-events: none;
position: absolute;
top: 0;
width: 100%;
}

.option {
display: flex;
width: 100%;
text-align: center;
justify-content: center;
height: 100%;
align-items: center;
}

.option:first-child {
border-top-left-radius: var(--border-radius);
border-bottom-left-radius: var(--border-radius);
}

.option:last-child {
border-top-right-radius: var(--border-radius);
border-bottom-right-radius: var(--border-radius);
}

.option.outside {
color: var(--fds-semantic-surface-action-second-default);
}

.option.inside {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import { StudioGridSelector } from './StudioGridSelector';

describe('StudioGridSelector', () => {
it('should render slider with value 12 and it is enabled by default', () => {
render(<StudioGridSelector handleSliderChange={() => jest.fn()} />);

const slider = screen.getByRole('slider');
expect(slider).toHaveValue('12');
expect(slider).not.toBeDisabled();
});

it('should render slider as disabled when disabled is true', () => {
render(<StudioGridSelector disabled={true} handleSliderChange={() => jest.fn()} />);

const slider = screen.getByRole('slider');
expect(slider).toBeDisabled();
});

it('should render slider with correct value', () => {
const sliderValue = 4;
render(<StudioGridSelector sliderValue={sliderValue} handleSliderChange={() => jest.fn()} />);

const slider = screen.getByRole('slider');
expect(slider).toHaveValue(sliderValue.toString());
});

it('should call onSliderChange when new value is clicked on slider', () => {
const sliderValue = 4;
const newSliderValue = 6;
const onSliderChange = jest.fn();
render(<StudioGridSelector sliderValue={sliderValue} handleSliderChange={onSliderChange} />);

const slider = screen.getByRole('slider');
fireEvent.change(slider, { target: { value: newSliderValue } });
standeren marked this conversation as resolved.
Show resolved Hide resolved

expect(onSliderChange).toHaveBeenCalledWith(newSliderValue);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import React, { ChangeEvent, useEffect, useState } from 'react';
import classes from './StudioGridSelector.module.css';
import cn from 'classnames';
import { GridSize } from './types/GridSize';

type StudioGridSelectorProps = {
disabled?: boolean;
sliderValue?: GridSize;
handleSliderChange: (newValue: GridSize) => void;
};

/**
* @component
* A component designed for choosing a value within the range of 1 to 12
*/
export const StudioGridSelector = ({
disabled = false,
sliderValue = 12,
handleSliderChange,
}: StudioGridSelectorProps) => {
const [value, setValue] = useState<number>(sliderValue);
const gridValues: GridSize[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
useEffect(() => {
setValue(sliderValue);
}, [sliderValue]);

const optionClassName = (gridValue: number) =>
cn(classes.option, gridValue > value ? classes.outside : classes.inside);

const backgroundCss = 'linear-gradient(\n' + generateLinearGradient(value) + ')';

return (
<div
className={cn(classes.sliderContainer, disabled && classes.disabled)}
style={{ '--background': backgroundCss } as React.CSSProperties}
>
<input
className={classes.range}
type='range'
min='1'
max='12'
id='range'
value={sliderValue}
list='gridValues'
onChange={(event: ChangeEvent<HTMLInputElement>) =>
handleSliderChange(convertToGridSize(event.target.value))
}
onInput={(event: ChangeEvent<HTMLInputElement>) => setValue(parseInt(event.target.value))}

Check warning on line 48 in frontend/libs/studio-components/src/components/StudioGridSelector/StudioGridSelector.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/libs/studio-components/src/components/StudioGridSelector/StudioGridSelector.tsx#L48

Added line #L48 was not covered by tests
disabled={disabled}
/>
<datalist id='gridValues'>
{gridValues.map((gridValue) => (
<option
key={gridValue}
value={gridValue}
label={gridValue.toString()}
className={optionClassName(gridValue)}
/>
))}
</datalist>
</div>
);
};

const generateLinearGradient = (gridValue: number): string => {
const gradientLines: string[] = ['to right'];
const gap = '2px';
const insideColour = 'var(--selected-square-colour)';
const outsideColour = 'var(--unselected-square-colour)';
const gapColour = 'white';
const totalBgWidth = `(100% + ${gap})`;

const createStep = (option: number) => {
const startSquarePosition = `calc(${totalBgWidth} * ${option - 1} / 12)`;
const endSquarePosition = `calc(${totalBgWidth} * ${option} / 12 - ${gap})`;
const endGapPosition = `calc(${totalBgWidth} * ${option} / 12)`;
const squareColour = option <= gridValue ? insideColour : outsideColour;
const startSquareLine = `${squareColour} ${startSquarePosition}`;
const endSquareLine = `${squareColour} ${endSquarePosition}`;
const startGapLine = `${gapColour} ${endSquarePosition}`;
const endGapLine = `${gapColour} ${endGapPosition}`;
return [startSquareLine, endSquareLine, startGapLine, endGapLine].join(',\n');
};

for (let i = 1; i <= 12; i++) {
gradientLines.push(createStep(i));
}

return gradientLines.join(',\n');
};

const convertToGridSize = (value: string): GridSize => {
const int = parseInt(value);
return int as GridSize;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { StudioGridSelector } from './StudioGridSelector';
export type { GridSize } from './types/GridSize';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type GridSize = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
1 change: 1 addition & 0 deletions frontend/libs/studio-components/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './StudioModal';
export * from './StudioNumberInput';
export * from './StudioGridSelector';
export * from './StudioSpinner';
export * from './StudioPageSpinner';
export * from './StudioCenter';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const EditFormComponent = ({
onChange={toggleShowBetaFunc}
propertyPath={component.propertyPath}
componentType={component.type}
helpText={t('ux_editor.edit_component.show_beta_func_helptext')}
helpText={t('ux_editor.edit_component.show_beta_func_help_text')}
renderField={({ fieldProps }) => (
<Switch {...fieldProps} checked={fieldProps.value} size='small'>
{t('ux_editor.edit_component.show_beta_func')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('FormComponentConfig', () => {
});

[
'grid',
'readOnly',
'required',
'hidden',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { useText } from '../../hooks';
import { getComponentPropertyLabel } from '../../utils/language';
import { getUnsupportedPropertyTypes } from '../../utils/component';
import { EditGrid } from './editModal/EditGrid';

export interface IEditFormComponentProps {
editFormId: string;
Expand Down Expand Up @@ -48,11 +49,15 @@
hasCustomFileEndings,
validFileEndings,
children,
grid,
...rest
} = schema.properties;

// children property is not supported in component config - it should be part of container config.
const unsupportedPropertyKeys: string[] = getUnsupportedPropertyTypes(rest, children ? ['children'] : undefined);
const unsupportedPropertyKeys: string[] = getUnsupportedPropertyTypes(
rest,
children ? ['children'] : undefined,
);
return (
<>
{id && (
Expand Down Expand Up @@ -98,9 +103,19 @@
})}
</>
)}
{!hideUnsupported && <Heading level={3} size='xxsmall'>
{'Andre innstillinger'}
</Heading>}
{grid && (
<div>
<Heading level={3} size='xxsmall'>
{t('ux_editor.component_properties.grid')}
</Heading>
<EditGrid component={component} handleComponentChange={handleComponentUpdate} />
</div>
)}
{!hideUnsupported && (
<Heading level={3} size='xxsmall'>
{'Andre innstillinger'}
</Heading>
)}
{options && optionsId && (
<EditOptions
component={component as any}
Expand Down Expand Up @@ -153,6 +168,7 @@
handleComponentChange={handleComponentUpdate}
/>
)}

{Object.keys(rest).map((propertyKey) => {
if (!rest[propertyKey]) return null;
if (
Expand Down Expand Up @@ -214,19 +230,19 @@
{rest[propertyKey]?.description && (
<Paragraph size='small'>{rest[propertyKey].description}</Paragraph>
)}
<FormComponentConfig
key={propertyKey}
schema={rest[propertyKey]}
component={component[propertyKey] || {}}
handleComponentUpdate={(updatedComponent: FormComponent) => {
handleComponentUpdate({
...component,
[propertyKey]: updatedComponent,
});
}}
editFormId={editFormId}
hideUnsupported
/>
<FormComponentConfig
key={propertyKey}
schema={rest[propertyKey]}
component={component[propertyKey] || {}}
handleComponentUpdate={(updatedComponent: FormComponent) => {
handleComponentUpdate({

Check warning on line 238 in frontend/packages/ux-editor/src/components/config/FormComponentConfig.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/packages/ux-editor/src/components/config/FormComponentConfig.tsx#L238

Added line #L238 was not covered by tests
...component,
[propertyKey]: updatedComponent,
});
}}
editFormId={editFormId}
hideUnsupported
/>
</Accordion.Content>
</Accordion.Item>
</Accordion>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.gridContainer {
background-color: white;
border-radius: var(--fds-border_radius-large);
}

.tabs {
overflow: scroll;
--scroll-bar-color: white;
}

.tabs:hover {
overflow: scroll;
--scroll-bar-color: lightgray;
}

.tabs::-webkit-scrollbar-thumb {
background-color: var(--scroll-bar-color);
}
Loading
Loading