-
Notifications
You must be signed in to change notification settings - Fork 67
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
util: polymorphic primatives #1690
Conversation
also some refactoring and code organization
: FunctionComponentWithAs<DefaultComponentType, ComponentProps> | ||
} | ||
|
||
export interface ForwardRefWithAsRenderFunction< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops. forgot to document using a render prop function. i'll add an example - the approach is pretty straightforward
interface Props {
children?: React.ReactNode | (() => JSX.Element);
}
const TestInput = forwardRefWithAs<Props, "input">((props, ref) => {
const { as: Comp = "input", children, ...rest } = props;
return (
<Comp ref={ref} {...rest}>
{isFunction(children) ? children() : children}
</Comp>
);
});
@danethurber So the idea here is that by asking the consumer to specify the underlying DOM element to be used, you're able to provide better autocomplete and static guarantees? (e.g. if they specify |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a really great pattern that should help a LOT with customization.
Is the idea that a component could have multiple <subcomponent>As
props so that I can override different parts of the component? Like when rendering a Dropdown component I could have buttonAs
and a menuAs
props to customize the rendering of either of them.
(Also submitting fixes for a few typos)
Co-authored-by: Kaiden Rawlinson <kaiden-rawlinson@pluralsight.com>
Co-authored-by: Kaiden Rawlinson <kaiden-rawlinson@pluralsight.com>
Co-authored-by: Kaiden Rawlinson <kaiden-rawlinson@pluralsight.com>
Co-authored-by: Kaiden Rawlinson <kaiden-rawlinson@pluralsight.com>
@justin yes the types should be able to tell which attrs/props are valid for the given tag name. the component's tag name also needs to have a default value. so for example the following component defaults to a
will be typed to only allow valid
the typings would now related to a @KaidenR i don't have a standard answer for how we get to the internal elements yet - but your approach could definitely be something we finalize on. we might end up with a collection of compound components meant to be hyper flexible and each should allow using the With the addition of these typings, our compounds component would allow changing the underlying dom elements at whatever level you need. an example being.
|
* master: build: publish fix(datepicker): closes on click outside resolves #1655 (#1700) tagsinput: fix scroll bar position when items/pills overflow (#1702) flexibility: remove base from color scale & add parseToRgb (#1696) fix(searchinput): weird type bug resolved (#1698) feat(core): temp core util for refactoring to vanillia css (#1689) build: publish select: stop intercepting all key events (#1699) build: publish fix(storybook-addon-theme): update incorrect package name in README feat(storybook-addon-theme): update icon to circle feat(storybook-addon-theme): ability to set theme per-story util: polymorphic primatives (#1690) build: publish fix(select): dropdown zindex added (#1691) Update @pluralsight/ps-design-system-storybook-addon-theme package.json for addon-catalog Update @pluralsight/ps-design-system-storybook-addon-center package.json for addon-catalog
new util methods to support typed(typescript) forwardRef components that support static properties, polymorphism and memoization.
for an example of how the methods would be used please see #1675 which shows refactoring the Button component and related components. Button is by far the most complicated and widely used component that supports the old polymorphic style.