Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

feat(renderComponent): merged state and props for styles and accessibility #173

Merged
merged 1 commit into from
Sep 3, 2018

Conversation

bmdalex
Copy link
Collaborator

@bmdalex bmdalex commented Sep 3, 2018

Component style function

This PR adds the ability to receive a unified (merged) object of props and state as argument to component's style functions, as well as to the accessibility behaviors.

More details in the dedicated issue: #161

TODO

  • Conformance test
  • Minimal doc site example
  • Stardust base theme
  • Teams Light theme
  • Teams Dark theme
  • Teams Contrast theme
  • Confirm RTL usage
  • W3 accessibility check
  • Stardust accessibility check
  • Update glossary props table
  • Update the CHANGELOG.md

API Proposal

Before

const buttonStyles: IComponentPartStylesInput = {
  root: ({
    props,
    variables
  }: { props: IButtonProps; variables: IButtonVars }): ICSSInJSStyle => {
    // props contains all props properties of the button
    return {}
  },
}

After

const buttonStyles: IComponentPartStylesInput = {
  root: ({
    props,
    variables
  }: { props: IButtonProps & IButtonState; variables: IButtonVars }): ICSSInJSStyle => {
    // props contains props and state properties merged, with props taking priority
    // in case of overlapping properties
    return {}
  },
}

@codecov
Copy link

codecov bot commented Sep 3, 2018

Codecov Report

Merging #173 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #173   +/-   ##
=======================================
  Coverage   68.02%   68.02%           
=======================================
  Files         101      101           
  Lines        1370     1370           
  Branches      269      260    -9     
=======================================
  Hits          932      932           
  Misses        436      436           
  Partials        2        2

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 82f0c56...d958935. Read the comment docs.

})
})

it('simple merge of props and state if both are set but there are no overlapping properties', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

it would be better to describe result in the test name, like props and state properties are merged together and props properties take precedence over ones of state for the next one

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think I understand, let's discuss

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

discussed with @kuzhelov and amended

@@ -83,7 +73,11 @@ const renderComponent = <P extends {}>(

// Resolve styles using resolved variables, merge results, allow props.styles to override
const mergedStyles = mergeComponentStyles(componentStyles[displayName], props.styles)
const styleParam: ComponentStyleFunctionParam = { props, variables: resolvedVariables }
const appliedProps = { ...state, ...props }
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
Contributor

Choose a reason for hiding this comment

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

one note - before we'll come up with a name for this 'merged' object, maybe we could use something that is a bit dumb but explicit and descriptive - like stateAndProps?

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, thanks

...props,
...state,
})
return callable(customAccessibility || defaultAccessibility || DefaultBehavior)(props)
Copy link
Contributor

Choose a reason for hiding this comment

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

accessibility functions end up with being provided with state only now - but previously state data was provided as well (this was a correct and expected behavior)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@jurokapsiar is this correct here?
@kuzhelov anything to change?

Copy link
Contributor

Choose a reason for hiding this comment

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

accessibility need a merge of props and state. props should override state. looking at line 83, I think this is correctly implemented now.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would rather change the name of the argument, because it is a bit misleading - to indicate that 'state' is merged to 'props'

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, thanks guys

types/theme.d.ts Outdated
@@ -77,7 +86,7 @@ export interface ICSSInJSStyle extends React.CSSProperties {
}

export interface ComponentStyleFunctionParam {
props: IProps
props: IState & IProps
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

types/theme.d.ts Outdated
@@ -20,7 +21,15 @@ type ObjectOf<T> = { [key: string]: T }
// Props
// ========================================================

export type IProps = ObjectOf<any>
export type IProps = Extendable<{
variables?: ComponentVariablesInput
Copy link
Contributor

@kuzhelov kuzhelov Sep 3, 2018

Choose a reason for hiding this comment

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

this won't work for all the components, unfortunately (Provider as an example) - lets keep to be generic for now

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok, done

export interface IRenderConfig {
className?: string
defaultProps?: { [key: string]: any }
displayName: string
handledProps: string[]
props: IRenderConfigProps
state: { [key: string]: any }
props: IProps
Copy link
Contributor

Choose a reason for hiding this comment

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

lets use the type that was used before - as IProps is not generic enough to suite all the components if styles and variables are introduced

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

@bmdalex bmdalex force-pushed the feat/styles-add-state-to-props branch from 780090c to d958935 Compare September 3, 2018 12:44
Copy link
Contributor

@kuzhelov kuzhelov left a comment

Choose a reason for hiding this comment

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

👍

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

Successfully merging this pull request may close these issues.

None yet

3 participants