Skip to content

Commit

Permalink
fix: onChange can also update other fields
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek-krysiak committed Aug 27, 2020
1 parent d4ce9a2 commit 46dacb9
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 31 deletions.
19 changes: 19 additions & 0 deletions example-app/cypress/integration/nested/create-nested.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference types="cypress" />
/// <reference types="../../support" />

context('resources/Nested/actions/new', () => {
before(() => {
cy.login()
})

beforeEach(() => {
Cypress.Cookies.preserveOnce(Cypress.env('COOKIE_NAME'))
cy.visit('resources/Nested/actions/new')
})

it('fills name when button is clicked', () => {
cy.get('[data-testid="name-button"]').click()
cy.get('[data-testid="property-edit-name"] input')
.should('have.value', 'my new name')
})
})
9 changes: 8 additions & 1 deletion example-app/src/nested/nested.admin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
const { default: AdminBro } = require('admin-bro')
const { Nested } = require('./nested.entity')

/** @type {import('admin-bro').ResourceOptions} */
const options = {

properties: {
valueTrigger: {
components: {
edit: AdminBro.bundle('./value-trigger.component.tsx'),
},
},
},
}

module.exports = {
Expand Down
25 changes: 25 additions & 0 deletions example-app/src/nested/value-trigger.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react'
import { EditPropertyProps } from 'admin-bro'
import { Button, Box } from '@admin-bro/design-system'

const ValueTrigger: React.FC<EditPropertyProps> = (props) => {
const { onChange, record } = props

const handleClick = (): void => {
onChange({
...record,
params: {
...record.params,
name: 'my new name',
},
})
}

return (
<Box mb="xxl">
<Button data-testid="name-button" type="button" onClick={handleClick}>Set Name</Button>
</Box>
)
}

export default ValueTrigger
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const DOCS = 'https://softwarebrothers.github.io/admin-bro-dev'
export const DOCS = 'https://adminbro.com'
export const DEFAULT_PATHS = {
rootPath: '/admin',
logoutPath: '/admin/logout',
Expand Down
7 changes: 2 additions & 5 deletions src/frontend/components/property-type/default-type/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Input, FormMessage, FormGroup, Label, selectStyles } from '@admin-bro/d

import { EditPropertyProps } from '../base-property-props'
import { recordPropertyIsEqual } from '../record-property-is-equal'
import usePrevious from '../../../utils/usePrevious'

type CombinedProps = EditPropertyProps & {theme: DefaultTheme}

Expand Down Expand Up @@ -52,13 +51,11 @@ const TextEdit: FC<CombinedProps> = (props) => {
const propValue = record.params?.[property.name] ?? ''
const [value, setValue] = useState(propValue)

const previous = usePrevious(propValue)
useEffect(() => {
// this means props updated
if (propValue !== previous) {
if (value !== propValue) {
setValue(propValue)
}
}, [])
}, [propValue])

return (
<Input
Expand Down
7 changes: 2 additions & 5 deletions src/frontend/components/property-type/password/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Label, Input, FormGroup, InputGroup, FormMessage, Button, Icon } from '

import { EditPropertyProps } from '../base-property-props'
import { recordPropertyIsEqual } from '../record-property-is-equal'
import usePrevious from '../../../utils/usePrevious'

const Edit: React.FC<EditPropertyProps> = (props) => {
const { property, record, onChange } = props
Expand All @@ -13,13 +12,11 @@ const Edit: React.FC<EditPropertyProps> = (props) => {
const error = record.errors && record.errors[property.name]
const [isInput, setIsInput] = useState(false)

const previous = usePrevious(propValue)
useEffect(() => {
// this means props updated
if (propValue !== previous) {
if (value !== propValue) {
setValue(propValue)
}
}, [])
}, [propValue])

return (
<FormGroup error={!!error}>
Expand Down
7 changes: 2 additions & 5 deletions src/frontend/components/property-type/textarea/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@ import { Input, Label, FormGroup, FormMessage } from '@admin-bro/design-system'

import { EditPropertyProps } from '../base-property-props'
import { recordPropertyIsEqual } from '../record-property-is-equal'
import usePrevious from '../../../utils/usePrevious'

const Edit: FC<EditPropertyProps> = (props) => {
const { onChange, property, record } = props
const propValue = record.params?.[property.name] ?? ''
const [value, setValue] = useState(propValue)
const error = record.errors?.[property.name]

const previous = usePrevious(propValue)
useEffect(() => {
// this means props updated
if (propValue !== previous) {
if (value !== propValue) {
setValue(propValue)
}
}, [])
}, [propValue])

return (
<FormGroup error={Boolean(error)}>
Expand Down
14 changes: 0 additions & 14 deletions src/frontend/utils/usePrevious.ts

This file was deleted.

0 comments on commit 46dacb9

Please sign in to comment.