-
Notifications
You must be signed in to change notification settings - Fork 683
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into 2019_10_15_Typo_Fixes
- Loading branch information
Showing
25 changed files
with
397 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useCallback, useMemo, useState } from 'react'; | ||
|
||
/** | ||
* Talon for Option. | ||
* | ||
* @param {number} props.attribute_id the id of the option | ||
* @param {string} props.label the label for the option | ||
* @param {function} props.onSelectionChange callback handler for when the option is clicked | ||
* @param {string} props.selectedValue the label of the selected option | ||
* @param {array} props.values an array containing possible values | ||
*/ | ||
export const useOption = props => { | ||
const { | ||
attribute_id, | ||
label, | ||
onSelectionChange, | ||
selectedValue, | ||
values | ||
} = props; | ||
const [selection, setSelection] = useState(null); | ||
const initialSelection = useMemo(() => { | ||
let initialSelection = {}; | ||
const searchValue = selection || selectedValue; | ||
if (searchValue) { | ||
initialSelection = | ||
values.find(value => value.default_label === searchValue) || {}; | ||
} | ||
return initialSelection; | ||
}, [selectedValue, selection, values]); | ||
|
||
const valuesMap = useMemo(() => { | ||
return new Map( | ||
values.map(value => [value.value_index, value.store_label]) | ||
); | ||
}, [values]); | ||
|
||
const selectedValueLabel = `Selected ${label}:`; | ||
const selectedValueDescription = | ||
selection || initialSelection.default_label || 'None'; | ||
|
||
const handleSelectionChange = useCallback( | ||
selection => { | ||
setSelection(valuesMap.get(selection)); | ||
|
||
if (onSelectionChange) { | ||
onSelectionChange(attribute_id, selection); | ||
} | ||
}, | ||
[attribute_id, onSelectionChange, valuesMap] | ||
); | ||
return { | ||
handleSelectionChange, | ||
initialSelection, | ||
selectedValueLabel, | ||
selectedValueDescription | ||
}; | ||
}; |
23 changes: 23 additions & 0 deletions
23
packages/peregrine/lib/talons/ProductOptions/useOptions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useCallback } from 'react'; | ||
|
||
export const useOptions = props => { | ||
const { onSelectionChange, selectedValues } = props; | ||
const handleSelectionChange = useCallback( | ||
(optionId, selection) => { | ||
if (onSelectionChange) { | ||
onSelectionChange(optionId, selection); | ||
} | ||
}, | ||
[onSelectionChange] | ||
); | ||
|
||
const selectedValueMap = new Map(); | ||
for (const { label, value } of selectedValues) { | ||
selectedValueMap.set(label, value); | ||
} | ||
|
||
return { | ||
handleSelectionChange, | ||
selectedValueMap | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useCallback } from 'react'; | ||
|
||
export const useSwatch = props => { | ||
const { onClick, value_index } = props; | ||
|
||
const handleClick = useCallback(() => { | ||
onClick(value_index); | ||
}, [value_index, onClick]); | ||
|
||
return { | ||
handleClick | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useCallback } from 'react'; | ||
|
||
export const useTile = props => { | ||
const { onClick, value_index } = props; | ||
|
||
const handleClick = useCallback(() => { | ||
onClick(value_index); | ||
}, [value_index, onClick]); | ||
|
||
return { | ||
handleClick | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,5 +56,3 @@ Array [ | |
</div>, | ||
] | ||
`; | ||
|
||
exports[`renders null if product is not configurable 1`] = `null`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.