简体   繁体   English

角材料:“自定义表单字段控件”-移动浮动标签(向下)

[英]Angular Material: Custom Form Field Control - move floating label (down)

I've created a Custom Form Field Control for Angular 8 compatible with Reactive Forms and Angular Material . 我已经为Angular 8创建了一个自定义表单域控件 ,该控件Reactive FormsAngular Material兼容。 However, it being a simplified Rich-Text Editor , it has a header with various buttons with actions for the user. 但是,它是简化的 Rich-Text编辑器 ,它带有一个带有各种按钮的标题,这些按钮带有针对用户的操作。

How can I move the Placeholder label below the header of my input control to the actual textarea? 如何将输入控件标题下方占位符标签移动到实际文本区域?

Current placeholder label placement 当前占位符标签的位置

Well, since nobody really responded to my question, I'll post my own solution to this problem, one cruder (Solution 2) and one I deem proper (Solution 2, though to me it appears otherwise). 好吧,由于没有人真正回答我的问题,因此我将发布自己的解决方案,一个更简单(解决方案2),另一个我认为合适的解决方案(解决方案2,但对我而言则相反)。

Edit: Solution 2: Proper approach 编辑:解决方案2:正确的方法

Well, I tried to make my code slightly more-configurable, hence I made one little mistake when changing code - I changed one variable to static , a variable used to declare the ID MatFormField uses in a class definition inside itself which we can use to customize the look of our component. 好吧,我试图使我的代码更具可配置性,因此在更改代码时犯了一个小错误-我将一个变量更改为static ,该变量用于在自身内部的类定义中声明ID MatFormField使用的ID自定义组件的外观。

Namely controlType . controlType Using this variable, we can identify when our component is in-use by direct class name following naming convention mat-form-field-type-<controlType> . 使用此变量,我们可以按照命名约定mat-form-field-type-<controlType>的直接类名来标识何时使用组件。 So, since my controlType = "app-text-editor" , I can use it like this: 因此,由于我的controlType = "app-text-editor" ,我可以这样使用它:

.mat-form-field-type-app-text-editor:not(.mat-form-field-should-float) .mat-form-field-label-wrapper > .mat-form-field-label {
    margin-top: 2.5rem;
    padding: .5em;
}

Original: Solution 1: Hacky-approach 原文:解决方案1:hacky方法

What I did was change my component to encapsulation: ViewEncapsulation.None , used the selector of my component inside css as my main identifier (in my case: app-text-editor ) and then used CSS Sibling selector to select the floating label and placeholder to set offset for my TextEditor header and reset it back to default once the label is floating. 我所做的就是将组件更改为encapsulation: ViewEncapsulation.None ,使用了css中组件的选择器作为我的主要标识符(在我的情况下为app-text-editor ),然后使用CSS Sibling选择器选择了浮动标签占位符为我的TextEditor标头设置偏移量,并在标签浮动后将其重置为默认值。 The resulting CSS looks like this: 生成的CSS如下所示:

app-text-editor {
  // Styling for actual TextEditor

  &.floating ~ .mat-form-field-label-wrapper > .mat-form-field-label {
    margin-top: initial;
    padding: initial;
  }
  & ~ .mat-form-field-label-wrapper > .mat-form-field-label {
    margin-top: 2.5rem;
    padding: .5em; // Used to properly align the content inside my contenteditable
}

Or as pure CSS would look like: 或者像纯CSS那样:

app-text-editor.floating ~ .mat-form-field-label-wrapper > .mat-form-field-label {
  margin-top: initial;
  padding: initial;
}
app-text-editor ~ .mat-form-field-label-wrapper > .mat-form-field-label {
  margin-top: 2.5rem;
  padding: .5em; /* Used to properly align the content inside my contenteditable */
}

Weirdly enough, even the animation transition looks smooth despite me using such a hacky-approach for repositioning it. 奇怪的是,尽管我使用了这样的方法来重新定位动画,但即使动画过渡也看起来很平滑。

If you don't mind using advanced CSS selector ( Can I Use: Advanced CSS3 selectors ), the solution can be even cleaner: 如果您不介意使用高级CSS选择器( 我可以使用: 高级CSS3选择器 ),则该解决方案甚至可以更干净:

SCSS : SCSS

app-text-editor {
  // Styling for the actual TextEditor

  &:not(.floating) ~ .mat-form-field-label-wrapper > .mat-form-field-label {
    margin-top: 2.5rem;
    padding: .5em; // Used to properly align the content inside my contenteditable
}

Pure CSS : 纯CSS

app-text-editor:not(.floating) ~ .mat-form-field-label-wrapper > .mat-form-field-label {
  margin-top: 2.5rem;
  padding: .5em; /* Used to properly align the content inside my contenteditable */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM