Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
editable field bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tinkertrain committed Feb 15, 2022
1 parent dd66a0d commit 6d8a632
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"release:beta": "npm run release -- --yolo --any-branch",
"snapshot": "npm run storybook:build && percy-storybook --widths=1280 --build_dir=public",
"start": "npm run storybook",
"storybook": "SC_ATTR=HSDS-react start-storybook -p 8900",
"storybook": "SC_ATTR=HSDS-react start-storybook -p 8900 --ci",
"storybook:ci": "npm run storybook -- --ci",
"storybook:build": "SC_ATTR=HSDS-react NODE_ENV=production build-storybook -c .storybook -o public",
"storybook:clean": "rimraf public",
Expand Down
1 change: 1 addition & 0 deletions src/components/EditableField/EditableField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export class EditableField extends React.Component {
value,
name,
defaultOption: defaultStateOption,
currentFieldValue: this.state.fieldValue,
})

if (!equal(initialFieldValue, this.state.fieldValue)) {
Expand Down
26 changes: 18 additions & 8 deletions src/components/EditableField/EditableField.utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { isArray, isObject } from '../../utilities/is'
import { isObject } from '../../utilities/is'
import { find } from '../../utilities/arrays'
import { getColor } from '../../styles/utilities/color'
import { createUniqueIDFactory } from '../../utilities/id'

const uniqueID = createUniqueIDFactory('EditableField')

export const EF_COMPONENT_KEY = 'EditableField'
Expand All @@ -12,23 +11,34 @@ export const INPUT_COMPONENT_KEY = 'FieldInput'
export const MASK_COMPONENT_KEY = 'FieldMask'
export const TRUNCATED_COMPONENT_KEY = 'Truncated'

export function normalizeFieldValue({ value, name, defaultOption }) {
if (isArray(value)) {
export function normalizeFieldValue({
value,
name,
defaultOption,
currentFieldValue,
}) {
if (Array.isArray(value)) {
if (value.length === 0) {
return [createNewValueFieldObject('', name, defaultOption)]
}
return value.map(val => createNewValueFieldObject(val, name, defaultOption))
} else {
return [createNewValueFieldObject(value, name, defaultOption)]
const currentId = currentFieldValue && currentFieldValue[0].id
return [createNewValueFieldObject(value, name, defaultOption, currentId)]
}
}

export function createNewValueFieldObject(value, name, defaultOption) {
export function createNewValueFieldObject(
value,
name,
defaultOption,
currentId
) {
// If it's an object already, grab the fields first
if (isObject(value)) {
const fieldObj = {
...value,
id: value.id || `${name}_${uniqueID()}`,
id: currentId || value.id || `${name}_${uniqueID()}`,
validated: false,
}

Expand All @@ -41,7 +51,7 @@ export function createNewValueFieldObject(value, name, defaultOption) {

const fieldObj = {
value,
id: `${name}_${uniqueID()}`,
id: currentId || `${name}_${uniqueID()}`,
validated: false,
}

Expand Down

0 comments on commit 6d8a632

Please sign in to comment.