diff --git a/src/frontend/components/property-type/default-type/edit.tsx b/src/frontend/components/property-type/default-type/edit.tsx index e3ee6f3ea..5af79f697 100644 --- a/src/frontend/components/property-type/default-type/edit.tsx +++ b/src/frontend/components/property-type/default-type/edit.tsx @@ -1,81 +1,75 @@ -import React, { ReactNode } from 'react' +/* eslint-disable @typescript-eslint/explicit-function-return-type */ +import React, { FC, useState, memo, useEffect } from 'react' import Select from 'react-select' import { withTheme, DefaultTheme } from 'styled-components' import { Input, FormMessage, FormGroup, Label, selectStyles } from '@admin-bro/design-system' import { EditPropertyProps } from '../base-property-props' import { recordPropertyIsEqual } from '../record-property-is-equal' +import usePrevious from '../../../utils/usePrevious' type CombinedProps = EditPropertyProps & {theme: DefaultTheme} -class Edit extends React.Component { - constructor(props) { - super(props) - this.handleInputChange = this.handleInputChange.bind(this) - this.handleSelectChange = this.handleSelectChange.bind(this) - } - - shouldComponentUpdate(prevProps: CombinedProps): boolean { - return !recordPropertyIsEqual(prevProps, this.props) - } +const Edit: FC = (props) => { + const { property, record } = props + const error = record.errors?.[property.name] + return ( + + + {property.availableValues ? : } + {error && error.message} + + ) +} - handleInputChange(event): void { - const { onChange, property } = this.props - onChange(property.name, event.target.value) +const SelectEdit: FC = (props) => { + const { theme, record, property, onChange } = props + if (!property.availableValues) { + return null } + const propValue = record.params?.[property.name] ?? '' + const styles = selectStyles(theme) + const selected = property.availableValues.find(av => av.value === propValue) + return ( + - ) + const previous = usePrevious(propValue) + useEffect(() => { + // this means props updated + if (propValue !== previous) { + setValue(propValue) } - return ( - - ) - } + }, []) - render(): ReactNode { - const { property, record } = this.props - const error = record.errors && record.errors[property.name] - return ( - - - {this.renderInput()} - {error && error.message} - - ) - } + return ( + setValue(e.target.value)} + onBlur={() => onChange(property.name, value)} + value={value} + disabled={property.isDisabled} + /> + ) } -export default withTheme(Edit) +export default withTheme(memo(Edit, recordPropertyIsEqual)) diff --git a/src/frontend/components/property-type/password/edit.tsx b/src/frontend/components/property-type/password/edit.tsx index e08b8283e..692dc07e3 100644 --- a/src/frontend/components/property-type/password/edit.tsx +++ b/src/frontend/components/property-type/password/edit.tsx @@ -1,16 +1,26 @@ -import React, { useState, memo } from 'react' +/* eslint-disable @typescript-eslint/explicit-function-return-type */ +import React, { useState, memo, useEffect } from 'react' import { Label, Input, FormGroup, InputGroup, FormMessage, Button, Icon } 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: React.FC = (props) => { const { property, record, onChange } = props - const value = record.params[property.name] + const propValue = record.params[property.name] + const [value, setValue] = useState(propValue) 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) { + setValue(propValue) + } + }, []) + return (