Skip to content

Commit

Permalink
ENH Add info message for readonly fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Jan 22, 2024
1 parent 19bd4a4 commit 1994149
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 33 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

39 changes: 23 additions & 16 deletions client/src/components/LinkField/LinkField.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const section = 'SilverStripe\\LinkField\\Controllers\\LinkFieldController';
* actions - object of redux actions
* isMulti - whether this field handles multiple links or not
* canCreate - whether this field can create new links or not
* readonly - whether this field is readonly or not
* ownerID - ID of the owner DataObject
* ownerClass - class name of the owner DataObject
* ownerRelation - name of the relation on the owner DataObject
Expand Down Expand Up @@ -199,6 +200,27 @@ const LinkField = ({
return links;
};

const sortableLinks = () => {
if (isMulti && !readonly) {
return <div className={linksClassName}>
<DndContext modifiers={[restrictToVerticalAxis, restrictToParentElement]}
sensors={sensors}
collisionDetection={closestCenter}
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
>
<SortableContext
items={linkIDs}
strategy={verticalListSortingStrategy}
>
{links}
</SortableContext>
</DndContext>
</div>
}
return <div>{links}</div>
};

const handleDragStart = (event) => {
setLinksClassName(classnames({
'link-picker__links': true,
Expand Down Expand Up @@ -259,22 +281,7 @@ const LinkField = ({
canCreate={canCreate}
readonly={readonly}
/> }
{ isMulti && <div className={linksClassName}>
<DndContext modifiers={[restrictToVerticalAxis, restrictToParentElement]}
sensors={sensors}
collisionDetection={closestCenter}
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
>
<SortableContext
items={linkIDs}
strategy={verticalListSortingStrategy}
>
{links}
</SortableContext>
</DndContext>
</div> }
{ !isMulti && <div>{links}</div>}
{sortableLinks()}
{ renderModal && <LinkModalContainer
types={types}
typeKey={data[editingID]?.typeKey}
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/LinkPicker/LinkPickerTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const LinkPickerTitle = ({
{...attributes}
{...listeners}
>
{ isMulti && <div className="link-picker__drag-handle"><i className="font-icon-drag-handle"></i></div> }
{ (isMulti && !readonly) && <div className="link-picker__drag-handle"><i className="font-icon-drag-handle"></i></div> }
<Button disabled={loading} className={`link-picker__button ${typeIcon}`} color="secondary" onClick={stopPropagation(onClick)}>
<div className="link-picker__link-detail">
<div className="link-picker__title">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ test('LinkPickerTitle main button should not fire the onClick callback while loa
expect(mockOnClick).toHaveBeenCalledTimes(0);
});

test('LinkPickerTitle render() should have readonly class if cannot edit', () => {
test('LinkPickerTitle render() should have readonly class if set to readonly', () => {
const { container } = render(<LinkFieldContext.Provider value={{ loading: false }}>
<LinkPickerTitle {...makeProps({ readonly: true })} />
</LinkFieldContext.Provider>);
expect(container.querySelectorAll('.link-picker__link--readonly')).toHaveLength(1);
});

test('LinkPickerTitle render() should not have readonly class if can edit', () => {
test('LinkPickerTitle render() should not have readonly class if set to readonly', () => {
const { container } = render(<LinkFieldContext.Provider value={{ loading: false }}>
<LinkPickerTitle {...makeProps({ readonly: false })} />
</LinkFieldContext.Provider>);
Expand Down
1 change: 1 addition & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ en:
INVALID_TOKEN: 'Invalid CSRF token'
INVALID_TYPEKEY: 'Invalid typeKey'
MENUTITLE: SilverStripe\LinkField\Controllers\LinkFieldController
READ_ONLY_MESSAGE: 'This form is read only'
UNAUTHORIZED: Unauthorized
UPDATE_LINK: 'Update link'
SilverStripe\LinkField\Form\Traits\AllowedLinkClassesTrait:
Expand Down
34 changes: 21 additions & 13 deletions src/Controllers/LinkFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,22 @@ private function createLinkForm(Link $link, string $operation): Form
. "&ownerRelation=$ownerRelation");
$form->setFormAction($url);

// Add save action button
$title = $id
? _t(__CLASS__ . '.UPDATE_LINK', 'Update link')
: _t(__CLASS__ . '.CREATE_LINK', 'Create link');
$actions = FieldList::create([
FormAction::create('save', $title)
->setSchemaData(['data' => ['buttonStyle' => 'primary']]),
]);
$form->setActions($actions);
$isNotEditable = $operation === 'create' && !$link->canCreate()
|| $operation === 'edit' && !$link->canEdit()
|| $this->isReadOnlyField();

// Add save action button if the user has permission to create or edit the Link
if (!$isNotEditable) {
// Add save action button
$title = $id
? _t(__CLASS__ . '.UPDATE_LINK', 'Update link')
: _t(__CLASS__ . '.CREATE_LINK', 'Create link');
$actions = FieldList::create([
FormAction::create('save', $title)
->setSchemaData(['data' => ['buttonStyle' => 'primary']]),
]);
$form->setActions($actions);
}

// Set the form request handler to return a FormSchema response during a POST request
// This will override the default FormRequestHandler::getAjaxErrorResponse() which isn't useful
Expand All @@ -377,13 +384,14 @@ private function createLinkForm(Link $link, string $operation): Form
});

// Make readonly if fail can check
if ($operation === 'create' && !$link->canCreate()
|| $operation === 'edit' && !$link->canEdit()
|| $this->isReadOnlyField()
) {
if ($isNotEditable) {
$form->makeReadonly();
}

if ($this->isReadOnlyField()) {
$form->setMessage(_t(__CLASS__ . '.READ_ONLY_MESSAGE', 'This form is read only'), 'info');
}

// Styling
$form->addExtraClass('form--no-dividers');

Expand Down

0 comments on commit 1994149

Please sign in to comment.