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 23, 2024
1 parent 6a80f83 commit 0f62e1d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 24 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
3 changes: 1 addition & 2 deletions src/Controllers/LinkFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,7 @@ 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()
) {
|| $this->isReadOnlyField()) {
$form->makeReadonly();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Form/LinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getSchemaDataDefaults()
*/
public function performReadonlyTransformation()
{
$clone = $this->castedCopy($this);
$clone = clone $this;
$clone->setReadonly(true);

return $clone;
Expand Down
2 changes: 1 addition & 1 deletion src/Form/MultiLinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private function loadFrom(DataObject $record): void
*/
public function performReadonlyTransformation()
{
$clone = $this->castedCopy($this);
$clone = clone $this;
$clone->setReadonly(true);

return $clone;
Expand Down

0 comments on commit 0f62e1d

Please sign in to comment.