@react-stately/checkbox 3.5.2-nightly.4230 → 3.5.2-nightly.4242

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/import.mjs CHANGED
@@ -1,4 +1,6 @@
1
+ import {useFormValidationState as $b3nuO$useFormValidationState, mergeValidation as $b3nuO$mergeValidation} from "@react-stately/form";
1
2
  import {useControlledState as $b3nuO$useControlledState} from "@react-stately/utils";
3
+ import {useRef as $b3nuO$useRef} from "react";
2
4
 
3
5
  /*
4
6
  * Copyright 2020 Adobe. All rights reserved.
@@ -21,9 +23,20 @@ import {useControlledState as $b3nuO$useControlledState} from "@react-stately/ut
21
23
  * OF ANY KIND, either express or implied. See the License for the specific language
22
24
  * governing permissions and limitations under the License.
23
25
  */
26
+
27
+
24
28
  function $587d3ad58be6d31f$export$daff6da51032a415(props = {}) {
25
29
  let [selectedValues, setValue] = (0, $b3nuO$useControlledState)(props.value, props.defaultValue || [], props.onChange);
30
+ let isRequired = props.isRequired && selectedValues.length === 0;
31
+ let invalidValues = (0, $b3nuO$useRef)(new Map());
32
+ let validation = (0, $b3nuO$useFormValidationState)({
33
+ ...props,
34
+ value: selectedValues
35
+ });
36
+ let isInvalid = validation.displayValidation.isInvalid;
37
+ var _props_validationState;
26
38
  const state = {
39
+ ...validation,
27
40
  value: selectedValues,
28
41
  setValue (value) {
29
42
  if (props.isReadOnly || props.isDisabled) return;
@@ -47,8 +60,16 @@ function $587d3ad58be6d31f$export$daff6da51032a415(props = {}) {
47
60
  if (selectedValues.includes(value)) setValue(selectedValues.filter((existingValue)=>existingValue !== value));
48
61
  else setValue(selectedValues.concat(value));
49
62
  },
50
- validationState: props.validationState,
51
- isInvalid: props.isInvalid || props.validationState === "invalid"
63
+ setInvalid (value, v) {
64
+ let s = new Map(invalidValues.current);
65
+ if (v.isInvalid) s.set(value, v);
66
+ else s.delete(value);
67
+ invalidValues.current = s;
68
+ validation.updateValidation((0, $b3nuO$mergeValidation)(...s.values()));
69
+ },
70
+ validationState: (_props_validationState = props.validationState) !== null && _props_validationState !== void 0 ? _props_validationState : isInvalid ? "invalid" : null,
71
+ isInvalid: isInvalid,
72
+ isRequired: isRequired
52
73
  };
53
74
  return state;
54
75
  }
package/dist/main.js CHANGED
@@ -1,4 +1,7 @@
1
+ var $jVLCf$reactstatelyform = require("@react-stately/form");
1
2
  var $jVLCf$reactstatelyutils = require("@react-stately/utils");
3
+ var $jVLCf$react = require("react");
4
+
2
5
 
3
6
  function $parcel$export(e, n, v, s) {
4
7
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
@@ -26,9 +29,20 @@ $parcel$export(module.exports, "useCheckboxGroupState", () => $943f4c622056df75$
26
29
  * OF ANY KIND, either express or implied. See the License for the specific language
27
30
  * governing permissions and limitations under the License.
28
31
  */
32
+
33
+
29
34
  function $943f4c622056df75$export$daff6da51032a415(props = {}) {
30
35
  let [selectedValues, setValue] = (0, $jVLCf$reactstatelyutils.useControlledState)(props.value, props.defaultValue || [], props.onChange);
36
+ let isRequired = props.isRequired && selectedValues.length === 0;
37
+ let invalidValues = (0, $jVLCf$react.useRef)(new Map());
38
+ let validation = (0, $jVLCf$reactstatelyform.useFormValidationState)({
39
+ ...props,
40
+ value: selectedValues
41
+ });
42
+ let isInvalid = validation.displayValidation.isInvalid;
43
+ var _props_validationState;
31
44
  const state = {
45
+ ...validation,
32
46
  value: selectedValues,
33
47
  setValue (value) {
34
48
  if (props.isReadOnly || props.isDisabled) return;
@@ -52,8 +66,16 @@ function $943f4c622056df75$export$daff6da51032a415(props = {}) {
52
66
  if (selectedValues.includes(value)) setValue(selectedValues.filter((existingValue)=>existingValue !== value));
53
67
  else setValue(selectedValues.concat(value));
54
68
  },
55
- validationState: props.validationState,
56
- isInvalid: props.isInvalid || props.validationState === "invalid"
69
+ setInvalid (value, v) {
70
+ let s = new Map(invalidValues.current);
71
+ if (v.isInvalid) s.set(value, v);
72
+ else s.delete(value);
73
+ invalidValues.current = s;
74
+ validation.updateValidation((0, $jVLCf$reactstatelyform.mergeValidation)(...s.values()));
75
+ },
76
+ validationState: (_props_validationState = props.validationState) !== null && _props_validationState !== void 0 ? _props_validationState : isInvalid ? "invalid" : null,
77
+ isInvalid: isInvalid,
78
+ isRequired: isRequired
57
79
  };
58
80
  return state;
59
81
  }
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;AA6CM,SAAS,0CAAsB,QAA4B,CAAC,CAAC;IAClE,IAAI,CAAC,gBAAgB,SAAS,GAAG,CAAA,GAAA,2CAAiB,EAAE,MAAM,OAAO,MAAM,gBAAgB,EAAE,EAAE,MAAM;IAEjG,MAAM,QAA4B;QAChC,OAAO;QACP,UAAS,KAAK;YACZ,IAAI,MAAM,cAAc,MAAM,YAC5B;YAGF,SAAS;QACX;QACA,YAAY,MAAM,cAAc;QAChC,YAAY,MAAM,cAAc;QAChC,YAAW,KAAK;YACd,OAAO,eAAe,SAAS;QACjC;QACA,UAAS,KAAK;YACZ,IAAI,MAAM,cAAc,MAAM,YAC5B;YAEF,IAAI,CAAC,eAAe,SAAS,QAC3B,SAAS,eAAe,OAAO;QAEnC;QACA,aAAY,KAAK;YACf,IAAI,MAAM,cAAc,MAAM,YAC5B;YAEF,IAAI,eAAe,SAAS,QAC1B,SAAS,eAAe,OAAO,CAAA,gBAAiB,kBAAkB;QAEtE;QACA,aAAY,KAAK;YACf,IAAI,MAAM,cAAc,MAAM,YAC5B;YAEF,IAAI,eAAe,SAAS,QAC1B,SAAS,eAAe,OAAO,CAAA,gBAAiB,kBAAkB;iBAElE,SAAS,eAAe,OAAO;QAEnC;QACA,iBAAiB,MAAM;QACvB,WAAW,MAAM,aAAa,MAAM,oBAAoB;IAC1D;IAEA,OAAO;AACT;;CD7FC","sources":["packages/@react-stately/checkbox/src/index.ts","packages/@react-stately/checkbox/src/useCheckboxGroupState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useCheckboxGroupState} from './useCheckboxGroupState';\n\nexport type {CheckboxGroupProps} from '@react-types/checkbox';\nexport type {CheckboxGroupState} from './useCheckboxGroupState';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {CheckboxGroupProps} from '@react-types/checkbox';\nimport {useControlledState} from '@react-stately/utils';\nimport {ValidationState} from '@react-types/shared';\n\nexport interface CheckboxGroupState {\n /** Current selected values. */\n readonly value: readonly string[],\n\n /** Whether the checkbox group is disabled. */\n readonly isDisabled: boolean,\n\n /** Whether the checkbox group is read only. */\n readonly isReadOnly: boolean,\n\n /**\n * The current validation state of the checkbox group.\n * @deprecated Use `isInvalid` instead.\n */\n readonly validationState: ValidationState,\n\n /** Whether the checkbox group is invalid. */\n readonly isInvalid: boolean,\n\n /** Returns whether the given value is selected. */\n isSelected(value: string): boolean,\n\n /** Sets the selected values. */\n setValue(value: string[]): void,\n\n /** Adds a value to the set of selected values. */\n addValue(value: string): void,\n\n /** Removes a value from the set of selected values. */\n removeValue(value: string): void,\n\n /** Toggles a value in the set of selected values. */\n toggleValue(value: string): void\n}\n\n/**\n * Provides state management for a checkbox group component. Provides a name for the group,\n * and manages selection and focus state.\n */\nexport function useCheckboxGroupState(props: CheckboxGroupProps = {}): CheckboxGroupState {\n let [selectedValues, setValue] = useControlledState(props.value, props.defaultValue || [], props.onChange);\n\n const state: CheckboxGroupState = {\n value: selectedValues,\n setValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n\n setValue(value);\n },\n isDisabled: props.isDisabled || false,\n isReadOnly: props.isReadOnly || false,\n isSelected(value) {\n return selectedValues.includes(value);\n },\n addValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n if (!selectedValues.includes(value)) {\n setValue(selectedValues.concat(value));\n }\n },\n removeValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n if (selectedValues.includes(value)) {\n setValue(selectedValues.filter(existingValue => existingValue !== value));\n }\n },\n toggleValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n if (selectedValues.includes(value)) {\n setValue(selectedValues.filter(existingValue => existingValue !== value));\n } else {\n setValue(selectedValues.concat(value));\n }\n },\n validationState: props.validationState,\n isInvalid: props.isInvalid || props.validationState === 'invalid'\n };\n\n return state;\n}\n"],"names":[],"version":3,"file":"main.js.map"}
1
+ {"mappings":";;;;;;;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;;;AAwDM,SAAS,0CAAsB,QAA4B,CAAC,CAAC;IAClE,IAAI,CAAC,gBAAgB,SAAS,GAAG,CAAA,GAAA,2CAAiB,EAAE,MAAM,KAAK,EAAE,MAAM,YAAY,IAAI,EAAE,EAAE,MAAM,QAAQ;IACzG,IAAI,aAAa,MAAM,UAAU,IAAI,eAAe,MAAM,KAAK;IAE/D,IAAI,gBAAgB,CAAA,GAAA,mBAAK,EAAE,IAAI;IAC/B,IAAI,aAAa,CAAA,GAAA,8CAAqB,EAAE;QACtC,GAAG,KAAK;QACR,OAAO;IACT;IAEA,IAAI,YAAY,WAAW,iBAAiB,CAAC,SAAS;QAqDnC;IApDnB,MAAM,QAA4B;QAChC,GAAG,UAAU;QACb,OAAO;QACP,UAAS,KAAK;YACZ,IAAI,MAAM,UAAU,IAAI,MAAM,UAAU,EACtC;YAGF,SAAS;QACX;QACA,YAAY,MAAM,UAAU,IAAI;QAChC,YAAY,MAAM,UAAU,IAAI;QAChC,YAAW,KAAK;YACd,OAAO,eAAe,QAAQ,CAAC;QACjC;QACA,UAAS,KAAK;YACZ,IAAI,MAAM,UAAU,IAAI,MAAM,UAAU,EACtC;YAEF,IAAI,CAAC,eAAe,QAAQ,CAAC,QAC3B,SAAS,eAAe,MAAM,CAAC;QAEnC;QACA,aAAY,KAAK;YACf,IAAI,MAAM,UAAU,IAAI,MAAM,UAAU,EACtC;YAEF,IAAI,eAAe,QAAQ,CAAC,QAC1B,SAAS,eAAe,MAAM,CAAC,CAAA,gBAAiB,kBAAkB;QAEtE;QACA,aAAY,KAAK;YACf,IAAI,MAAM,UAAU,IAAI,MAAM,UAAU,EACtC;YAEF,IAAI,eAAe,QAAQ,CAAC,QAC1B,SAAS,eAAe,MAAM,CAAC,CAAA,gBAAiB,kBAAkB;iBAElE,SAAS,eAAe,MAAM,CAAC;QAEnC;QACA,YAAW,KAAK,EAAE,CAAC;YACjB,IAAI,IAAI,IAAI,IAAI,cAAc,OAAO;YACrC,IAAI,EAAE,SAAS,EACb,EAAE,GAAG,CAAC,OAAO;iBAEb,EAAE,MAAM,CAAC;YAGX,cAAc,OAAO,GAAG;YACxB,WAAW,gBAAgB,CAAC,CAAA,GAAA,uCAAc,KAAK,EAAE,MAAM;QACzD;QACA,iBAAiB,CAAA,yBAAA,MAAM,eAAe,cAArB,oCAAA,yBAA0B,YAAY,YAAY;mBACnE;oBACA;IACF;IAEA,OAAO;AACT;;CD7HC","sources":["packages/@react-stately/checkbox/src/index.ts","packages/@react-stately/checkbox/src/useCheckboxGroupState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useCheckboxGroupState} from './useCheckboxGroupState';\n\nexport type {CheckboxGroupProps} from '@react-types/checkbox';\nexport type {CheckboxGroupState} from './useCheckboxGroupState';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {CheckboxGroupProps} from '@react-types/checkbox';\nimport {FormValidationState, mergeValidation, useFormValidationState} from '@react-stately/form';\nimport {useControlledState} from '@react-stately/utils';\nimport {useRef} from 'react';\nimport {ValidationResult, ValidationState} from '@react-types/shared';\n\nexport interface CheckboxGroupState extends FormValidationState {\n /** Current selected values. */\n readonly value: readonly string[],\n\n /** Whether the checkbox group is disabled. */\n readonly isDisabled: boolean,\n\n /** Whether the checkbox group is read only. */\n readonly isReadOnly: boolean,\n\n /**\n * The current validation state of the checkbox group.\n * @deprecated Use `isInvalid` instead.\n */\n readonly validationState: ValidationState | null,\n\n /** Whether the checkbox group is invalid. */\n readonly isInvalid: boolean,\n\n /**\n * Whether the checkboxes in the group are required.\n * This changes to false once at least one item is selected.\n */\n readonly isRequired: boolean,\n\n /** Returns whether the given value is selected. */\n isSelected(value: string): boolean,\n\n /** Sets the selected values. */\n setValue(value: string[]): void,\n\n /** Adds a value to the set of selected values. */\n addValue(value: string): void,\n\n /** Removes a value from the set of selected values. */\n removeValue(value: string): void,\n\n /** Toggles a value in the set of selected values. */\n toggleValue(value: string): void,\n\n /** Sets whether one of the checkboxes is invalid. */\n setInvalid(value: string, validation: ValidationResult): void\n}\n\n/**\n * Provides state management for a checkbox group component. Provides a name for the group,\n * and manages selection and focus state.\n */\nexport function useCheckboxGroupState(props: CheckboxGroupProps = {}): CheckboxGroupState {\n let [selectedValues, setValue] = useControlledState(props.value, props.defaultValue || [], props.onChange);\n let isRequired = props.isRequired && selectedValues.length === 0;\n\n let invalidValues = useRef(new Map<string, ValidationResult>());\n let validation = useFormValidationState({\n ...props,\n value: selectedValues\n });\n\n let isInvalid = validation.displayValidation.isInvalid;\n const state: CheckboxGroupState = {\n ...validation,\n value: selectedValues,\n setValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n\n setValue(value);\n },\n isDisabled: props.isDisabled || false,\n isReadOnly: props.isReadOnly || false,\n isSelected(value) {\n return selectedValues.includes(value);\n },\n addValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n if (!selectedValues.includes(value)) {\n setValue(selectedValues.concat(value));\n }\n },\n removeValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n if (selectedValues.includes(value)) {\n setValue(selectedValues.filter(existingValue => existingValue !== value));\n }\n },\n toggleValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n if (selectedValues.includes(value)) {\n setValue(selectedValues.filter(existingValue => existingValue !== value));\n } else {\n setValue(selectedValues.concat(value));\n }\n },\n setInvalid(value, v) {\n let s = new Map(invalidValues.current);\n if (v.isInvalid) {\n s.set(value, v);\n } else {\n s.delete(value);\n }\n\n invalidValues.current = s;\n validation.updateValidation(mergeValidation(...s.values()));\n },\n validationState: props.validationState ?? (isInvalid ? 'invalid' : null),\n isInvalid,\n isRequired\n };\n\n return state;\n}\n"],"names":[],"version":3,"file":"main.js.map"}
package/dist/module.js CHANGED
@@ -1,4 +1,6 @@
1
+ import {useFormValidationState as $b3nuO$useFormValidationState, mergeValidation as $b3nuO$mergeValidation} from "@react-stately/form";
1
2
  import {useControlledState as $b3nuO$useControlledState} from "@react-stately/utils";
3
+ import {useRef as $b3nuO$useRef} from "react";
2
4
 
3
5
  /*
4
6
  * Copyright 2020 Adobe. All rights reserved.
@@ -21,9 +23,20 @@ import {useControlledState as $b3nuO$useControlledState} from "@react-stately/ut
21
23
  * OF ANY KIND, either express or implied. See the License for the specific language
22
24
  * governing permissions and limitations under the License.
23
25
  */
26
+
27
+
24
28
  function $587d3ad58be6d31f$export$daff6da51032a415(props = {}) {
25
29
  let [selectedValues, setValue] = (0, $b3nuO$useControlledState)(props.value, props.defaultValue || [], props.onChange);
30
+ let isRequired = props.isRequired && selectedValues.length === 0;
31
+ let invalidValues = (0, $b3nuO$useRef)(new Map());
32
+ let validation = (0, $b3nuO$useFormValidationState)({
33
+ ...props,
34
+ value: selectedValues
35
+ });
36
+ let isInvalid = validation.displayValidation.isInvalid;
37
+ var _props_validationState;
26
38
  const state = {
39
+ ...validation,
27
40
  value: selectedValues,
28
41
  setValue (value) {
29
42
  if (props.isReadOnly || props.isDisabled) return;
@@ -47,8 +60,16 @@ function $587d3ad58be6d31f$export$daff6da51032a415(props = {}) {
47
60
  if (selectedValues.includes(value)) setValue(selectedValues.filter((existingValue)=>existingValue !== value));
48
61
  else setValue(selectedValues.concat(value));
49
62
  },
50
- validationState: props.validationState,
51
- isInvalid: props.isInvalid || props.validationState === "invalid"
63
+ setInvalid (value, v) {
64
+ let s = new Map(invalidValues.current);
65
+ if (v.isInvalid) s.set(value, v);
66
+ else s.delete(value);
67
+ invalidValues.current = s;
68
+ validation.updateValidation((0, $b3nuO$mergeValidation)(...s.values()));
69
+ },
70
+ validationState: (_props_validationState = props.validationState) !== null && _props_validationState !== void 0 ? _props_validationState : isInvalid ? "invalid" : null,
71
+ isInvalid: isInvalid,
72
+ isRequired: isRequired
52
73
  };
53
74
  return state;
54
75
  }
@@ -1 +1 @@
1
- {"mappings":";;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;AA6CM,SAAS,0CAAsB,QAA4B,CAAC,CAAC;IAClE,IAAI,CAAC,gBAAgB,SAAS,GAAG,CAAA,GAAA,yBAAiB,EAAE,MAAM,OAAO,MAAM,gBAAgB,EAAE,EAAE,MAAM;IAEjG,MAAM,QAA4B;QAChC,OAAO;QACP,UAAS,KAAK;YACZ,IAAI,MAAM,cAAc,MAAM,YAC5B;YAGF,SAAS;QACX;QACA,YAAY,MAAM,cAAc;QAChC,YAAY,MAAM,cAAc;QAChC,YAAW,KAAK;YACd,OAAO,eAAe,SAAS;QACjC;QACA,UAAS,KAAK;YACZ,IAAI,MAAM,cAAc,MAAM,YAC5B;YAEF,IAAI,CAAC,eAAe,SAAS,QAC3B,SAAS,eAAe,OAAO;QAEnC;QACA,aAAY,KAAK;YACf,IAAI,MAAM,cAAc,MAAM,YAC5B;YAEF,IAAI,eAAe,SAAS,QAC1B,SAAS,eAAe,OAAO,CAAA,gBAAiB,kBAAkB;QAEtE;QACA,aAAY,KAAK;YACf,IAAI,MAAM,cAAc,MAAM,YAC5B;YAEF,IAAI,eAAe,SAAS,QAC1B,SAAS,eAAe,OAAO,CAAA,gBAAiB,kBAAkB;iBAElE,SAAS,eAAe,OAAO;QAEnC;QACA,iBAAiB,MAAM;QACvB,WAAW,MAAM,aAAa,MAAM,oBAAoB;IAC1D;IAEA,OAAO;AACT;;CD7FC","sources":["packages/@react-stately/checkbox/src/index.ts","packages/@react-stately/checkbox/src/useCheckboxGroupState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useCheckboxGroupState} from './useCheckboxGroupState';\n\nexport type {CheckboxGroupProps} from '@react-types/checkbox';\nexport type {CheckboxGroupState} from './useCheckboxGroupState';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {CheckboxGroupProps} from '@react-types/checkbox';\nimport {useControlledState} from '@react-stately/utils';\nimport {ValidationState} from '@react-types/shared';\n\nexport interface CheckboxGroupState {\n /** Current selected values. */\n readonly value: readonly string[],\n\n /** Whether the checkbox group is disabled. */\n readonly isDisabled: boolean,\n\n /** Whether the checkbox group is read only. */\n readonly isReadOnly: boolean,\n\n /**\n * The current validation state of the checkbox group.\n * @deprecated Use `isInvalid` instead.\n */\n readonly validationState: ValidationState,\n\n /** Whether the checkbox group is invalid. */\n readonly isInvalid: boolean,\n\n /** Returns whether the given value is selected. */\n isSelected(value: string): boolean,\n\n /** Sets the selected values. */\n setValue(value: string[]): void,\n\n /** Adds a value to the set of selected values. */\n addValue(value: string): void,\n\n /** Removes a value from the set of selected values. */\n removeValue(value: string): void,\n\n /** Toggles a value in the set of selected values. */\n toggleValue(value: string): void\n}\n\n/**\n * Provides state management for a checkbox group component. Provides a name for the group,\n * and manages selection and focus state.\n */\nexport function useCheckboxGroupState(props: CheckboxGroupProps = {}): CheckboxGroupState {\n let [selectedValues, setValue] = useControlledState(props.value, props.defaultValue || [], props.onChange);\n\n const state: CheckboxGroupState = {\n value: selectedValues,\n setValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n\n setValue(value);\n },\n isDisabled: props.isDisabled || false,\n isReadOnly: props.isReadOnly || false,\n isSelected(value) {\n return selectedValues.includes(value);\n },\n addValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n if (!selectedValues.includes(value)) {\n setValue(selectedValues.concat(value));\n }\n },\n removeValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n if (selectedValues.includes(value)) {\n setValue(selectedValues.filter(existingValue => existingValue !== value));\n }\n },\n toggleValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n if (selectedValues.includes(value)) {\n setValue(selectedValues.filter(existingValue => existingValue !== value));\n } else {\n setValue(selectedValues.concat(value));\n }\n },\n validationState: props.validationState,\n isInvalid: props.isInvalid || props.validationState === 'invalid'\n };\n\n return state;\n}\n"],"names":[],"version":3,"file":"module.js.map"}
1
+ {"mappings":";;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;;;AAwDM,SAAS,0CAAsB,QAA4B,CAAC,CAAC;IAClE,IAAI,CAAC,gBAAgB,SAAS,GAAG,CAAA,GAAA,yBAAiB,EAAE,MAAM,KAAK,EAAE,MAAM,YAAY,IAAI,EAAE,EAAE,MAAM,QAAQ;IACzG,IAAI,aAAa,MAAM,UAAU,IAAI,eAAe,MAAM,KAAK;IAE/D,IAAI,gBAAgB,CAAA,GAAA,aAAK,EAAE,IAAI;IAC/B,IAAI,aAAa,CAAA,GAAA,6BAAqB,EAAE;QACtC,GAAG,KAAK;QACR,OAAO;IACT;IAEA,IAAI,YAAY,WAAW,iBAAiB,CAAC,SAAS;QAqDnC;IApDnB,MAAM,QAA4B;QAChC,GAAG,UAAU;QACb,OAAO;QACP,UAAS,KAAK;YACZ,IAAI,MAAM,UAAU,IAAI,MAAM,UAAU,EACtC;YAGF,SAAS;QACX;QACA,YAAY,MAAM,UAAU,IAAI;QAChC,YAAY,MAAM,UAAU,IAAI;QAChC,YAAW,KAAK;YACd,OAAO,eAAe,QAAQ,CAAC;QACjC;QACA,UAAS,KAAK;YACZ,IAAI,MAAM,UAAU,IAAI,MAAM,UAAU,EACtC;YAEF,IAAI,CAAC,eAAe,QAAQ,CAAC,QAC3B,SAAS,eAAe,MAAM,CAAC;QAEnC;QACA,aAAY,KAAK;YACf,IAAI,MAAM,UAAU,IAAI,MAAM,UAAU,EACtC;YAEF,IAAI,eAAe,QAAQ,CAAC,QAC1B,SAAS,eAAe,MAAM,CAAC,CAAA,gBAAiB,kBAAkB;QAEtE;QACA,aAAY,KAAK;YACf,IAAI,MAAM,UAAU,IAAI,MAAM,UAAU,EACtC;YAEF,IAAI,eAAe,QAAQ,CAAC,QAC1B,SAAS,eAAe,MAAM,CAAC,CAAA,gBAAiB,kBAAkB;iBAElE,SAAS,eAAe,MAAM,CAAC;QAEnC;QACA,YAAW,KAAK,EAAE,CAAC;YACjB,IAAI,IAAI,IAAI,IAAI,cAAc,OAAO;YACrC,IAAI,EAAE,SAAS,EACb,EAAE,GAAG,CAAC,OAAO;iBAEb,EAAE,MAAM,CAAC;YAGX,cAAc,OAAO,GAAG;YACxB,WAAW,gBAAgB,CAAC,CAAA,GAAA,sBAAc,KAAK,EAAE,MAAM;QACzD;QACA,iBAAiB,CAAA,yBAAA,MAAM,eAAe,cAArB,oCAAA,yBAA0B,YAAY,YAAY;mBACnE;oBACA;IACF;IAEA,OAAO;AACT;;CD7HC","sources":["packages/@react-stately/checkbox/src/index.ts","packages/@react-stately/checkbox/src/useCheckboxGroupState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useCheckboxGroupState} from './useCheckboxGroupState';\n\nexport type {CheckboxGroupProps} from '@react-types/checkbox';\nexport type {CheckboxGroupState} from './useCheckboxGroupState';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {CheckboxGroupProps} from '@react-types/checkbox';\nimport {FormValidationState, mergeValidation, useFormValidationState} from '@react-stately/form';\nimport {useControlledState} from '@react-stately/utils';\nimport {useRef} from 'react';\nimport {ValidationResult, ValidationState} from '@react-types/shared';\n\nexport interface CheckboxGroupState extends FormValidationState {\n /** Current selected values. */\n readonly value: readonly string[],\n\n /** Whether the checkbox group is disabled. */\n readonly isDisabled: boolean,\n\n /** Whether the checkbox group is read only. */\n readonly isReadOnly: boolean,\n\n /**\n * The current validation state of the checkbox group.\n * @deprecated Use `isInvalid` instead.\n */\n readonly validationState: ValidationState | null,\n\n /** Whether the checkbox group is invalid. */\n readonly isInvalid: boolean,\n\n /**\n * Whether the checkboxes in the group are required.\n * This changes to false once at least one item is selected.\n */\n readonly isRequired: boolean,\n\n /** Returns whether the given value is selected. */\n isSelected(value: string): boolean,\n\n /** Sets the selected values. */\n setValue(value: string[]): void,\n\n /** Adds a value to the set of selected values. */\n addValue(value: string): void,\n\n /** Removes a value from the set of selected values. */\n removeValue(value: string): void,\n\n /** Toggles a value in the set of selected values. */\n toggleValue(value: string): void,\n\n /** Sets whether one of the checkboxes is invalid. */\n setInvalid(value: string, validation: ValidationResult): void\n}\n\n/**\n * Provides state management for a checkbox group component. Provides a name for the group,\n * and manages selection and focus state.\n */\nexport function useCheckboxGroupState(props: CheckboxGroupProps = {}): CheckboxGroupState {\n let [selectedValues, setValue] = useControlledState(props.value, props.defaultValue || [], props.onChange);\n let isRequired = props.isRequired && selectedValues.length === 0;\n\n let invalidValues = useRef(new Map<string, ValidationResult>());\n let validation = useFormValidationState({\n ...props,\n value: selectedValues\n });\n\n let isInvalid = validation.displayValidation.isInvalid;\n const state: CheckboxGroupState = {\n ...validation,\n value: selectedValues,\n setValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n\n setValue(value);\n },\n isDisabled: props.isDisabled || false,\n isReadOnly: props.isReadOnly || false,\n isSelected(value) {\n return selectedValues.includes(value);\n },\n addValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n if (!selectedValues.includes(value)) {\n setValue(selectedValues.concat(value));\n }\n },\n removeValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n if (selectedValues.includes(value)) {\n setValue(selectedValues.filter(existingValue => existingValue !== value));\n }\n },\n toggleValue(value) {\n if (props.isReadOnly || props.isDisabled) {\n return;\n }\n if (selectedValues.includes(value)) {\n setValue(selectedValues.filter(existingValue => existingValue !== value));\n } else {\n setValue(selectedValues.concat(value));\n }\n },\n setInvalid(value, v) {\n let s = new Map(invalidValues.current);\n if (v.isInvalid) {\n s.set(value, v);\n } else {\n s.delete(value);\n }\n\n invalidValues.current = s;\n validation.updateValidation(mergeValidation(...s.values()));\n },\n validationState: props.validationState ?? (isInvalid ? 'invalid' : null),\n isInvalid,\n isRequired\n };\n\n return state;\n}\n"],"names":[],"version":3,"file":"module.js.map"}
package/dist/types.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { CheckboxGroupProps } from "@react-types/checkbox";
2
- import { ValidationState } from "@react-types/shared";
3
- export interface CheckboxGroupState {
2
+ import { FormValidationState } from "@react-stately/form";
3
+ import { ValidationResult, ValidationState } from "@react-types/shared";
4
+ export interface CheckboxGroupState extends FormValidationState {
4
5
  /** Current selected values. */
5
6
  readonly value: readonly string[];
6
7
  /** Whether the checkbox group is disabled. */
@@ -11,9 +12,14 @@ export interface CheckboxGroupState {
11
12
  * The current validation state of the checkbox group.
12
13
  * @deprecated Use `isInvalid` instead.
13
14
  */
14
- readonly validationState: ValidationState;
15
+ readonly validationState: ValidationState | null;
15
16
  /** Whether the checkbox group is invalid. */
16
17
  readonly isInvalid: boolean;
18
+ /**
19
+ * Whether the checkboxes in the group are required.
20
+ * This changes to false once at least one item is selected.
21
+ */
22
+ readonly isRequired: boolean;
17
23
  /** Returns whether the given value is selected. */
18
24
  isSelected(value: string): boolean;
19
25
  /** Sets the selected values. */
@@ -24,6 +30,8 @@ export interface CheckboxGroupState {
24
30
  removeValue(value: string): void;
25
31
  /** Toggles a value in the set of selected values. */
26
32
  toggleValue(value: string): void;
33
+ /** Sets whether one of the checkboxes is invalid. */
34
+ setInvalid(value: string, validation: ValidationResult): void;
27
35
  }
28
36
  /**
29
37
  * Provides state management for a checkbox group component. Provides a name for the group,
@@ -1 +1 @@
1
- {"mappings":";;AAgBA;IACE,+BAA+B;IAC/B,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;IAElC,8CAA8C;IAC9C,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAE7B,+CAA+C;IAC/C,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAE7B;;;OAGG;IACH,QAAQ,CAAC,eAAe,EAAE,eAAe,CAAC;IAE1C,6CAA6C;IAC7C,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAE5B,mDAAmD;IACnD,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IAEnC,gCAAgC;IAChC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAEhC,kDAAkD;IAClD,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAE9B,uDAAuD;IACvD,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAEjC,qDAAqD;IACrD,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;CACjC;AAED;;;GAGG;AACH,sCAAsC,KAAK,GAAE,kBAAuB,GAAG,kBAAkB,CAgDxF;ACzFD,YAAY,EAAC,kBAAkB,EAAC,MAAM,uBAAuB,CAAC","sources":["packages/@react-stately/checkbox/src/packages/@react-stately/checkbox/src/useCheckboxGroupState.ts","packages/@react-stately/checkbox/src/packages/@react-stately/checkbox/src/index.ts","packages/@react-stately/checkbox/src/index.ts"],"sourcesContent":[null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useCheckboxGroupState} from './useCheckboxGroupState';\n\nexport type {CheckboxGroupProps} from '@react-types/checkbox';\nexport type {CheckboxGroupState} from './useCheckboxGroupState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
1
+ {"mappings":";;;AAkBA,mCAAoC,SAAQ,mBAAmB;IAC7D,+BAA+B;IAC/B,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;IAElC,8CAA8C;IAC9C,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAE7B,+CAA+C;IAC/C,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAE7B;;;OAGG;IACH,QAAQ,CAAC,eAAe,EAAE,eAAe,GAAG,IAAI,CAAC;IAEjD,6CAA6C;IAC7C,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAE5B;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAE7B,mDAAmD;IACnD,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IAEnC,gCAAgC;IAChC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAEhC,kDAAkD;IAClD,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAE9B,uDAAuD;IACvD,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAEjC,qDAAqD;IACrD,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAEjC,qDAAqD;IACrD,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,gBAAgB,GAAG,IAAI,CAAA;CAC9D;AAED;;;GAGG;AACH,sCAAsC,KAAK,GAAE,kBAAuB,GAAG,kBAAkB,CAqExF;ACzHD,YAAY,EAAC,kBAAkB,EAAC,MAAM,uBAAuB,CAAC","sources":["packages/@react-stately/checkbox/src/packages/@react-stately/checkbox/src/useCheckboxGroupState.ts","packages/@react-stately/checkbox/src/packages/@react-stately/checkbox/src/index.ts","packages/@react-stately/checkbox/src/index.ts"],"sourcesContent":[null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useCheckboxGroupState} from './useCheckboxGroupState';\n\nexport type {CheckboxGroupProps} from '@react-types/checkbox';\nexport type {CheckboxGroupState} from './useCheckboxGroupState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-stately/checkbox",
3
- "version": "3.5.2-nightly.4230+192e66d11",
3
+ "version": "3.5.2-nightly.4242+068ecd12e",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -22,10 +22,11 @@
22
22
  "url": "https://github.com/adobe/react-spectrum"
23
23
  },
24
24
  "dependencies": {
25
- "@react-stately/toggle": "3.0.0-nightly.2519+192e66d11",
26
- "@react-stately/utils": "3.0.0-nightly.2519+192e66d11",
27
- "@react-types/checkbox": "3.0.0-nightly.2519+192e66d11",
28
- "@react-types/shared": "3.0.0-nightly.2519+192e66d11",
25
+ "@react-stately/form": "3.0.0-nightly.4242+068ecd12e",
26
+ "@react-stately/toggle": "3.0.0-nightly.2531+068ecd12e",
27
+ "@react-stately/utils": "3.0.0-nightly.2531+068ecd12e",
28
+ "@react-types/checkbox": "3.0.0-nightly.2531+068ecd12e",
29
+ "@react-types/shared": "3.0.0-nightly.2531+068ecd12e",
29
30
  "@swc/helpers": "^0.5.0"
30
31
  },
31
32
  "peerDependencies": {
@@ -34,5 +35,5 @@
34
35
  "publishConfig": {
35
36
  "access": "public"
36
37
  },
37
- "gitHead": "192e66d11e2f49caba5194e2094bfd1534db7cda"
38
+ "gitHead": "068ecd12e8a8c61546dfda691b04ca87cbdae687"
38
39
  }
@@ -11,10 +11,12 @@
11
11
  */
12
12
 
13
13
  import {CheckboxGroupProps} from '@react-types/checkbox';
14
+ import {FormValidationState, mergeValidation, useFormValidationState} from '@react-stately/form';
14
15
  import {useControlledState} from '@react-stately/utils';
15
- import {ValidationState} from '@react-types/shared';
16
+ import {useRef} from 'react';
17
+ import {ValidationResult, ValidationState} from '@react-types/shared';
16
18
 
17
- export interface CheckboxGroupState {
19
+ export interface CheckboxGroupState extends FormValidationState {
18
20
  /** Current selected values. */
19
21
  readonly value: readonly string[],
20
22
 
@@ -28,11 +30,17 @@ export interface CheckboxGroupState {
28
30
  * The current validation state of the checkbox group.
29
31
  * @deprecated Use `isInvalid` instead.
30
32
  */
31
- readonly validationState: ValidationState,
33
+ readonly validationState: ValidationState | null,
32
34
 
33
35
  /** Whether the checkbox group is invalid. */
34
36
  readonly isInvalid: boolean,
35
37
 
38
+ /**
39
+ * Whether the checkboxes in the group are required.
40
+ * This changes to false once at least one item is selected.
41
+ */
42
+ readonly isRequired: boolean,
43
+
36
44
  /** Returns whether the given value is selected. */
37
45
  isSelected(value: string): boolean,
38
46
 
@@ -46,7 +54,10 @@ export interface CheckboxGroupState {
46
54
  removeValue(value: string): void,
47
55
 
48
56
  /** Toggles a value in the set of selected values. */
49
- toggleValue(value: string): void
57
+ toggleValue(value: string): void,
58
+
59
+ /** Sets whether one of the checkboxes is invalid. */
60
+ setInvalid(value: string, validation: ValidationResult): void
50
61
  }
51
62
 
52
63
  /**
@@ -55,8 +66,17 @@ export interface CheckboxGroupState {
55
66
  */
56
67
  export function useCheckboxGroupState(props: CheckboxGroupProps = {}): CheckboxGroupState {
57
68
  let [selectedValues, setValue] = useControlledState(props.value, props.defaultValue || [], props.onChange);
69
+ let isRequired = props.isRequired && selectedValues.length === 0;
70
+
71
+ let invalidValues = useRef(new Map<string, ValidationResult>());
72
+ let validation = useFormValidationState({
73
+ ...props,
74
+ value: selectedValues
75
+ });
58
76
 
77
+ let isInvalid = validation.displayValidation.isInvalid;
59
78
  const state: CheckboxGroupState = {
79
+ ...validation,
60
80
  value: selectedValues,
61
81
  setValue(value) {
62
82
  if (props.isReadOnly || props.isDisabled) {
@@ -96,8 +116,20 @@ export function useCheckboxGroupState(props: CheckboxGroupProps = {}): CheckboxG
96
116
  setValue(selectedValues.concat(value));
97
117
  }
98
118
  },
99
- validationState: props.validationState,
100
- isInvalid: props.isInvalid || props.validationState === 'invalid'
119
+ setInvalid(value, v) {
120
+ let s = new Map(invalidValues.current);
121
+ if (v.isInvalid) {
122
+ s.set(value, v);
123
+ } else {
124
+ s.delete(value);
125
+ }
126
+
127
+ invalidValues.current = s;
128
+ validation.updateValidation(mergeValidation(...s.values()));
129
+ },
130
+ validationState: props.validationState ?? (isInvalid ? 'invalid' : null),
131
+ isInvalid,
132
+ isRequired
101
133
  };
102
134
 
103
135
  return state;