@dloizides/ui-forms 1.45.0 → 1.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -8,7 +8,9 @@ var designTokens = require('@dloizides/design-tokens');
8
8
  var uiButtons = require('@dloizides/ui-buttons');
9
9
  var reactDom = require('react-dom');
10
10
  var a11y = require('@dloizides/a11y');
11
+ var apiClientBase = require('@dloizides/api-client-base');
11
12
  var Svg = require('react-native-svg');
13
+ var uiIcons = require('@dloizides/ui-icons');
12
14
 
13
15
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
14
16
 
@@ -3752,6 +3754,71 @@ function useFocusFirstInvalid(refs) {
3752
3754
  first?.focus();
3753
3755
  }, []);
3754
3756
  }
3757
+
3758
+ // src/useServerFieldErrors/assignServerErrors.ts
3759
+ function lastSegment(path) {
3760
+ return path.slice(path.lastIndexOf(".") + 1).toLowerCase();
3761
+ }
3762
+ function matchField(path, fields) {
3763
+ const exact = fields.find((field2) => field2 === path);
3764
+ if (exact !== void 0) {
3765
+ return exact;
3766
+ }
3767
+ const key = lastSegment(path);
3768
+ return key === "" ? null : fields.find((field2) => field2.toLowerCase() === key) ?? null;
3769
+ }
3770
+ function messageOf(error, formatCode) {
3771
+ const formatted = error.code !== void 0 && formatCode !== void 0 ? formatCode(error.code, error) : null;
3772
+ return typeof formatted === "string" && formatted !== "" ? formatted : error.message;
3773
+ }
3774
+ function assignServerErrors(errors, fields, formatCode) {
3775
+ const byField = {};
3776
+ let formError = null;
3777
+ for (const error of errors) {
3778
+ const field2 = matchField(error.path, fields);
3779
+ const message = messageOf(error, formatCode);
3780
+ if (field2 === null) {
3781
+ formError = formError ?? message;
3782
+ } else if (byField[field2] === void 0) {
3783
+ byField[field2] = message;
3784
+ }
3785
+ }
3786
+ const matched = fields.filter((field2) => byField[field2] !== void 0);
3787
+ return { byField, formError, matched };
3788
+ }
3789
+
3790
+ // src/useServerFieldErrors/useServerFieldErrors.ts
3791
+ var EMPTY_STATE = { byField: {}, formError: null };
3792
+ var NO_REFS = {};
3793
+ function withoutField(state, field2) {
3794
+ if (state.byField[field2] === void 0) {
3795
+ return state;
3796
+ }
3797
+ const byField = { ...state.byField };
3798
+ delete byField[field2];
3799
+ return { ...state, byField };
3800
+ }
3801
+ function useServerFieldErrors(options) {
3802
+ const { fields, refs, formatCode } = options;
3803
+ const [state, setState] = React37.useState(EMPTY_STATE);
3804
+ const focusFirst = useFocusFirstInvalid(refs ?? NO_REFS);
3805
+ const hasRefs = refs !== void 0;
3806
+ const apply = React37.useCallback(
3807
+ (error) => {
3808
+ const assigned = assignServerErrors(apiClientBase.parseFieldErrors(error), fields, formatCode);
3809
+ setState({ byField: assigned.byField, formError: assigned.formError });
3810
+ if (hasRefs && assigned.matched.length > 0) {
3811
+ focusFirst(assigned.matched);
3812
+ }
3813
+ return assigned.matched.length > 0;
3814
+ },
3815
+ [fields, formatCode, focusFirst, hasRefs]
3816
+ );
3817
+ const clear = React37.useCallback((field2) => setState((current) => withoutField(current, field2)), []);
3818
+ const reset = React37.useCallback(() => setState(EMPTY_STATE), []);
3819
+ const errorFor = React37.useCallback((field2) => state.byField[field2], [state.byField]);
3820
+ return { errorFor, apply, clear, formError: state.formError, reset };
3821
+ }
3755
3822
  function isScrollable(node) {
3756
3823
  return "scrollIntoView" in Object(node);
3757
3824
  }
@@ -5753,6 +5820,261 @@ function DecisionPanel(props) {
5753
5820
  ] });
5754
5821
  }
5755
5822
 
5823
+ // src/PasswordField/PasswordRulesDisplay.ts
5824
+ var PasswordRulesDisplay = /* @__PURE__ */ ((PasswordRulesDisplay2) => {
5825
+ PasswordRulesDisplay2["Checklist"] = "checklist";
5826
+ PasswordRulesDisplay2["MeterGrid"] = "meterGrid";
5827
+ PasswordRulesDisplay2["Chips"] = "chips";
5828
+ PasswordRulesDisplay2["Sentence"] = "sentence";
5829
+ return PasswordRulesDisplay2;
5830
+ })(PasswordRulesDisplay || {});
5831
+
5832
+ // src/PasswordField/RuleTone.ts
5833
+ var RuleTone = /* @__PURE__ */ ((RuleTone2) => {
5834
+ RuleTone2["Met"] = "met";
5835
+ RuleTone2["Unmet"] = "unmet";
5836
+ RuleTone2["Refused"] = "refused";
5837
+ return RuleTone2;
5838
+ })(RuleTone || {});
5839
+ function ruleTone(met, flagUnmet) {
5840
+ if (met) return "met" /* Met */;
5841
+ return flagUnmet ? "refused" /* Refused */ : "unmet" /* Unmet */;
5842
+ }
5843
+ var MARK_SIZE = 16;
5844
+ var CHECK_SIZE = 12;
5845
+ var RING_WIDTH = 1.5;
5846
+ var POP_FROM = 0.6;
5847
+ var POP_PEAK = 1.12;
5848
+ var POP_UP_MS = 160;
5849
+ var POP_SETTLE_MS = 100;
5850
+ var styles38 = reactNative.StyleSheet.create({
5851
+ mark: { width: MARK_SIZE, height: MARK_SIZE, borderRadius: MARK_SIZE / 2, alignItems: "center", justifyContent: "center" },
5852
+ ring: { borderWidth: RING_WIDTH }
5853
+ });
5854
+ function RuleMark({ tone, colors }) {
5855
+ const reduced = useReducedMotion();
5856
+ const scale = React37.useRef(new reactNative.Animated.Value(1)).current;
5857
+ const wasMet = React37.useRef(tone === "met" /* Met */);
5858
+ const met = tone === "met" /* Met */;
5859
+ React37.useEffect(() => {
5860
+ const turnedMet = met && !wasMet.current;
5861
+ wasMet.current = met;
5862
+ if (!turnedMet || reduced) return;
5863
+ scale.setValue(POP_FROM);
5864
+ reactNative.Animated.sequence([
5865
+ reactNative.Animated.timing(scale, { toValue: POP_PEAK, duration: POP_UP_MS, easing: reactNative.Easing.out(reactNative.Easing.cubic), useNativeDriver: false }),
5866
+ reactNative.Animated.timing(scale, { toValue: 1, duration: POP_SETTLE_MS, easing: reactNative.Easing.out(reactNative.Easing.cubic), useNativeDriver: false })
5867
+ ]).start();
5868
+ }, [met, reduced, scale]);
5869
+ const color = colors.mark[tone];
5870
+ const shape = met ? { backgroundColor: color } : [styles38.ring, { borderColor: color }];
5871
+ return /* @__PURE__ */ jsxRuntime.jsx(reactNative.Animated.View, { style: { transform: [{ scale }] }, children: /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: [styles38.mark, shape], children: met ? /* @__PURE__ */ jsxRuntime.jsx(uiIcons.SvgIcon, { color: colors.onMark, name: "check", size: CHECK_SIZE }) : null }) });
5872
+ }
5873
+ var GAP8 = 6;
5874
+ var ROW_GAP8 = 8;
5875
+ var FONT5 = 14;
5876
+ var LINE = 20;
5877
+ var METER_HEIGHT = 4;
5878
+ var RADIUS3 = 999;
5879
+ var CHIP_PAD_V = 4;
5880
+ var CHIP_PAD_H = 10;
5881
+ var CHIP_BORDER = 1;
5882
+ var HALF4 = "50%";
5883
+ var styles39 = reactNative.StyleSheet.create({
5884
+ column: { gap: GAP8 },
5885
+ row: { flexDirection: "row", alignItems: "center", gap: ROW_GAP8 },
5886
+ text: { fontSize: FONT5, lineHeight: LINE, flexShrink: 1 },
5887
+ meter: { flexDirection: "row", gap: GAP8 },
5888
+ segment: { flex: 1, height: METER_HEIGHT, borderRadius: RADIUS3 },
5889
+ grid: { flexDirection: "row", flexWrap: "wrap", rowGap: GAP8 },
5890
+ cell: { flexBasis: HALF4, maxWidth: HALF4, paddingRight: GAP8 },
5891
+ chips: { flexDirection: "row", flexWrap: "wrap", gap: GAP8 },
5892
+ chip: { borderRadius: RADIUS3, borderWidth: CHIP_BORDER, paddingVertical: CHIP_PAD_V, paddingHorizontal: CHIP_PAD_H },
5893
+ metPhrase: { fontWeight: "700", textDecorationLine: "underline" }
5894
+ });
5895
+ function RuleList({ rules, flagUnmet, colors, testID, listStyle, itemStyle, outlined = false }) {
5896
+ return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { role: "list", style: listStyle, testID, children: rules.map((rule) => {
5897
+ const tone = ruleTone(rule.met, flagUnmet);
5898
+ const outline = outlined ? { borderColor: colors.mark[tone] } : null;
5899
+ return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { role: "listitem", style: [styles39.row, itemStyle, outline], testID: `${testID}-${rule.id}`, ...{ dataSet: { tone } }, children: [
5900
+ /* @__PURE__ */ jsxRuntime.jsx(RuleMark, { colors, tone }),
5901
+ /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: [styles39.text, { color: colors.text[tone] }], children: rule.label })
5902
+ ] }, rule.id);
5903
+ }) });
5904
+ }
5905
+ function RulesChecklist(props) {
5906
+ return /* @__PURE__ */ jsxRuntime.jsx(RuleList, { ...props, listStyle: styles39.column });
5907
+ }
5908
+ function RulesChips(props) {
5909
+ return /* @__PURE__ */ jsxRuntime.jsx(RuleList, { ...props, outlined: true, itemStyle: styles39.chip, listStyle: styles39.chips });
5910
+ }
5911
+ function RulesMeterGrid(props) {
5912
+ const { rules, flagUnmet, colors, testID } = props;
5913
+ const metCount = rules.filter((rule) => rule.met).length;
5914
+ const unfilled = flagUnmet ? colors.mark.refused : colors.track;
5915
+ return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { style: styles39.column, children: [
5916
+ /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: styles39.meter, testID: `${testID}-meter`, children: rules.map((rule, index) => /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: [styles39.segment, { backgroundColor: index < metCount ? colors.mark.met : unfilled }] }, rule.id)) }),
5917
+ /* @__PURE__ */ jsxRuntime.jsx(RuleList, { ...props, itemStyle: styles39.cell, listStyle: styles39.grid })
5918
+ ] });
5919
+ }
5920
+ function RulesSentence({ rules, flagUnmet, colors, testID }) {
5921
+ return /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: [styles39.text, { color: colors.text.unmet }], testID, children: rules.map((rule, index) => {
5922
+ const tone = ruleTone(rule.met, flagUnmet);
5923
+ return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.Text, { style: [{ color: colors.text[tone] }, rule.met ? styles39.metPhrase : null], testID: `${testID}-${rule.id}`, ...{ dataSet: { tone } }, children: [
5924
+ index > 0 ? ", " : "",
5925
+ rule.label
5926
+ ] }, rule.id);
5927
+ }) });
5928
+ }
5929
+ function PasswordRules({ display, ...variant }) {
5930
+ switch (display) {
5931
+ case "checklist" /* Checklist */:
5932
+ return /* @__PURE__ */ jsxRuntime.jsx(RulesChecklist, { ...variant });
5933
+ case "chips" /* Chips */:
5934
+ return /* @__PURE__ */ jsxRuntime.jsx(RulesChips, { ...variant });
5935
+ case "sentence" /* Sentence */:
5936
+ return /* @__PURE__ */ jsxRuntime.jsx(RulesSentence, { ...variant });
5937
+ default:
5938
+ return /* @__PURE__ */ jsxRuntime.jsx(RulesMeterGrid, { ...variant });
5939
+ }
5940
+ }
5941
+ var ICON_SIZE = 20;
5942
+ var styles40 = reactNative.StyleSheet.create({
5943
+ button: { width: a11y.MIN_TOUCH_TARGET_PX, height: a11y.MIN_TOUCH_TARGET_PX, alignItems: "center", justifyContent: "center" }
5944
+ });
5945
+ function RevealToggle({ revealed, onToggle, onPressIn, showLabel, hideLabel, color, testID }) {
5946
+ const label = revealed ? hideLabel : showLabel;
5947
+ return /* @__PURE__ */ jsxRuntime.jsx(
5948
+ reactNative.Pressable,
5949
+ {
5950
+ accessibilityHint: label,
5951
+ accessibilityLabel: label,
5952
+ accessibilityRole: "button",
5953
+ "aria-pressed": revealed,
5954
+ style: styles40.button,
5955
+ testID,
5956
+ onPress: onToggle,
5957
+ onPressIn,
5958
+ children: /* @__PURE__ */ jsxRuntime.jsx(uiIcons.SvgIcon, { color, name: revealed ? "eyeOff" : "eye", size: ICON_SIZE })
5959
+ }
5960
+ );
5961
+ }
5962
+
5963
+ // src/PasswordField/ruleAnnouncement.ts
5964
+ function changedRules(previous, next) {
5965
+ const before = new Map(previous.map((rule) => [rule.id, rule.met]));
5966
+ return next.filter((rule) => before.has(rule.id) && before.get(rule.id) !== rule.met);
5967
+ }
5968
+ function ruleAnnouncement(changed, labels) {
5969
+ return changed.map((rule) => `${rule.label}: ${rule.met ? labels.met : labels.unmet}`).join(". ");
5970
+ }
5971
+
5972
+ // src/PasswordField/usePasswordRuleAnnouncement.ts
5973
+ function usePasswordRuleAnnouncement(rules, labels) {
5974
+ const previous = React37.useRef(rules);
5975
+ const [message, setMessage] = React37.useState("");
5976
+ React37.useEffect(() => {
5977
+ const changed = changedRules(previous.current, rules);
5978
+ previous.current = rules;
5979
+ if (changed.length > 0) setMessage(ruleAnnouncement(changed, labels));
5980
+ }, [rules, labels]);
5981
+ return message;
5982
+ }
5983
+ function ruleColorsFrom(palette) {
5984
+ return {
5985
+ mark: { ["met" /* Met */]: palette.success, ["unmet" /* Unmet */]: palette.textSecondary, ["refused" /* Refused */]: palette.error },
5986
+ text: { ["met" /* Met */]: palette.text, ["unmet" /* Unmet */]: palette.textSecondary, ["refused" /* Refused */]: palette.error },
5987
+ track: palette.border,
5988
+ onMark: palette.background
5989
+ };
5990
+ }
5991
+ function useRuleColors(override) {
5992
+ const { theme } = uiFeedback.useUi();
5993
+ return React37.useMemo(() => {
5994
+ if (override !== void 0) return ruleColorsFrom(override);
5995
+ return ruleColorsFrom({
5996
+ success: theme.semantic.success?.["500"] ?? theme.colors.text,
5997
+ error: theme.semantic.error["500"],
5998
+ text: theme.colors.text,
5999
+ textSecondary: theme.colors.textSecondary,
6000
+ border: theme.colors.border,
6001
+ background: theme.colors.background
6002
+ });
6003
+ }, [theme, override]);
6004
+ }
6005
+ var RULES_GAP = 8;
6006
+ var TOGGLE_INSET = 48;
6007
+ var NO_RULES = [];
6008
+ var styles41 = reactNative.StyleSheet.create({
6009
+ inputRow: { position: "relative", justifyContent: "center" },
6010
+ input: { paddingRight: TOGGLE_INSET },
6011
+ toggle: { position: "absolute", right: 0, top: 0, bottom: 0, justifyContent: "center" },
6012
+ rules: { marginTop: RULES_GAP },
6013
+ live: { position: "absolute", width: 1, height: 1, overflow: "hidden", opacity: 0 }
6014
+ });
6015
+ function joinIds(...ids) {
6016
+ const joined = ids.filter((id) => id !== void 0 && id !== "").join(" ");
6017
+ return joined === "" ? void 0 : joined;
6018
+ }
6019
+ function useCaretKeepingToggle(inputRef, initialLength) {
6020
+ const [revealed, setRevealed] = React37.useState(false);
6021
+ const selection2 = React37.useRef({ start: initialLength, end: initialLength });
6022
+ const hadFocus = React37.useRef(false);
6023
+ const onSelectionChange = React37.useCallback((event) => {
6024
+ selection2.current = event.nativeEvent.selection;
6025
+ }, []);
6026
+ const onPressIn = React37.useCallback(() => {
6027
+ hadFocus.current = inputRef.current?.isFocused?.() ?? false;
6028
+ }, [inputRef]);
6029
+ const onToggle = React37.useCallback(() => {
6030
+ setRevealed((current) => !current);
6031
+ if (!hadFocus.current) return;
6032
+ const node = inputRef.current;
6033
+ const { start, end } = selection2.current;
6034
+ requestAnimationFrame(() => {
6035
+ node?.focus();
6036
+ node?.setSelectionRange?.(start, end);
6037
+ });
6038
+ }, [inputRef]);
6039
+ return { revealed, onPressIn, onToggle, onSelectionChange };
6040
+ }
6041
+ var PasswordField = React37__default.default.forwardRef(function PasswordField2({ label, value, onChangeText, labels, testID, rules = NO_RULES, rulesDisplay = "meterGrid" /* MeterGrid */, flagUnmet = false, error, colors: colorOverride, inputProps }, ref) {
6042
+ const inputRef = React37.useRef(null);
6043
+ React37.useImperativeHandle(ref, () => inputRef.current);
6044
+ const toggle2 = useCaretKeepingToggle(inputRef, value.length);
6045
+ const colors = useRuleColors(colorOverride);
6046
+ const announcement = usePasswordRuleAnnouncement(rules, labels);
6047
+ const rulesId = `${testID}-rules`;
6048
+ const hasRules = rules.length > 0;
6049
+ return /* @__PURE__ */ jsxRuntime.jsx(Field, { error, label, testID: `${testID}-field`, children: (context) => /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { children: [
6050
+ /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { style: styles41.inputRow, children: [
6051
+ /* @__PURE__ */ jsxRuntime.jsx(
6052
+ ThemedTextInput,
6053
+ {
6054
+ autoCapitalize: "none",
6055
+ autoCorrect: false,
6056
+ ...inputProps,
6057
+ ref: inputRef,
6058
+ describedById: joinIds(context.describedById, hasRules ? rulesId : void 0),
6059
+ hasError: context.hasError || flagUnmet && rules.some((rule) => !rule.met),
6060
+ id: context.controlId,
6061
+ secureTextEntry: !toggle2.revealed,
6062
+ style: [styles41.input, inputProps?.style],
6063
+ testID,
6064
+ value,
6065
+ onChangeText,
6066
+ onSelectionChange: toggle2.onSelectionChange
6067
+ }
6068
+ ),
6069
+ /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: styles41.toggle, children: /* @__PURE__ */ jsxRuntime.jsx(RevealToggle, { color: colors.text.unmet, hideLabel: labels.hide, revealed: toggle2.revealed, showLabel: labels.show, testID: `${testID}-reveal`, onPressIn: toggle2.onPressIn, onToggle: toggle2.onToggle }) })
6070
+ ] }),
6071
+ hasRules ? /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { nativeID: rulesId, style: styles41.rules, children: [
6072
+ /* @__PURE__ */ jsxRuntime.jsx(PasswordRules, { colors, display: rulesDisplay, flagUnmet, rules, testID: rulesId }),
6073
+ /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { "aria-live": "polite", accessibilityLiveRegion: "polite", style: styles41.live, testID: `${testID}-announce`, children: announcement })
6074
+ ] }) : null
6075
+ ] }) });
6076
+ });
6077
+
5756
6078
  exports.ActionBar = ActionBar;
5757
6079
  exports.AddOnRequest = AddOnRequest;
5758
6080
  exports.AlignToControl = AlignToControl;
@@ -5802,9 +6124,14 @@ exports.InlineFieldRow = InlineFieldRow;
5802
6124
  exports.LINE_ITEMS_WIDE_MIN_WIDTH = LINE_ITEMS_WIDE_MIN_WIDTH;
5803
6125
  exports.LineItems = LineItems;
5804
6126
  exports.PHONE_MAX_WIDTH = PHONE_MAX_WIDTH;
6127
+ exports.PasswordField = PasswordField;
6128
+ exports.PasswordRules = PasswordRules;
6129
+ exports.PasswordRulesDisplay = PasswordRulesDisplay;
5805
6130
  exports.RESPONSIVE_FORM_DEFAULT_BREAKPOINT = RESPONSIVE_FORM_DEFAULT_BREAKPOINT;
5806
6131
  exports.RadioList = RadioList;
5807
6132
  exports.ResponsiveFormLayout = ResponsiveFormLayout;
6133
+ exports.RevealToggle = RevealToggle;
6134
+ exports.RuleTone = RuleTone;
5808
6135
  exports.SEGMENT_BESIDE_FIELD = SEGMENT_BESIDE_FIELD;
5809
6136
  exports.SEGMENT_BESIDE_TEXT_INPUT = SEGMENT_BESIDE_TEXT_INPUT;
5810
6137
  exports.SearchSelect = SearchSelect;
@@ -5817,8 +6144,10 @@ exports.ThemedTextInput = ThemedTextInput;
5817
6144
  exports.TypeaheadControl = TypeaheadControl;
5818
6145
  exports.UploadWithLinkField = UploadWithLinkField;
5819
6146
  exports.actionBarPosition = actionBarPosition;
6147
+ exports.assignServerErrors = assignServerErrors;
5820
6148
  exports.buildSearchSelectSections = buildSearchSelectSections;
5821
6149
  exports.canRemoveLine = canRemoveLine;
6150
+ exports.changedRules = changedRules;
5822
6151
  exports.controlStyles = controlStyles;
5823
6152
  exports.countryFieldOptions = countryFieldOptions;
5824
6153
  exports.countryFlag = countryFlag;
@@ -5856,6 +6185,9 @@ exports.resolveGroupSelection = resolveGroupSelection;
5856
6185
  exports.resolveSelectMotion = resolveSelectMotion;
5857
6186
  exports.resolveStepNeighbours = resolveStepNeighbours;
5858
6187
  exports.rowsForText = rowsForText;
6188
+ exports.ruleAnnouncement = ruleAnnouncement;
6189
+ exports.ruleColorsFrom = ruleColorsFrom;
6190
+ exports.ruleTone = ruleTone;
5859
6191
  exports.suggestOptions = suggestOptions;
5860
6192
  exports.toCountryValue = toCountryValue;
5861
6193
  exports.toggleGroupSelection = toggleGroupSelection;
@@ -5868,6 +6200,7 @@ exports.useOutsidePress = useOutsidePress;
5868
6200
  exports.useRecentValues = useRecentValues;
5869
6201
  exports.useReducedMotion = useReducedMotion;
5870
6202
  exports.useSearchSelectState = useSearchSelectState;
6203
+ exports.useServerFieldErrors = useServerFieldErrors;
5871
6204
  exports.useThemedInput = useThemedInput;
5872
6205
  exports.validateBic = validateBic;
5873
6206
  exports.validateDecision = validateDecision;