Skip to content
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

[WIP] Add blockTypesData for additional props. #141

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cwmanning
Copy link
Member

Specify (optional) blockType-specific data to pass
as props on render.

Motivation

Imagine a component that selects from a list:

{
  id: 'animals',
  label: 'Animals',
  component: Animals
}

You use 'animals' in many places. But then a new
component request comes in: it should be similar
to 'animals', but filter results to show only
warm-blooded animals.

Now you could create a brand new
blockType, and that might be appropriate in
certain situations. But in many cases, it would be
simpler to reuse component logic with props.

Proposal

Use a new bootstrap options key, blockTypesData,
as a map of blockType.id keys. The value for
each key is an object, which becomes a part of the
BlockType as data and spread as props for the
component.

I'm open to approaching this in other ways. This
is a first pass that I hope helps communicate
the end goal.

Specify (optional) blockType-specific data to pass
as props on render.

# Motivation

Imagine a component that selects from a list:

```
{
  id: 'animals',
  label: 'Animals',
  component: Animals
}
```

You use 'animals' in many places. But then a new 
component request comes in: it should be similar 
to  'animals', but filter results to show only 
warm-blooded animals. 

Now you could create a brand new
blockType, and that might be appropriate in 
certain situations. But in many cases, it would be 
simpler to reuse component logic with props.

# Proposal

Use a new bootstrap options key, `blockTypesData`,
as a map of `blockType.id` keys. The value for
each key is an object, which becomes a part of the
`BlockType` as `data` and spread as props for the
component.

I'm open to approaching this in other ways. This 
is a first pass that I hope helps to communicate 
the end goal.
@codecov-io
Copy link

codecov-io commented Nov 8, 2018

Codecov Report

Merging #141 into master will decrease coverage by 0.16%.
The diff coverage is 88.88%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #141      +/-   ##
==========================================
- Coverage   93.49%   93.33%   -0.17%     
==========================================
  Files          33       33              
  Lines         246      255       +9     
  Branches       28       30       +2     
==========================================
+ Hits          230      238       +8     
- Misses         14       15       +1     
  Partials        2        2
Impacted Files Coverage Δ
src/components/Block.js 96.15% <100%> (+0.32%) ⬆️
src/plugins/bootstrap.js 94.11% <85.71%> (-5.89%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0aa4658...6230591. Read the comment docs.

Copy link
Contributor

@dlederle dlederle left a comment

Choose a reason for hiding this comment

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

👍for the idea. I'll defer to someone who knows more about CK internals to say if this is a good implementation for it.

@@ -28,6 +28,8 @@ export default class YouTube extends React.Component {
render() {
const { baseUrl, content } = this.props

console.log(this.props.name)
Copy link
Contributor

Choose a reason for hiding this comment

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

✂️

Copy link
Member Author

Choose a reason for hiding this comment

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

for sure! left this here just for demonstration purposes.

Copy link
Contributor

@nhunzaker nhunzaker left a comment

Choose a reason for hiding this comment

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

This looks good to me. We should add documentation for it.

What about calling this blockOptions? Additionally, we could expose the ability to add a static to each component to provide defaults, like:

class YouTubeBlock extends React.Component {
  static options = {
    name: "User"
  }
}

let editor = new ColonelKurtz({
  // ...
  blockTypeOptions: {
    youtube: { name: 'Chris' }
  }
})

We'd also pass the value as options to the block.

@cwmanning
Copy link
Member Author

Thanks, @nhunzaker. And good suggestion on the name blockOptions! Planning to come back with some changes (along with coverage and docs) this week.

@@ -74,6 +79,7 @@ export default class Block extends React.PureComponent {
<Component
ref={el => (this.block = el)}
{...block}
{...options}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm worried this will cause bugs where an option field could clear important block properties. What if this were passed as options={options}?

@solomonhawk
Copy link

Is there a reason for placing this config as an option in the call to instantiate CK? As an alternative, each item in the blockTypes array could contain a special key (e.g. blockOptions).

Both routes seem reasonable to me but specifying options for a particular block type within the block type definition kind of makes more sense to me.

./loosely-held-opinion

@cwmanning cwmanning marked this pull request as draft May 13, 2020 16:42
@solomonhawk
Copy link

A few additional thoughts based on a conversation with @cwmanning:

  1. This feature generally unlocks the idea of "Parameterized Block Types", allowing a developer to re-use more code in the case where there are 2 or more block types that are similar but whose differences can be abstracted by a prop being passed in.

  2. It makes sense to have some kind of options key in the block types definition.

  3. There should also be the possibility to override that with a value passed in via an html attribute (from the backend).

I'm thinking it would be useful to have a few layers of configuration with the values specified directly on the input taking precedence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants