2.0.2 • Published 9 months ago

@pacote/react-with-props v2.0.2

Weekly downloads
58
License
MIT
Repository
github
Last release
9 months ago

@pacote/react-with-props

version minified minified + gzip

Enhance component with preset properties.

Installation

yarn add @pacote/react-with-props

Usage

withProps(props: {} | (outer: {}) => {}, component: Component | string)

Creates a new component with the provided props applied to an existing component.

import React from 'react'
import { withProps } from '@pacote/react-with-props'

type ComponentProps = {
  name: string
  value: string
}

const NameValue = (props: ComponentProps) => (
  <div>
    {props.name}: {props.value}
  </div>
)

// Enhance component:
const ExampleValue = withProps({ name: 'Example' }, NameValue)

render(<ExampleValue value="with props" />)
// => <div>Example: with props</div>

// Enhance component with function:
const FieldValue = withProps(
  ({ field = '' }) => ({ name: `[${field}]` }),
  NameValue
)

render(<FieldValue field="Example" value="with props" />)
// => <div>[Example]: with props</div>

// Enhance DOM component:
const PasswordInput = withProps({ type: 'password' }, 'input')

render(<PasswordInput name="secret" />)
// => <input type='password' name='secret' />

withDefaultProps(props: {}, component: Component | string)

Like withProps(), but provided properties can be overridden.

import React from 'react'
import { withDefaultProps } from '@pacote/react-with-props'

type ComponentProps = {
  name: string
  value: string
}

const NameValue = (props: ComponentProps) => (
  <div>
    {props.name}: {props.value}
  </div>
)

// Enhance component:
const Example = withDefaultProps(
  { name: 'Example', value: 'default value' },
  NameValue
)

render(<Example value="with props" />)
// => <div>Example: with props</div>

// Enhance DOM component:
const PasswordInput = withProps(
  { type: 'password', placeholder: 'Password' },
  'input'
)

render(<PasswordInput name="secret" placeholder="API Key" />)
// => <input type='password' name='secret' placeholder='API Key' />

License

MIT © Luís Rodrigues.

2.0.2

9 months ago

2.0.1

1 year ago

2.0.0

1 year ago

1.1.12

2 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago