Skip to content

Conversation

@davydog187
Copy link
Collaborator

@davydog187 davydog187 commented Apr 30, 2021

Similar to #355, allows ranges to be passed for :list values. This is particularly useful when you have a fixed number of components that would be generated from within the prop. I found the need for this when implementing a RadioGroup component that composes Surface.Components.Form.RadioButton

There is an argument for allowing any enumerable to be passed as a list, I'm not sure if that's a good idea or not.

Example

<MyComponent list={ 1..4 } />

@davydog187
Copy link
Collaborator Author

updated the CI matrix as well here to only support 1.11 and up

@Malian
Copy link
Collaborator

Malian commented May 2, 2021

LGTM.

@msaraiva If I well understood, we should merge this to the https://github.com/surface-ui/surface/tree/surface-next branch?

@davydog187 davydog187 changed the base branch from master to surface-next May 2, 2021 12:51
@davydog187
Copy link
Collaborator Author

@msaraiva rebased this against surface-next and changed the target to that branch as well

case unquote(expr) do
value when is_list(value) ->
value when is_list(value) or is_struct(value, Range) ->
value
Copy link
Member

Choose a reason for hiding this comment

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

@davydog187 maybe we should call Enum.to_list/1 to make sure the value will be a list (unless this is already done somewhere else?). After all, the value is defined as :list so the user might have code depending on the value being a list, e.g. List.first/1 and friends. This is similar to what we do when accepting keyword lists in props defined as :map, we accept it as syntactic sugar but always convert it to map. WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Seems reasonable. Effectively you're suggesting doing an implicit cast so that when it crosses the component boundary, it's in the type expected. I buy that

Copy link
Member

Choose a reason for hiding this comment

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

Exactly! If the user actually needs a range, then it's better to explicitly define the prop as :range.

Copy link
Collaborator Author

@davydog187 davydog187 May 2, 2021

Choose a reason for hiding this comment

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

Ok, updated the code, feels like it should be tested though! Is compiler_test.exs the best place to test this?

Nevermind, this gets caught by my current thests 👍🏼

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done. Now the tests make more sense too. Good call @msaraiva

@msaraiva msaraiva merged commit a6c7590 into surface-ui:surface-next May 2, 2021
@msaraiva
Copy link
Member

msaraiva commented May 2, 2021

@davydog187 thanks! ❤️

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.

3 participants