-
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
Separation of duties #47
base: master
Are you sure you want to change the base?
Conversation
Nice! I think separating the business logic from the view of the component is a good idea. However, I think the abstraction may need to be thought out a little more. My main concern is that the 'utility' component assumes the children My other concern is allowing the 'view' component to extend the 'utility' component. Extending other components are commonly considered an anti-pattern in React due to shared namespaces & the user is forced to use a class component. I'd recommend either: converting the 'utility' component into a React Hook, or using the 'render prop' pattern. This will not force the user to using a class component to implement their input fields, and will have clear understanding of where the utility functions are coming from. Here could be a possible React Hook example (pretty high level): import { useCreditCardInputs } from 'react-credit-card-input';
export default function CreditCardInputs() {
const { cardNumberProps, expiryProps, cvcProps } = useCreditCardInputs();
return (
<div>
<input name="card-number" {...cardNumberProps} />
<input name="expiry" {...expiryProps} />
<input name="cvc" {...cvcProps} />
</div>
)
} and maybe a render props example: import { CreditCardInputsContainer } from 'react-credit-card-input';
export default function CreditCardInputs() {
return (
<CreditCardInputsContainer>
{({ cardNumberProps, expiryProps, cvcProps }) => (
<div>
<input name="card-number" {...cardNumberProps} />
<input name="expiry" {...expiryProps} />
<input name="cvc" {...cvcProps} />
</div>
)}
</CreditCardInputsContainer>
)
} |
1d84d1d
to
5402f78
Compare
Fix an issue on showing card image
f6819e0
to
3892403
Compare
<Container>
<FieldWrapper>
<CardImage />
<InputWrapper>
<CardNumberInput/>
</InputWrapper>
<InputWrapper>
<CardExpiryInput />
</InputWrapper>
<InputWrapper>
<CardCvcInput />
</InputWrapper>
<InputWrapper>
<CardZipInput />
</InputWrapper>
</FieldWrapper>
</Container> |
@jxom the business logic is separated from the view of the component by using the React Hook. |
So... in essence... this PR divides the business logic from the view of the component. Why? By doing so, it allows another React component to extend the main class, and renders a different view.
Several projects do not take advantages of style-components, it means this limits other projects to import that great library, because it forces others to use style-components. For instance, to import a new framework in a big project, such as style-components, it takes sometime to work between architecture and design teams. So, by having two classes, it makes possible to render another view without the need to install style-component. See example below - another library extending from
CreditCardUtilities
:At the end, this PR makes possible to have different React components working in harmony. See others sample below: