-
Notifications
You must be signed in to change notification settings - Fork 2
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: add WidgetCheckBoxes #8
base: main
Are you sure you want to change the base?
Changes from 4 commits
50dd498
6558251
68f2748
1821c28
36337cf
f68ad26
24fa9c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React from 'react' | ||
import React, { useState } from 'react' | ||
import { FrIconClassName, RiIconClassName } from '@codegouvfr/react-dsfr' | ||
import Button from '@codegouvfr/react-dsfr/Button' | ||
import Tabs from '@codegouvfr/react-dsfr/Tabs' | ||
|
@@ -30,59 +30,69 @@ export default function ({ | |
items, | ||
canAdd, | ||
onAddClick, | ||
registry, | ||
}: ArrayFieldTemplateProps & { uiSchema?: UiSchemaDSFR }) { | ||
const [selectedTabId, setSelectedTabId] = useState('tab0') | ||
const tabLabel = uiSchema?.['ui:tabLabel'] ?? 'Element' | ||
const removeIcon = uiSchema?.['ui:removeIcon'] ?? 'fr-icon-delete-line' | ||
const addIcon = uiSchema?.['ui:addIcon'] ?? 'fr-icon-add-circle-line' | ||
console.log('items', items) | ||
|
||
// ensure no exception when last selected tab has been destroyed | ||
const selectedIndex = Math.min( | ||
items.length - 1, | ||
parseInt(selectedTabId.replace(/^tab(\d+)$/, '$1')), | ||
) | ||
|
||
const tabContent = | ||
(items.length && ( | ||
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. nitpick: j'avoue que je suis plutôt pour utiliser systématiquement l'opérateur ternaire pour éviter des petits bugs d'affichages non désirés ( 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. ok! |
||
<> | ||
<Button | ||
type="button" | ||
iconId={removeIcon} | ||
onClick={(e) => { | ||
items[selectedIndex].onDropIndexClick(selectedIndex)() | ||
}} | ||
size="small" | ||
priority="secondary" | ||
> | ||
Supprimer | ||
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. question: est-ce que l'on a envie de laisser la possibilité de customiser le label des buttons avec un 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. oui! |
||
</Button> | ||
{items[selectedIndex].children} | ||
</> | ||
)) || | ||
null | ||
|
||
const onTabChange = (id: string) => { | ||
if (id === 'add') { | ||
onAddClick() | ||
setSelectedTabId(`tab${items.length}`) | ||
return | ||
} | ||
setSelectedTabId(id) | ||
} | ||
|
||
return ( | ||
<div className="form-group field"> | ||
<div className="fr-input-group"> | ||
<label className="fr-label">{title}</label> | ||
<div className="fr-input-wrap"> | ||
<Tabs | ||
onTabChange={onTabChange} | ||
selectedTabId={selectedTabId} | ||
tabs={items | ||
.map((element) => ({ | ||
label: `${tabLabel} ${element.index + 1}`, | ||
content: ( | ||
<> | ||
<Button | ||
type="button" | ||
iconId={removeIcon} | ||
onClick={(e) => { | ||
console.log('io', element) | ||
element.onDropIndexClick(element.index) | ||
}} | ||
size="small" | ||
priority="secondary" | ||
> | ||
Supprimer | ||
</Button> | ||
{element.children} | ||
</> | ||
), | ||
tabId: `tab${element.index}`, | ||
})) | ||
.concat([ | ||
{ | ||
label: `Ajouter`, | ||
content: ( | ||
<> | ||
{canAdd && ( | ||
<Button | ||
type="button" | ||
iconId={addIcon} | ||
onClick={onAddClick} | ||
size="small" | ||
priority="secondary" | ||
> | ||
Ajouter | ||
</Button> | ||
)} | ||
</> | ||
), | ||
tabId: 'add', | ||
}, | ||
])} | ||
/> | ||
> | ||
{selectedTabId !== 'add' && tabContent} | ||
</Tabs> | ||
</div> | ||
</div> | ||
</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { | ||
enumOptionsDeselectValue, | ||
enumOptionsIsSelected, | ||
enumOptionsSelectValue, | ||
enumOptionsValueForIndex, | ||
FormContextType, | ||
RJSFSchema, | ||
StrictRJSFSchema, | ||
WidgetProps, | ||
} from '@rjsf/utils' | ||
import Checkbox from '@codegouvfr/react-dsfr/Checkbox' | ||
import LabelWithHelp from './LabelWithHelp' | ||
import { ChangeEvent, FocusEvent } from 'react' | ||
|
||
//export default function (props: WidgetProps) { | ||
revolunet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export default function CheckboxesWidget< | ||
T = any, | ||
S extends StrictRJSFSchema = RJSFSchema, | ||
F extends FormContextType = any, | ||
>({ | ||
id, | ||
disabled, | ||
options, | ||
value, | ||
autofocus, | ||
uiSchema, | ||
schema, | ||
readonly, | ||
required, | ||
onChange, | ||
onBlur, | ||
onFocus, | ||
}: WidgetProps<T, S, F>) { | ||
const { enumOptions, enumDisabled, inline, emptyValue } = options | ||
const checkboxesValues = Array.isArray(value) ? value : [value] | ||
|
||
const _onChange = | ||
(index: number) => | ||
({ target: { checked } }: ChangeEvent<HTMLInputElement>) => { | ||
if (checked) { | ||
onChange( | ||
enumOptionsSelectValue<S>(index, checkboxesValues, enumOptions), | ||
) | ||
} else { | ||
onChange( | ||
enumOptionsDeselectValue<S>(index, checkboxesValues, enumOptions), | ||
) | ||
} | ||
} | ||
|
||
const _onBlur = ({ target }: FocusEvent<HTMLInputElement>) => | ||
onBlur( | ||
id, | ||
enumOptionsValueForIndex<S>( | ||
target && target.value, | ||
enumOptions, | ||
emptyValue, | ||
), | ||
) | ||
const _onFocus = ({ target }: FocusEvent<HTMLInputElement>) => | ||
onFocus( | ||
id, | ||
enumOptionsValueForIndex<S>( | ||
target && target.value, | ||
enumOptions, | ||
emptyValue, | ||
), | ||
) | ||
|
||
return ( | ||
<div style={{ marginTop: '1rem', marginBottom: '-1rem' }}> | ||
<Checkbox | ||
options={ | ||
(Array.isArray(enumOptions) && | ||
enumOptions.map((option, index: number) => { | ||
const checked = enumOptionsIsSelected<S>( | ||
option.value, | ||
checkboxesValues, | ||
) | ||
const itemDisabled = | ||
Array.isArray(enumDisabled) && | ||
enumDisabled.indexOf(option.value) !== -1 | ||
|
||
return { | ||
label: ( | ||
<LabelWithHelp | ||
helpText={ | ||
uiSchema !== undefined ? uiSchema['ui:help'] : undefined | ||
} | ||
> | ||
{option.label + (required ? '*' : '')} | ||
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. question: est-ce que la gestion des champs requis ne devrait pas être directement gérée au dans le composant 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. will look |
||
</LabelWithHelp> | ||
), | ||
nativeInputProps: { | ||
checked, | ||
disabled: itemDisabled, | ||
onChange: _onChange(index), | ||
onBlur: _onBlur, | ||
onFocus: _onFocus, | ||
}, | ||
} | ||
})) || | ||
[] | ||
} | ||
/> | ||
</div> | ||
) | ||
} |
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.