Toggle UI Elements

Handling minor UX variations in the component by toggling ON/OFF features.

Modify the component to take in a prop to control it’s behavior.

Gotcha:

Easy to overuse this idea by adding props for every variation.

  • Only add in props for features specific to the current feature that the component.
  • Basically, not violate the Single Responsibility Principle.

Example

Show/Hide password feature in Login form

  1. class PasswordField extends Component {
  2. render() {
  3. const {
  4. password,
  5. showHidePassword,
  6. showErrorOnTop,
  7. showLabels,
  8. shouldComplyAda
  9. } = this.props;
  10. return (
  11. <div>
  12. <Password
  13. field={password}
  14. label="Password"
  15. showErrorOnTop={showErrorOnTop}
  16. placeholder={shouldComplyAda ? "" : "Password"}
  17. showLabel={showLabels}
  18. showHidePassword={showHidePassword}
  19. />
  20. </div>
  21. );
  22. }
  23. }