Skip to content

add default state to checkbox inside textRun #1790

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
29 changes: 29 additions & 0 deletions src/PhpWord/Element/CheckBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class CheckBox extends Text
*/
private $name;

/**
* Default state.
*
* @var bool
*/
private $defaultChecked = false;

/**
* Create new instance.
*
Expand Down Expand Up @@ -71,4 +78,26 @@ public function getName()
{
return $this->name;
}

/**
* Set default state.
*
* @return self
*/
public function setDefaultChecked(bool $default = true)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public function setDefaultChecked(bool $default = true)
public function setDefaultChecked(bool $default = true): self

Copy link
Author

Choose a reason for hiding this comment

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

How can i update the changelog ?

Copy link
Author

Choose a reason for hiding this comment

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

I added the test but i dont know why it fails only on 8.2

{
$this->defaultChecked = $default;

return $this;
}

/**
* Is default state checked?
*
* @return bool
*/
public function isDefaultChecked(): bool
{
return $this->defaultChecked;
}
}
2 changes: 1 addition & 1 deletion src/PhpWord/Writer/Word2007/Element/CheckBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function write(): void
$xmlWriter->startElement('w:checkBox');
$xmlWriter->writeAttribute('w:sizeAuto', '');
$xmlWriter->startElement('w:default');
$xmlWriter->writeAttribute('w:val', 0);
$xmlWriter->writeAttribute('w:val', $element->isDefaultChecked() ? 1 : 0);
$xmlWriter->endElement(); //w:default
$xmlWriter->endElement(); //w:checkBox
$xmlWriter->endElement(); // w:ffData
Expand Down
12 changes: 12 additions & 0 deletions tests/PhpWordTests/Element/CheckBoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,16 @@ public function testParagraph(): void
$oCheckBox->setParagraphStyle(['alignment' => Jc::CENTER, 'spaceAfter' => 100]);
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oCheckBox->getParagraphStyle());
}

/**
* Set and get default value.
*/
public function testDefault(): void
{
$oCheckBox = new CheckBox('chkBox', 'CheckBox');
self::assertFalse($oCheckBox->isDefaultChecked());

$oCheckBox->setDefaultChecked(true);
self::assertTrue($oCheckBox->isDefaultChecked());
}
}
Loading