Skip to content

Commit

Permalink
Merge pull request GeekyAnts#3342 from GeekyAnts/feat/v3-factory
Browse files Browse the repository at this point in the history
Feat/v3 factory
  • Loading branch information
theankurkedia authored Jan 5, 2021
2 parents cdfaae7 + 3cfae80 commit 42bf8b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import Wrapper from './../../Wrapper';
import Usage from './usage';
import Theme from './theme';
import Modes from './modes';
import Ref from './ref';

storiesOf('Factory', module)
.addDecorator(withKnobs)
.addDecorator((getStory: any) => <Wrapper>{getStory()}</Wrapper>)
.add('Usage', () => <Usage />)
.add('Theme', () => <Theme />)
.add('Modes', () => <Modes />);
.add('Modes', () => <Modes />)
.add('Ref', () => <Ref />);
21 changes: 21 additions & 0 deletions example/storybook/stories/components/composites/factory/ref.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import { NBFactory, Button } from 'native-base';
import { TextInput } from 'react-native';

export default function () {
const NBInput = NBFactory(TextInput);
const inputRef = React.useRef(null);
return (
<>
<NBInput ref={inputRef} height={10} border={1} borderColor="cyan.400" />
<Button
onPress={() => {
// @ts-ignore
inputRef?.current?.focus();
}}
>
Set Focus
</Button>
</>
);
}
6 changes: 3 additions & 3 deletions src/core/factory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function NBFactory<P>(
Component: React.ComponentType<P>,
componentTheme?: ComponentTheme
) {
return function WithFactory(props: P & FactoryComponentProps) {
return React.forwardRef((props: P & FactoryComponentProps, ref: any) => {
const StyledComponent = styled(Component as AnyStyledComponent)(
color,
background,
Expand All @@ -53,8 +53,8 @@ export function NBFactory<P>(
customLayout
);
const calculatedProps = usePropsWithComponentTheme(componentTheme, props);
return <StyledComponent {...(calculatedProps as P)} />;
};
return <StyledComponent {...(calculatedProps as P)} ref={ref} />;
});
}

export type { FactoryComponentProps };

0 comments on commit 42bf8b3

Please sign in to comment.