API Add a way to check if a form or form field has an extra css class (#10112)

Required for silverstripe/silverstripe-admin#1252
This commit is contained in:
GuySartorelli 2021-11-01 17:01:17 +13:00 committed by GitHub
parent cd076542f4
commit fc349db511
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 0 deletions

View File

@ -1779,6 +1779,25 @@ class Form extends ViewableData implements HasRequestHandler
return implode(' ', array_unique($this->extraClasses)); return implode(' ', array_unique($this->extraClasses));
} }
/**
* Check if a CSS-class has been added to the form container.
*
* @param string $class A string containing a classname or several class
* names delimited by a single space.
* @return boolean True if all of the classnames passed in have been added.
*/
public function hasExtraClass($class)
{
//split at white space
$classes = preg_split('/\s+/', $class);
foreach ($classes as $class) {
if (!isset($this->extraClasses[$class])) {
return false;
}
}
return true;
}
/** /**
* Add a CSS-class to the form-container. If needed, multiple classes can * Add a CSS-class to the form-container. If needed, multiple classes can
* be added by delimiting a string with spaces. * be added by delimiting a string with spaces.

View File

@ -602,6 +602,25 @@ class FormField extends RequestHandler
return implode(' ', $classes); return implode(' ', $classes);
} }
/**
* Check if a CSS-class has been added to the form container.
*
* @param string $class A string containing a classname or several class
* names delimited by a single space.
* @return boolean True if all of the classnames passed in have been added.
*/
public function hasExtraClass($class)
{
//split at white space
$classes = preg_split('/\s+/', $class);
foreach ($classes as $class) {
if (!isset($this->extraClasses[$class])) {
return false;
}
}
return true;
}
/** /**
* Add one or more CSS-classes to the FormField container. * Add one or more CSS-classes to the FormField container.
* *

View File

@ -93,6 +93,19 @@ class FormFieldTest extends SapphireTest
$this->assertStringEndsWith('class1 class2', $field->extraClass()); $this->assertStringEndsWith('class1 class2', $field->extraClass());
} }
public function testHasExtraClass()
{
$field = new FormField('MyField');
$field->addExtraClass('class1');
$field->addExtraClass('class2');
$this->assertTrue($field->hasExtraClass('class1'));
$this->assertTrue($field->hasExtraClass('class2'));
$this->assertTrue($field->hasExtraClass('class1 class2'));
$this->assertTrue($field->hasExtraClass('class2 class1'));
$this->assertFalse($field->hasExtraClass('class3'));
$this->assertFalse($field->hasExtraClass('class2 class3'));
}
public function testRemoveExtraClass() public function testRemoveExtraClass()
{ {
$field = new FormField('MyField'); $field = new FormField('MyField');

View File

@ -726,6 +726,19 @@ class FormTest extends FunctionalTest
$this->assertStringEndsWith('class1 class2', $form->extraClass()); $this->assertStringEndsWith('class1 class2', $form->extraClass());
} }
public function testHasExtraClass()
{
$form = $this->getStubForm();
$form->addExtraClass('class1');
$form->addExtraClass('class2');
$this->assertTrue($form->hasExtraClass('class1'));
$this->assertTrue($form->hasExtraClass('class2'));
$this->assertTrue($form->hasExtraClass('class1 class2'));
$this->assertTrue($form->hasExtraClass('class2 class1'));
$this->assertFalse($form->hasExtraClass('class3'));
$this->assertFalse($form->hasExtraClass('class2 class3'));
}
public function testRemoveExtraClass() public function testRemoveExtraClass()
{ {
$form = $this->getStubForm(); $form = $this->getStubForm();