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

feat: handle question component css #287

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions src/ui/components/orchestrator/Orchestrator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ const useStyles = tss.create(({ theme }) => ({
backgroundColor: theme.palette.background.default,
paddingTop: '60px',
flex: 1,
width: 'calc(100% - 60px)',
},
mainContainer: {
flex: 1,
Expand All @@ -250,9 +251,9 @@ const useStyles = tss.create(({ theme }) => ({
height: '100%',
},
'& > div:first-of-type > div': {
width: '80%',
maxWidth: 'calc(100% - 100px)',
marginLeft: '100px',
marginTop: '3em',
marginTop: '4em',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you need to increase it ?

Copy link
Contributor Author

@chloe-renaud chloe-renaud Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed a marginTop: 1em that was on every .field-container and should have been called only on the first one. It should not change anything except remove a weird top margin in table cells and it's way easier to handle things in Question components since they use more .field-container.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Just we globally lose some space at the top of the "main part" compared with before

flexGrow: 1,
},
},
Expand All @@ -263,7 +264,10 @@ const useStyles = tss.create(({ theme }) => ({
minHeight: '2.3em',
},
navBarContainer: {
position: 'relative',
backgroundColor: theme.palette.background.default,
height: 'calc(100vh - 65px - 2em)',
right: '0',
position: 'fixed',
justifyContent: 'flex-end',
gap: '2em',
paddingBottom: '2em',
Expand Down
142 changes: 121 additions & 21 deletions src/ui/components/orchestrator/lunaticStyle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export const useLunaticStyles = tss.create(({ theme }) => ({
padding: '0.2em 1em',
},
},
'&.Question .lunatic-table .lunatic-table-td > .field-container': {
marginTop: '1em',
},

'&.Loop': {
display: 'block',
Expand Down Expand Up @@ -110,42 +113,61 @@ export const useLunaticStyles = tss.create(({ theme }) => ({
},

// declarations
'& .declarations-lunatic': {
padding: '0.5em',
fontSize: '92%',
},
'& .declarations-lunatic, &:is(.Sequence, .Subsequence) .label-description':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to check after we change the generation by Eno : there should not be any description in sequence/subSequence, only declarations as for other components

{
padding: '0.5em',
fontSize: '92%',
},
'& .declaration-lunatic': {
marginBottom: '1em',
},
'&:not(.Question) .declaration-lunatic': {
'&.declaration-help': {
color: theme.palette.declarations.help,
},
'&.declaration-instruction': {
color: theme.palette.declarations.instruction,
},
'&.declaration-statement': {
'&.declaration-instruction, &.declaration-statement, &.declaration-codecard':
{
color: theme.palette.declarations.instruction,
},
},
'&.Question > .declarations-lunatic': {
'.declaration-help': {
color: theme.palette.declarations.instruction,
},
'&.declaration-codecard': {
color: theme.palette.declarations.instruction,
},
'&.Question > .field-container .declarations-lunatic': {
'.declaration-help': {
color: theme.palette.declarations.help,
},
'.declaration-instruction, .declaration-statement, .declaration-codecard':
{
color: theme.palette.declarations.instruction,
},
},
'&.Subsequence > .field-container > .field > .label-description': {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, here we should have declarations only

color: theme.palette.declarations.instruction,
},

'& .label-top label': {
fontWeight: 'bold',
},

'& .lunatic-component fieldset legend, fieldset legend': {
fontWeight: 'bold',
maxWidth: '90%',
color: 'initial',
backgroundColor: 'initial',
fontSize: 'initial',
marginBottom: '1.5em',
lineHeight: '1.3em',
},
'& .lunatic-component .field-container, .field-container': {
'&:not(.Loop) .lunatic-component fieldset legend, &:not(.Question, .Loop) fieldset legend':
{
fontWeight: 'bold',
maxWidth: '90%',
color: 'initial',
backgroundColor: 'initial',
fontSize: 'initial',
marginBottom: '1.5em',
lineHeight: '1.3em',
},
'&:not(.Question) .lunatic-component .field-container': {
marginTop: '1em',
},
'&.Question .lunatic-component .field-container': {
marginTop: '0',
},
'& .lunatic-textarea textarea': {
padding: '0.5em',
fontSize: '100%',
Expand Down Expand Up @@ -186,14 +208,22 @@ export const useLunaticStyles = tss.create(({ theme }) => ({
borderRadius: '15px',
},
},
'&.Question .lunatic-table fieldset:is(.lunatic-radio-group, .lunatic-checkbox-one) .code-modality':
{
borderRadius: '5px',
},
'&.Question fieldset:is(.lunatic-radio-group, .lunatic-checkbox-one) .code-modality':
{
borderRadius: '15px',
},

'& .lunatic-component fieldset, fieldset': {
padding: 0,
margin: 0,
border: 'none',

// checkbox & radio
'& .lunatic-input-checkbox, & .lunatic-input-checkbox': {
'&:not(:has(.lunatic-checkbox-boolean)) .lunatic-input-checkbox': {
borderRadius: '5px',
border: `1px solid ${borderColorCheckbox}`,
backgroundColor: `${backgroundColorCheckbox}`,
Expand Down Expand Up @@ -287,11 +317,59 @@ export const useLunaticStyles = tss.create(({ theme }) => ({
padding: '0.5em',
},
},
'&:is(.Question, .Loop) .field-container > .field > fieldset > legend': {
'> h3': {
backgroundColor: 'transparent',
fontSize: '1em',
color: 'black',
display: 'block',
marginBottom: '1em',
fontWeight: 'bold',
padding: '0.5em',
marginTop: '0',
},
'> p:not(:has(*))': {
display: 'none',
},
},
'&.Loop .field-container > .field > fieldset > legend': {
'> h3': { marginBottom: '0' },
},

// missing response buttons css override
// roll-back some changes when Missing override is available in lunatic-v2
// such as shortcut and checked selectors

'&.Question, &.Loop': {
'&:has(> .declarations-lunatic)': {
display: 'grid',
gridTemplateRows: 'auto 1fr',
gridRowGap: '1em',
},
'.field-container, .field': {
height: '100%',
},
'.field fieldset': {
height: '100%',
display: 'grid',
gridTemplateRows: 'auto auto 1fr',
legend: {
gridRow: '1',
'&:not(:has(*))': {
display: 'none',
},
},
'.declarations-lunatic': {
gridRow: '2',
},
'.lunatic.lunatic-component': {
gridRow: '3',
display: 'grid',
gridTemplateRows: 'auto 1fr',
},
},
},

'& .lunatic-component .missing-buttons, .missing-buttons': {
display: 'flex',
gap: '1em',
Expand Down Expand Up @@ -512,5 +590,27 @@ export const useLunaticStyles = tss.create(({ theme }) => ({
},
},
},
'&.Question .lunatic-combo-box-container:has(.lunatic-combo-box-fab)': {
display: 'grid',
gridTemplateColumns: '70% auto',
gridTemplateRows: 'auto auto',
columnGap: '0.5em',
'.lunatic-combo-box': {
gridColumn: 1,
'.lunatic-combo-box-content': {
width: 'unset',
},
},
'.lunatic-combo-box-fab': {
gridColumn: 2,
position: 'unset',
left: 'unset',
alignSelf: 'center',
},
'.lunatic-errors': {
gridRow: 2,
gridColumn: 1 / 3,
},
},
},
}))
71 changes: 71 additions & 0 deletions src/ui/components/orchestrator/tools/functions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import type { LunaticComponentProps } from '@inseefr/lunatic/components/type'
import { describe, expect, it, vi } from 'vitest'

import { shouldAutoNext } from './functions'

describe('Should auto next', () => {
it('returns false with non-radio and non-checkbox components', () => {
const components: LunaticComponentProps[] = [
{
componentType: 'Text',
id: 'a',
value: 'v',
handleChanges: vi.fn(),
missingResponse: { name: 'a_MISSING' },
},
]
const valueChange = [{ name: 'a', value: 'v2' }]
expect(shouldAutoNext(components, valueChange)).toBeFalsy()
})
it('returns true with radio components', () => {
const components: LunaticComponentProps[] = [
{
componentType: 'Radio',
id: 'a',
value: '',
options: [],
handleChanges: vi.fn(),
response: { name: 'a' },
missingResponse: { name: 'a_MISSING' },
},
]
const valueChange = [{ name: 'a', value: 'v2' }]
expect(shouldAutoNext(components, valueChange)).toBeTruthy()
})
it('returns true with missing value', () => {
const components: LunaticComponentProps[] = [
{
componentType: 'Radio',
id: 'a',
value: '',
options: [],
handleChanges: vi.fn(),
response: { name: 'a' },
missingResponse: { name: 'a_MISSING' },
},
]
const valueChange = [{ name: 'a_MISSING', value: 'DK' }]
expect(shouldAutoNext(components, valueChange)).toBeTruthy()
})
it('returns true with a radio in a question component', () => {
const components: LunaticComponentProps[] = [
{
componentType: 'Question',
id: 'q',
components: [
{
componentType: 'Radio',
id: 'a',
value: '',
options: [],
handleChanges: vi.fn(),
response: { name: 'a' },
missingResponse: { name: 'a_MISSING' },
},
],
},
]
const valueChange = [{ name: 'a', value: 'v2' }]
expect(shouldAutoNext(components, valueChange)).toBeTruthy()
})
})
42 changes: 41 additions & 1 deletion src/ui/components/orchestrator/tools/functions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,52 @@ function countMissingResponseInComponent(component: Component): number {
/**
* temporary : should be handle by Lunatic
*/
export function countMissingResponseInPage(components: Components) {
function countMissingResponseInPage(components: Components) {
return components.reduce((total, component) => {
return total + countMissingResponseInComponent(component)
}, 0)
}

/**
* Whether or not we should go on next page on a value has changed.
*
* It is true if we have only one component that is a radio or checkbox,
* or if the response is a "don't know" / "refusal".
*/
export function shouldAutoNext(
components: Components,
valueChange: {
name: string
value: any
iteration?: number[]
}[],
): boolean {
const firstComponent = components[0]
// If it's a question we need to look at its components instead
if (firstComponent.componentType === 'Question') {
return shouldAutoNext(firstComponent.components, valueChange)
}
// at least one missing value has been selected
const hasMissingValue = valueChange.some(
(variable) =>
variable.name.includes('_MISSING') &&
['DK', 'RF'].includes(variable.value),
)
if (
// There is only one "don't know / refusal" variable on the page
countMissingResponseInPage(components) === 1 &&
// One of the values changed is a "don't know / refusal" response, or the current component is a radio or checkbox
(hasMissingValue ||
(firstComponent.componentType &&
['Radio', 'CheckboxBoolean', 'CheckboxOne'].includes(
firstComponent.componentType,
)))
) {
return true
}
return false
}

// check if the first subPage of an iteration is before lastReachedPage
export function isIterationReachable(
currentPage: number,
Expand Down
20 changes: 2 additions & 18 deletions src/ui/components/orchestrator/tools/useAutoNext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useLunatic } from '@inseefr/lunatic'

import { useCallback, useRef } from 'react'

import { countMissingResponseInPage } from './functions'
import { shouldAutoNext } from './functions'

type PartialLunatic = Pick<
ReturnType<typeof useLunatic>,
Expand All @@ -26,23 +26,7 @@ export function useAutoNext() {
if (ref.current === null) return
const { getComponents, goNextPage } = ref.current
const components = getComponents()
const firstComponent = components[0]
// at least one missing value has been selected
const hasMissingValue = valueChange.some(
(variable) =>
variable.name.includes('_MISSING') &&
['DK', 'RF'].includes(variable.value),
)
if (
// There is only one "don't know / refusal" variable on the page
countMissingResponseInPage(components) === 1 &&
// One of the values changed is a "don't know / refusal" response, or the current component is a radio or checkbox
(hasMissingValue ||
(firstComponent.componentType &&
['Radio', 'CheckboxBoolean', 'CheckboxOne'].includes(
firstComponent.componentType,
)))
) {
if (shouldAutoNext(components, valueChange)) {
goNextPage()
}
},
Expand Down
Loading